Files
SCM-Manager/scm-ui/src/users/modules/users.js

149 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-07-09 11:38:13 +02:00
// @flow
2018-07-10 15:18:37 +02:00
import {apiClient, PAGE_NOT_FOUND_ERROR} from '../../apiclient';
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 15:18:37 +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-10 15:18:37 +02:00
2018-07-02 16:05:58 +02:00
return function(dispatch) {
2018-07-10 15:18:37 +02:00
dispatch(requestUsers());
return apiClient.get(USERS_URL)
.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
})
.catch((err) => {
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 15:18:37 +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-04 09:55:02 +02:00
export function shouldFetchUsers(state: any): boolean {
2018-07-10 15:18:37 +02:00
if(state.users.users == null){
return true;
}
return false;
2018-07-02 16:05:58 +02:00
}
2018-07-04 09:55:02 +02:00
export function fetchUsersIfNeeded() {
2018-07-02 16:05:58 +02:00
return (dispatch, getState) => {
2018-07-04 09:55:02 +02:00
if (shouldFetchUsers(getState())) {
dispatch(fetchUsers());
2018-07-02 16:05:58 +02:00
}
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 {
type: DELETE_USER_SUCCESS,
};
}
function deleteUserFailure(url: string, err: Error) {
return {
type: DELETE_USER_FAILURE,
payload: err,
url
};
}
export function deleteUser(username: string) {
return function(dispatch) {
dispatch(requestDeleteUser(username));
return apiClient.delete(USERS_URL + '/' + username)
.then(() => {
dispatch(deleteUserSuccess());
})
.catch((err) => dispatch(deleteUserFailure(username, err)));
}
}
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-02 16:05:58 +02:00
return {
...state,
2018-07-10 15:18:37 +02:00
users: null
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-10 08:38:38 +02:00
users: action.payload._embedded.users
2018-07-02 16:05:58 +02:00
};
2018-07-04 09:55:02 +02:00
case FETCH_USERS_FAILURE:
2018-07-02 16:05:58 +02:00
return {
...state,
2018-07-02 16:22:24 +02:00
login: false,
2018-07-02 16:05:58 +02:00
error: action.payload
};
2018-07-10 15:18:37 +02:00
case DELETE_USER_SUCCESS:
return {
...state,
users: null
};
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
}
}