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

490 lines
11 KiB
JavaScript
Raw Normal View History

2018-07-09 11:38:13 +02:00
// @flow
2018-07-23 17:00:33 +02:00
import { apiClient } from "../../apiclient";
2018-07-17 17:00:45 +02:00
import type { User } from "../types/User";
import type { UserEntry } from "../types/UserEntry";
import { Dispatch } from "redux";
2018-07-24 16:45:09 +02:00
import { Action } from "../../types/Action";
2018-07-09 11:38:13 +02:00
2018-07-23 17:00:33 +02:00
export const FETCH_USERS_PENDING = "scm/users/FETCH_USERS_PENDING";
export const FETCH_USERS_SUCCESS = "scm/users/FETCH_USERS_SUCCESS";
export const FETCH_USERS_FAILURE = "scm/users/FETCH_USERS_FAILURE";
2018-07-02 16:05:58 +02:00
2018-07-24 13:02:50 +02:00
export const FETCH_USER_PENDING = "scm/users/FETCH_USER_PENDING";
export const FETCH_USER_SUCCESS = "scm/users/FETCH_USER_SUCCESS";
2018-07-19 17:37:20 +02:00
export const FETCH_USER_FAILURE = "scm/users/FETCH_USER_FAILURE";
2018-07-23 17:00:33 +02:00
export const CREATE_USER_PENDING = "scm/users/CREATE_USER_PENDING";
export const CREATE_USER_SUCCESS = "scm/users/CREATE_USER_SUCCESS";
export const CREATE_USER_FAILURE = "scm/users/CREATE_USER_FAILURE";
2018-07-11 17:02:38 +02:00
2018-07-23 17:00:33 +02:00
export const MODIFY_USER_PENDING = "scm/users/MODIFY_USER_PENDING";
export const MODIFY_USER_SUCCESS = "scm/users/MODIFY_USER_SUCCESS";
export const MODIFY_USER_FAILURE = "scm/users/MODIFY_USER_FAILURE";
2018-07-11 17:02:38 +02:00
2018-07-12 16:02:37 +02:00
export const DELETE_USER = "scm/users/DELETE";
export const DELETE_USER_SUCCESS = "scm/users/DELETE_SUCCESS";
export const DELETE_USER_FAILURE = "scm/users/DELETE_FAILURE";
2018-07-10 15:18:37 +02:00
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-17 15:10:37 +02:00
2018-07-23 17:00:33 +02:00
//fetch users
2018-07-10 15:18:37 +02:00
2018-07-10 08:38:38 +02:00
export function fetchUsers() {
2018-07-17 17:00:45 +02:00
return function(dispatch: any) {
2018-07-23 17:00:33 +02:00
dispatch(fetchUsersPending());
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-19 11:57:59 +02:00
.catch(cause => {
const error = new Error(`could not fetch users: ${cause.message}`);
2018-07-23 17:00:33 +02:00
dispatch(fetchUsersFailure(USERS_URL, error));
2018-07-10 08:38:38 +02:00
});
2018-07-10 16:52:23 +02:00
};
2018-07-10 08:38:38 +02:00
}
2018-07-24 16:45:09 +02:00
export function fetchUsersPending(): Action {
2018-07-23 17:00:33 +02:00
return {
type: FETCH_USERS_PENDING
};
}
2018-07-24 16:45:09 +02:00
export function fetchUsersSuccess(users: any): Action {
2018-07-10 08:38:38 +02:00
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-24 16:45:09 +02:00
export function fetchUsersFailure(url: string, error: Error): Action {
return {
2018-07-23 17:00:33 +02:00
type: FETCH_USERS_FAILURE,
payload: {
error,
url
}
};
}
2018-07-23 17:00:33 +02:00
//fetch user
//TODO: fetchUsersPending and FetchUsersFailure are the wrong functions here!
export function fetchUser(name: string) {
2018-07-23 17:00:33 +02:00
const userUrl = USERS_URL + "/" + name;
return function(dispatch: any) {
2018-07-24 16:45:09 +02:00
dispatch(fetchUserPending(name));
return apiClient
.get(userUrl)
.then(response => {
return response;
})
.then(response => {
if (response.ok) {
return response.json();
}
})
.then(data => {
dispatch(fetchUserSuccess(data));
})
2018-07-19 11:57:59 +02:00
.catch(cause => {
const error = new Error(`could not fetch user: ${cause.message}`);
2018-07-24 16:45:09 +02:00
dispatch(fetchUserFailure(USERS_URL, error));
});
};
}
2018-07-24 16:45:09 +02:00
export function fetchUserPending(name: string): Action {
return {
2018-07-23 17:00:33 +02:00
type: FETCH_USER_PENDING,
payload: { name }
};
}
2018-07-24 16:45:09 +02:00
export function fetchUserSuccess(user: any): Action {
2018-07-19 17:37:20 +02:00
return {
type: FETCH_USER_SUCCESS,
payload: user
2018-07-19 17:37:20 +02:00
};
}
2018-07-24 16:45:09 +02:00
export function fetchUserFailure(username: string, error: Error): Action {
2018-07-11 17:02:38 +02:00
return {
2018-07-19 17:37:20 +02:00
type: FETCH_USER_FAILURE,
2018-07-24 16:45:09 +02:00
payload: {
username,
error
}
2018-07-11 17:02:38 +02:00
};
}
2018-07-23 17:00:33 +02:00
//create user
2018-07-11 17:02:38 +02:00
2018-07-23 17:00:33 +02:00
export function createUser(user: User) {
2018-07-17 17:00:45 +02:00
return function(dispatch: Dispatch) {
2018-07-23 17:00:33 +02:00
dispatch(createUserPending(user));
2018-07-11 17:02:38 +02:00
return apiClient
.postWithContentType(USERS_URL, user, CONTENT_TYPE_USER)
.then(() => {
2018-07-23 17:00:33 +02:00
dispatch(createUserSuccess());
2018-07-11 17:02:38 +02:00
dispatch(fetchUsers());
})
2018-07-19 12:05:50 +02:00
.catch(err =>
dispatch(
2018-07-23 17:00:33 +02:00
createUserFailure(
2018-07-19 12:05:50 +02:00
user,
new Error(`failed to add user ${user.name}: ${err.message}`)
)
)
);
2018-07-11 17:02:38 +02:00
};
}
2018-07-24 16:45:09 +02:00
export function createUserPending(user: User): Action {
2018-07-11 17:02:38 +02:00
return {
2018-07-23 17:00:33 +02:00
type: CREATE_USER_PENDING,
user
2018-07-11 17:02:38 +02:00
};
}
2018-07-24 16:45:09 +02:00
export function createUserSuccess(): Action {
2018-07-11 17:02:38 +02:00
return {
2018-07-23 17:00:33 +02:00
type: CREATE_USER_SUCCESS
2018-07-11 17:02:38 +02:00
};
}
2018-07-24 16:45:09 +02:00
export function createUserFailure(user: User, err: Error): Action {
2018-07-11 17:02:38 +02:00
return {
2018-07-23 17:00:33 +02:00
type: CREATE_USER_FAILURE,
payload: err,
2018-07-11 17:02:38 +02:00
user
};
}
2018-07-23 17:00:33 +02:00
//modify user
export function modifyUser(user: User) {
2018-07-17 17:00:45 +02:00
return function(dispatch: Dispatch) {
2018-07-23 17:00:33 +02:00
dispatch(modifyUserPending(user));
2018-07-11 17:02:38 +02:00
return apiClient
2018-07-17 08:39:51 +02:00
.putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER)
2018-07-11 17:02:38 +02:00
.then(() => {
2018-07-23 17:00:33 +02:00
dispatch(modifyUserSuccess(user));
2018-07-11 17:02:38 +02:00
dispatch(fetchUsers());
})
2018-07-19 12:38:26 +02:00
.catch(err => {
2018-07-23 17:00:33 +02:00
dispatch(modifyUserFailure(user, err));
2018-07-19 12:38:26 +02:00
});
2018-07-11 17:02:38 +02:00
};
}
2018-07-24 16:45:09 +02:00
function modifyUserPending(user: User): Action {
2018-07-11 17:02:38 +02:00
return {
2018-07-23 17:00:33 +02:00
type: MODIFY_USER_PENDING,
2018-07-24 16:45:09 +02:00
payload: {
user
}
2018-07-11 17:02:38 +02:00
};
}
2018-07-24 16:45:09 +02:00
function modifyUserSuccess(user: User): Action {
2018-07-11 17:02:38 +02:00
return {
2018-07-23 17:00:33 +02:00
type: MODIFY_USER_SUCCESS,
2018-07-24 16:45:09 +02:00
payload: {
user
}
2018-07-23 17:00:33 +02:00
};
}
2018-07-24 16:45:09 +02:00
export function modifyUserFailure(user: User, error: Error): Action {
2018-07-23 17:00:33 +02:00
return {
type: MODIFY_USER_FAILURE,
2018-07-24 16:45:09 +02:00
payload: {
error,
user
}
2018-07-11 17:02:38 +02:00
};
}
2018-07-23 17:00:33 +02:00
//delete user
export function deleteUser(user: User) {
return function(dispatch: any) {
dispatch(deleteUserPending(user));
return apiClient
.delete(user._links.delete.href)
.then(() => {
dispatch(deleteUserSuccess(user));
dispatch(fetchUsers());
})
.catch(cause => {
const error = new Error(
`could not delete user ${user.name}: ${cause.message}`
);
dispatch(deleteUserFailure(user, error));
});
2018-07-11 17:02:38 +02:00
};
}
2018-07-24 16:45:09 +02:00
export function deleteUserPending(user: User): Action {
2018-07-10 15:18:37 +02:00
return {
type: DELETE_USER,
payload: user
2018-07-10 15:18:37 +02:00
};
}
2018-07-24 16:45:09 +02:00
export function deleteUserSuccess(user: User): Action {
2018-07-10 15:18:37 +02:00
return {
type: DELETE_USER_SUCCESS,
payload: user
2018-07-10 15:18:37 +02:00
};
}
2018-07-24 16:45:09 +02:00
export function deleteUserFailure(user: User, error: Error): Action {
2018-07-10 15:18:37 +02:00
return {
type: DELETE_USER_FAILURE,
2018-07-17 16:35:01 +02:00
payload: {
error,
user
}
2018-07-10 15:18:37 +02:00
};
}
2018-07-23 17:00:33 +02:00
//helper functions
2018-07-10 15:18:37 +02:00
2018-07-23 17:00:33 +02:00
export function getUsersFromState(state: any) {
2018-07-12 16:02:37 +02:00
if (!state.users.users) {
return null;
}
const userNames = state.users.users.entries;
if (!userNames) {
return null;
}
2018-07-17 17:00:45 +02:00
const userEntries: Array<UserEntry> = [];
2018-07-12 16:02:37 +02:00
for (let userName of userNames) {
userEntries.push(state.users.usersByNames[userName]);
}
return userEntries;
}
2018-07-12 16:51:32 +02:00
function extractUsersByNames(
2018-07-17 17:00:45 +02:00
users: Array<User>,
userNames: Array<string>,
2018-07-12 16:51:32 +02:00
oldUsersByNames: {}
) {
2018-07-17 17:00:45 +02:00
const usersByNames = {};
2018-07-12 16:02:37 +02:00
for (let user of users) {
usersByNames[user.name] = {
entry: user
};
}
2018-07-12 16:51:32 +02:00
2018-07-17 17:00:45 +02:00
for (let userName in oldUsersByNames) {
2018-07-12 16:51:32 +02:00
usersByNames[userName] = oldUsersByNames[userName];
}
2018-07-12 16:02:37 +02:00
return usersByNames;
}
2018-07-19 12:38:26 +02:00
function deleteUserInUsersByNames(users: {}, userName: any) {
let newUsers = {};
2018-07-19 12:38:26 +02:00
for (let username in users) {
2018-07-23 17:00:33 +02:00
if (username !== userName) newUsers[username] = users[username];
}
return newUsers;
}
2018-07-19 12:38:26 +02:00
function deleteUserInEntries(users: [], userName: any) {
let newUsers = [];
2018-07-19 12:38:26 +02:00
for (let user of users) {
2018-07-23 17:00:33 +02:00
if (user !== userName) newUsers.push(user);
}
return newUsers;
}
2018-07-12 16:02:37 +02:00
2018-07-17 17:00:45 +02:00
const reduceUsersByNames = (
state: any,
username: string,
newUserState: any
) => {
const newUsersByNames = {
...state.usersByNames,
2018-07-17 16:35:01 +02:00
[username]: newUserState
};
return {
...state,
usersByNames: newUsersByNames
};
};
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-19 17:37:20 +02:00
// fetch user list cases
2018-07-24 13:02:50 +02:00
case FETCH_USERS_PENDING:
2018-07-12 16:02:37 +02:00
return {
...state,
users: {
loading: true
}
2018-07-12 16:02:37 +02:00
};
2018-07-04 09:55:02 +02:00
case FETCH_USERS_SUCCESS:
// return red(state, action.payload._embedded.users);
2018-07-12 16:02:37 +02:00
const users = action.payload._embedded.users;
const userNames = users.map(user => user.name);
2018-07-12 16:51:32 +02:00
const usersByNames = extractUsersByNames(
users,
userNames,
state.usersByNames
);
2018-07-02 16:05:58 +02:00
return {
...state,
2018-07-12 16:02:37 +02:00
users: {
2018-07-19 12:05:50 +02:00
userCreatePermission: action.payload._links.create ? true : false,
2018-07-12 16:02:37 +02:00
error: null,
entries: userNames,
loading: false
},
usersByNames
2018-07-02 16:05:58 +02:00
};
2018-07-19 17:37:20 +02:00
case FETCH_USERS_FAILURE:
return {
...state,
users: {
...state.users,
loading: false,
error: action.payload.error
}
};
// Fetch single user cases
2018-07-24 13:02:50 +02:00
case FETCH_USER_PENDING:
2018-07-19 17:37:20 +02:00
return reduceUsersByNames(state, action.payload.name, {
loading: true,
error: null
});
case FETCH_USER_SUCCESS:
const ubn = extractUsersByNames(
[action.payload],
[action.payload.name],
state.usersByNames
);
return {
...state,
users: {
error: null,
entries: [action.payload.name],
loading: false
},
usersByNames: ubn
};
2018-07-19 17:37:20 +02:00
case FETCH_USER_FAILURE:
2018-07-24 16:45:09 +02:00
return reduceUsersByNames(state, action.payload.username, {
2018-07-19 17:37:20 +02:00
loading: true,
2018-07-24 16:45:09 +02:00
error: action.payload.error
2018-07-19 17:37:20 +02:00
});
// Delete single user cases
case DELETE_USER:
return reduceUsersByNames(state, action.payload.name, {
loading: true,
error: null,
entry: action.payload
});
case DELETE_USER_SUCCESS:
2018-07-19 12:38:26 +02:00
const newUserByNames = deleteUserInUsersByNames(state.usersByNames, [
action.payload.name
]);
const newUserEntries = deleteUserInEntries(state.users.entries, [
action.payload.name
]);
return {
...state,
users: {
...state.users,
entries: newUserEntries
},
usersByNames: newUserByNames
2018-07-19 12:38:26 +02:00
};
2018-07-19 17:37:20 +02:00
case DELETE_USER_FAILURE:
const newState = reduceUsersByNames(state, action.payload.user.name, {
2018-07-17 16:35:01 +02:00
loading: false,
error: action.payload.error,
entry: action.payload.user
});
return {
...newState,
users: {
...newState.users,
error: action.payload.error
}
};
2018-07-19 17:37:20 +02:00
// Add single user cases
2018-07-23 17:00:33 +02:00
case CREATE_USER_PENDING:
2018-07-19 12:05:50 +02:00
return {
...state,
users: {
...state.users,
loading: true,
error: null
}
};
2018-07-23 17:00:33 +02:00
case CREATE_USER_SUCCESS:
2018-07-19 12:05:50 +02:00
return {
...state,
users: {
...state.users,
loading: false,
2018-07-19 12:38:26 +02:00
error: null
}
};
2018-07-23 17:00:33 +02:00
case CREATE_USER_FAILURE:
2018-07-17 08:39:51 +02:00
return {
...state,
2018-07-19 12:05:50 +02:00
users: {
...state.users,
loading: false,
error: action.payload
}
2018-07-17 08:39:51 +02:00
};
2018-07-19 17:37:20 +02:00
// Update single user cases
2018-07-23 17:00:33 +02:00
case MODIFY_USER_PENDING:
2018-07-19 12:38:26 +02:00
return {
...state,
usersByNames: {
...state.usersByNames,
[action.user.name]: {
loading: true,
error: null,
entry: action.user
}
}
};
2018-07-23 17:00:33 +02:00
case MODIFY_USER_SUCCESS:
2018-07-19 12:38:26 +02:00
return {
...state,
usersByNames: {
...state.usersByNames,
[action.user.name]: {
loading: false,
error: null,
entry: action.user
}
}
};
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
}
}