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

213 lines
4.4 KiB
JavaScript
Raw Normal View History

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 17:02:38 +02:00
import type { User } from "../types/User";
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-11 17:02:38 +02:00
const ADD_USER = "scm/users/ADD";
const ADD_USER_SUCCESS = "scm/users/ADD_SUCCESS";
const ADD_USER_FAILURE = "scm/users/ADD_FAILURE";
const EDIT_USER = "scm/users/EDIT";
const EDIT_USER_SUCCESS = "scm/users/EDIT_SUCCESS";
const EDIT_USER_FAILURE = "scm/users/EDIT_FAILURE";
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-11 17:02:38 +02:00
const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2";
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() {
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-11 17:02:38 +02:00
function requestAddUser(user: User) {
return {
type: ADD_USER,
user
};
}
export function addUser(user: User) {
return function(dispatch: ThunkDispatch) {
dispatch(requestAddUser(user));
return apiClient
.postWithContentType(USERS_URL, user, CONTENT_TYPE_USER)
.then(() => {
dispatch(addUserSuccess());
dispatch(fetchUsers());
})
.catch(err => dispatch(addUserFailure(user, err)));
};
}
function addUserSuccess() {
return {
type: ADD_USER_SUCCESS
};
}
function addUserFailure(user: User, err: Error) {
return {
type: ADD_USER_FAILURE,
payload: err,
user
};
}
function requestAddUser(user: User) {
return {
type: ADD_USER,
user
};
}
export function editUser(user: User) {
return function(dispatch: ThunkDispatch) {
dispatch(requestAddUser(user));
return apiClient
.putWithContentType(USERS_URL + "/" + user.name, user, CONTENT_TYPE_USER)
.then(() => {
dispatch(addUserSuccess());
dispatch(fetchUsers());
})
.catch(err => dispatch(addUserFailure(user, err)));
};
}
function editUserSuccess() {
return {
type: ADD_USER_SUCCESS
};
}
function addUserFailure(user: User, err: Error) {
return {
type: ADD_USER_FAILURE,
payload: err,
user
};
}
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
};
}
export function deleteUser(link: string) {
return function(dispatch: ThunkDispatch) {
dispatch(requestDeleteUser(link));
2018-07-10 16:52:23 +02:00
return apiClient
.delete(link)
2018-07-10 15:18:37 +02:00
.then(() => {
dispatch(deleteUserSuccess());
dispatch(fetchUsers());
2018-07-10 15:18:37 +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:
case DELETE_USER:
2018-07-02 16:05:58 +02:00
return {
...state,
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,
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:
case DELETE_USER_FAILURE:
2018-07-02 16:05:58 +02:00
return {
...state,
2018-07-02 16:22:24 +02:00
login: false,
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
}
}