2018-07-09 11:38:13 +02:00
|
|
|
// @flow
|
2018-07-10 16:52:23 +02:00
|
|
|
import { apiClient, PAGE_NOT_FOUND_ERROR } from "../../apiclient";
|
2018-07-11 12:02:53 +02:00
|
|
|
import { ThunkDispatch } from "redux-thunk";
|
2018-07-09 11:38:13 +02:00
|
|
|
|
|
|
|
|
const FETCH_USERS = "scm/users/FETCH";
|
|
|
|
|
const FETCH_USERS_SUCCESS = "scm/users/FETCH_SUCCESS";
|
|
|
|
|
const FETCH_USERS_FAILURE = "scm/users/FETCH_FAILURE";
|
2018-07-10 16:52:23 +02:00
|
|
|
const FETCH_USERS_NOTFOUND = "scm/users/FETCH_NOTFOUND";
|
2018-07-02 16:05:58 +02:00
|
|
|
|
2018-07-10 15:18:37 +02:00
|
|
|
const DELETE_USER = "scm/users/DELETE";
|
|
|
|
|
const DELETE_USER_SUCCESS = "scm/users/DELETE_SUCCESS";
|
|
|
|
|
const DELETE_USER_FAILURE = "scm/users/DELETE_FAILURE";
|
|
|
|
|
|
|
|
|
|
const USERS_URL = "users";
|
2018-07-10 08:38:38 +02:00
|
|
|
|
2018-07-04 09:55:02 +02:00
|
|
|
function requestUsers() {
|
2018-07-02 16:05:58 +02:00
|
|
|
return {
|
2018-07-04 09:55:02 +02:00
|
|
|
type: FETCH_USERS
|
2018-07-02 16:05:58 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 15:18:37 +02:00
|
|
|
function failedToFetchUsers(url: string, err: Error) {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_USERS_FAILURE,
|
|
|
|
|
payload: err,
|
|
|
|
|
url
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function usersNotFound(url: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_USERS_NOTFOUND,
|
|
|
|
|
url
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 08:38:38 +02:00
|
|
|
export function fetchUsers() {
|
2018-07-11 12:02:53 +02:00
|
|
|
return function(dispatch: ThunkDispatch) {
|
2018-07-10 15:18:37 +02:00
|
|
|
dispatch(requestUsers());
|
2018-07-10 16:52:23 +02:00
|
|
|
return apiClient
|
|
|
|
|
.get(USERS_URL)
|
2018-07-10 15:18:37 +02:00
|
|
|
.then(response => {
|
|
|
|
|
return response;
|
|
|
|
|
})
|
2018-07-10 08:38:38 +02:00
|
|
|
.then(response => {
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
return response.json();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
|
|
|
|
dispatch(fetchUsersSuccess(data));
|
2018-07-10 15:18:37 +02:00
|
|
|
})
|
2018-07-10 16:52:23 +02:00
|
|
|
.catch(err => {
|
2018-07-10 15:18:37 +02:00
|
|
|
if (err === PAGE_NOT_FOUND_ERROR) {
|
|
|
|
|
dispatch(usersNotFound(USERS_URL));
|
|
|
|
|
} else {
|
|
|
|
|
dispatch(failedToFetchUsers(USERS_URL, err));
|
|
|
|
|
}
|
2018-07-10 08:38:38 +02:00
|
|
|
});
|
2018-07-10 16:52:23 +02:00
|
|
|
};
|
2018-07-10 08:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fetchUsersSuccess(users: any) {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_USERS_SUCCESS,
|
|
|
|
|
payload: users
|
2018-07-09 11:38:13 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-10 15:18:37 +02:00
|
|
|
function requestDeleteUser(url: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: DELETE_USER,
|
|
|
|
|
url
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deleteUserSuccess() {
|
|
|
|
|
return {
|
2018-07-10 16:52:23 +02:00
|
|
|
type: DELETE_USER_SUCCESS
|
2018-07-10 15:18:37 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deleteUserFailure(url: string, err: Error) {
|
|
|
|
|
return {
|
|
|
|
|
type: DELETE_USER_FAILURE,
|
|
|
|
|
payload: err,
|
|
|
|
|
url
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 12:02:53 +02:00
|
|
|
export function deleteUser(link: string) {
|
|
|
|
|
return function(dispatch: ThunkDispatch) {
|
|
|
|
|
dispatch(requestDeleteUser(link));
|
2018-07-10 16:52:23 +02:00
|
|
|
return apiClient
|
2018-07-11 12:02:53 +02:00
|
|
|
.delete(link)
|
2018-07-10 15:18:37 +02:00
|
|
|
.then(() => {
|
|
|
|
|
dispatch(deleteUserSuccess());
|
2018-07-11 12:02:53 +02:00
|
|
|
dispatch(fetchUsers());
|
2018-07-10 15:18:37 +02:00
|
|
|
})
|
2018-07-11 12:02:53 +02:00
|
|
|
.catch(err => dispatch(deleteUserFailure(link, err)));
|
2018-07-10 16:52:23 +02:00
|
|
|
};
|
2018-07-10 15:18:37 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-09 11:38:13 +02:00
|
|
|
export default function reducer(state: any = {}, action: any = {}) {
|
2018-07-02 16:05:58 +02:00
|
|
|
switch (action.type) {
|
2018-07-04 09:55:02 +02:00
|
|
|
case FETCH_USERS:
|
2018-07-11 12:02:53 +02:00
|
|
|
case DELETE_USER:
|
2018-07-02 16:05:58 +02:00
|
|
|
return {
|
|
|
|
|
...state,
|
2018-07-11 12:02:53 +02:00
|
|
|
users: null,
|
|
|
|
|
loading: true
|
2018-07-02 16:05:58 +02:00
|
|
|
};
|
2018-07-04 09:55:02 +02:00
|
|
|
case FETCH_USERS_SUCCESS:
|
2018-07-02 16:05:58 +02:00
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
error: null,
|
2018-07-11 12:02:53 +02:00
|
|
|
users: action.payload._embedded.users,
|
|
|
|
|
loading: false
|
2018-07-02 16:05:58 +02:00
|
|
|
};
|
2018-07-04 09:55:02 +02:00
|
|
|
case FETCH_USERS_FAILURE:
|
2018-07-11 12:02:53 +02:00
|
|
|
case DELETE_USER_FAILURE:
|
2018-07-02 16:05:58 +02:00
|
|
|
return {
|
|
|
|
|
...state,
|
2018-07-02 16:22:24 +02:00
|
|
|
login: false,
|
2018-07-11 12:02:53 +02:00
|
|
|
error: action.payload,
|
|
|
|
|
loading: false
|
2018-07-10 15:18:37 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
|
|
|
|
|
default:
|
2018-07-09 11:38:13 +02:00
|
|
|
return state;
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
|
|
|
|
}
|