mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Merged heads
This commit is contained in:
@@ -1,27 +1,26 @@
|
||||
// @flow
|
||||
import { apiClient, NOT_FOUND_ERROR } from "../../apiclient";
|
||||
import { apiClient } from "../../apiclient";
|
||||
import type { User } from "../types/User";
|
||||
import type { UserEntry } from "../types/UserEntry";
|
||||
import asyncActionCreator from "./asyncActionCreator";
|
||||
import { Dispatch } from "redux";
|
||||
|
||||
export const FETCH_USERS_PENDING = "scm/users/FETCH_PENDING";
|
||||
export const FETCH_USERS_SUCCESS = "scm/users/FETCH_SUCCESS";
|
||||
export const FETCH_USERS_FAILURE = "scm/users/FETCH_FAILURE";
|
||||
export const FETCH_USERS_NOTFOUND = "scm/users/FETCH_NOTFOUND";
|
||||
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";
|
||||
|
||||
export const FETCH_USER_PENDING = "scm/users/FETCH_USER_PENDING";
|
||||
export const FETCH_USER_SUCCESS = "scm/users/FETCH_USER_SUCCESS";
|
||||
export const FETCH_USER_FAILURE = "scm/users/FETCH_USER_FAILURE";
|
||||
|
||||
export const ADD_USER_PENDING = "scm/users/ADD_PENDING";
|
||||
export const ADD_USER_SUCCESS = "scm/users/ADD_SUCCESS";
|
||||
export const ADD_USER_FAILURE = "scm/users/ADD_FAILURE";
|
||||
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";
|
||||
|
||||
export const UPDATE_USER_PENDING = "scm/users/UPDATE_PENDING";
|
||||
export const UPDATE_USER_SUCCESS = "scm/users/UPDATE_SUCCESS";
|
||||
export const UPDATE_USER_FAILURE = "scm/users/UPDATE_FAILURE";
|
||||
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";
|
||||
|
||||
export const DELETE_USER_PENDING = "scm/users/DELETE_PENDING";
|
||||
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";
|
||||
|
||||
@@ -30,25 +29,11 @@ const USER_URL = "users/";
|
||||
|
||||
const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2";
|
||||
|
||||
export function requestUsers() {
|
||||
return {
|
||||
type: FETCH_USERS_PENDING
|
||||
};
|
||||
}
|
||||
|
||||
export function failedToFetchUsers(url: string, error: Error) {
|
||||
return {
|
||||
type: FETCH_USERS_FAILURE,
|
||||
payload: {
|
||||
error,
|
||||
url
|
||||
}
|
||||
};
|
||||
}
|
||||
//fetch users
|
||||
|
||||
export function fetchUsers() {
|
||||
return function(dispatch: any) {
|
||||
dispatch(requestUsers());
|
||||
dispatch(fetchUsersPending());
|
||||
return apiClient
|
||||
.get(USERS_URL)
|
||||
.then(response => {
|
||||
@@ -64,11 +49,17 @@ export function fetchUsers() {
|
||||
})
|
||||
.catch(cause => {
|
||||
const error = new Error(`could not fetch users: ${cause.message}`);
|
||||
dispatch(failedToFetchUsers(USERS_URL, error));
|
||||
dispatch(fetchUsersFailure(USERS_URL, error));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchUsersPending() {
|
||||
return {
|
||||
type: FETCH_USERS_PENDING
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchUsersSuccess(users: any) {
|
||||
return {
|
||||
type: FETCH_USERS_SUCCESS,
|
||||
@@ -76,17 +67,22 @@ export function fetchUsersSuccess(users: any) {
|
||||
};
|
||||
}
|
||||
|
||||
export function requestUser(name: string) {
|
||||
export function fetchUsersFailure(url: string, error: Error) {
|
||||
return {
|
||||
type: FETCH_USER_PENDING,
|
||||
payload: { name }
|
||||
type: FETCH_USERS_FAILURE,
|
||||
payload: {
|
||||
error,
|
||||
url
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//fetch user
|
||||
//TODO: fetchUsersPending and FetchUsersFailure are the wrong functions here!
|
||||
export function fetchUser(name: string) {
|
||||
const userUrl = USER_URL + name;
|
||||
const userUrl = USERS_URL + "/" + name;
|
||||
return function(dispatch: any) {
|
||||
dispatch(requestUser(name));
|
||||
dispatch(fetchUsersPending());
|
||||
return apiClient
|
||||
.get(userUrl)
|
||||
.then(response => {
|
||||
@@ -102,48 +98,47 @@ export function fetchUser(name: string) {
|
||||
})
|
||||
.catch(cause => {
|
||||
const error = new Error(`could not fetch user: ${cause.message}`);
|
||||
dispatch(fetchUserFailure(USERS_URL, error));
|
||||
dispatch(fetchUsersFailure(USERS_URL, error));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchUserSuccess(user: User) {
|
||||
export function fetchUserPending(name: string) {
|
||||
return {
|
||||
type: FETCH_USER_PENDING,
|
||||
payload: { name }
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchUserSuccess(user: any) {
|
||||
return {
|
||||
type: FETCH_USER_SUCCESS,
|
||||
payload: user
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchUserFailure(username: string, error: Error) {
|
||||
export function fetchUserFailure(user: User, error: Error) {
|
||||
return {
|
||||
type: FETCH_USER_FAILURE,
|
||||
error: true,
|
||||
payload: {
|
||||
username,
|
||||
error
|
||||
}
|
||||
user,
|
||||
error
|
||||
};
|
||||
}
|
||||
|
||||
export function requestAddUser(user: User) {
|
||||
return {
|
||||
type: ADD_USER_PENDING,
|
||||
payload: user
|
||||
};
|
||||
}
|
||||
//create user
|
||||
|
||||
export function addUser(user: User) {
|
||||
export function createUser(user: User) {
|
||||
return function(dispatch: Dispatch) {
|
||||
dispatch(requestAddUser(user));
|
||||
dispatch(createUserPending(user));
|
||||
return apiClient
|
||||
.postWithContentType(USERS_URL, user, CONTENT_TYPE_USER)
|
||||
.then(() => {
|
||||
dispatch(addUserSuccess());
|
||||
dispatch(createUserSuccess());
|
||||
dispatch(fetchUsers());
|
||||
})
|
||||
.catch(err =>
|
||||
dispatch(
|
||||
addUserFailure(
|
||||
createUserFailure(
|
||||
user,
|
||||
new Error(`failed to add user ${user.name}: ${err.message}`)
|
||||
)
|
||||
@@ -152,92 +147,73 @@ export function addUser(user: User) {
|
||||
};
|
||||
}
|
||||
|
||||
export function addUserSuccess() {
|
||||
export function createUserPending(user: User) {
|
||||
return {
|
||||
type: ADD_USER_SUCCESS
|
||||
type: CREATE_USER_PENDING,
|
||||
user
|
||||
};
|
||||
}
|
||||
|
||||
export function addUserFailure(user: User, error: Error) {
|
||||
|
||||
export function createUserSuccess() {
|
||||
return {
|
||||
type: ADD_USER_FAILURE,
|
||||
error: true,
|
||||
payload: {
|
||||
error,
|
||||
user
|
||||
}
|
||||
type: CREATE_USER_SUCCESS
|
||||
};
|
||||
}
|
||||
|
||||
function requestUpdateUser(user: User) {
|
||||
export function createUserFailure(user: User, err: Error) {
|
||||
return {
|
||||
type: UPDATE_USER_PENDING,
|
||||
payload: user
|
||||
type: CREATE_USER_FAILURE,
|
||||
payload: err,
|
||||
user
|
||||
};
|
||||
}
|
||||
|
||||
export function updateUser(user: User) {
|
||||
//modify user
|
||||
|
||||
export function modifyUser(user: User) {
|
||||
return function(dispatch: Dispatch) {
|
||||
dispatch(requestUpdateUser(user));
|
||||
dispatch(modifyUserPending(user));
|
||||
return apiClient
|
||||
.putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER)
|
||||
.then(() => {
|
||||
dispatch(updateUserSuccess(user));
|
||||
dispatch(modifyUserSuccess(user));
|
||||
dispatch(fetchUsers());
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
dispatch(updateUserFailure(user, err));
|
||||
dispatch(modifyUserFailure(user, err));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function updateUserSuccess(user: User) {
|
||||
function modifyUserPending(user: User) {
|
||||
return {
|
||||
type: UPDATE_USER_SUCCESS,
|
||||
payload: user
|
||||
type: MODIFY_USER_PENDING,
|
||||
user
|
||||
};
|
||||
}
|
||||
|
||||
export function updateUserFailure(user: User, error: Error) {
|
||||
function modifyUserSuccess(user: User) {
|
||||
return {
|
||||
type: UPDATE_USER_FAILURE,
|
||||
error: true,
|
||||
payload: {
|
||||
error,
|
||||
user
|
||||
}
|
||||
type: MODIFY_USER_SUCCESS,
|
||||
user
|
||||
};
|
||||
}
|
||||
|
||||
export function requestDeleteUser(user: User) {
|
||||
export function modifyUserFailure(user: User, error: Error) {
|
||||
return {
|
||||
type: DELETE_USER_PENDING,
|
||||
payload: user
|
||||
type: MODIFY_USER_FAILURE,
|
||||
payload: error,
|
||||
user
|
||||
};
|
||||
}
|
||||
|
||||
export function deleteUserSuccess(user: User) {
|
||||
return {
|
||||
type: DELETE_USER_SUCCESS,
|
||||
payload: user
|
||||
};
|
||||
}
|
||||
|
||||
export function deleteUserFailure(user: User, error: Error) {
|
||||
return {
|
||||
type: DELETE_USER_FAILURE,
|
||||
error: true,
|
||||
payload: {
|
||||
error,
|
||||
user
|
||||
}
|
||||
};
|
||||
}
|
||||
//delete user
|
||||
|
||||
export function deleteUser(user: User) {
|
||||
return function(dispatch: any) {
|
||||
dispatch(requestDeleteUser(user));
|
||||
dispatch(deleteUserPending(user));
|
||||
return apiClient
|
||||
.delete(user._links.delete.href)
|
||||
.then(() => {
|
||||
@@ -253,7 +229,33 @@ export function deleteUser(user: User) {
|
||||
};
|
||||
}
|
||||
|
||||
export function getUsersFromState(state) {
|
||||
export function deleteUserPending(user: User) {
|
||||
return {
|
||||
type: DELETE_USER,
|
||||
payload: user
|
||||
};
|
||||
}
|
||||
|
||||
export function deleteUserSuccess(user: User) {
|
||||
return {
|
||||
type: DELETE_USER_SUCCESS,
|
||||
payload: user
|
||||
};
|
||||
}
|
||||
|
||||
export function deleteUserFailure(user: User, error: Error) {
|
||||
return {
|
||||
type: DELETE_USER_FAILURE,
|
||||
payload: {
|
||||
error,
|
||||
user
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//helper functions
|
||||
|
||||
export function getUsersFromState(state: any) {
|
||||
if (!state.users.users) {
|
||||
return null;
|
||||
}
|
||||
@@ -291,7 +293,7 @@ function extractUsersByNames(
|
||||
function deleteUserInUsersByNames(users: {}, userName: any) {
|
||||
let newUsers = {};
|
||||
for (let username in users) {
|
||||
if (username != userName) newUsers[username] = users[username];
|
||||
if (username !== userName) newUsers[username] = users[username];
|
||||
}
|
||||
return newUsers;
|
||||
}
|
||||
@@ -299,7 +301,7 @@ function deleteUserInUsersByNames(users: {}, userName: any) {
|
||||
function deleteUserInEntries(users: [], userName: any) {
|
||||
let newUsers = [];
|
||||
for (let user of users) {
|
||||
if (user != userName) newUsers.push(user);
|
||||
if (user !== userName) newUsers.push(user);
|
||||
}
|
||||
return newUsers;
|
||||
}
|
||||
@@ -381,12 +383,12 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
usersByNames: ubn
|
||||
};
|
||||
case FETCH_USER_FAILURE:
|
||||
return reduceUsersByNames(state, action.payload.username, {
|
||||
return reduceUsersByNames(state, action.user.name, {
|
||||
loading: true,
|
||||
error: action.payload.error
|
||||
error: action.error
|
||||
});
|
||||
// Delete single user cases
|
||||
case DELETE_USER_PENDING:
|
||||
case DELETE_USER:
|
||||
return reduceUsersByNames(state, action.payload.name, {
|
||||
loading: true,
|
||||
error: null,
|
||||
@@ -423,7 +425,7 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
}
|
||||
};
|
||||
// Add single user cases
|
||||
case ADD_USER_PENDING:
|
||||
case CREATE_USER_PENDING:
|
||||
return {
|
||||
...state,
|
||||
users: {
|
||||
@@ -432,7 +434,7 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
error: null
|
||||
}
|
||||
};
|
||||
case ADD_USER_SUCCESS:
|
||||
case CREATE_USER_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
users: {
|
||||
@@ -441,17 +443,17 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
error: null
|
||||
}
|
||||
};
|
||||
case ADD_USER_FAILURE:
|
||||
case CREATE_USER_FAILURE:
|
||||
return {
|
||||
...state,
|
||||
users: {
|
||||
...state.users,
|
||||
loading: false,
|
||||
error: action.payload.error
|
||||
error: action.payload
|
||||
}
|
||||
};
|
||||
// Update single user cases
|
||||
case UPDATE_USER_PENDING:
|
||||
case MODIFY_USER_PENDING:
|
||||
return {
|
||||
...state,
|
||||
usersByNames: {
|
||||
@@ -463,7 +465,7 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
}
|
||||
}
|
||||
};
|
||||
case UPDATE_USER_SUCCESS:
|
||||
case MODIFY_USER_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
usersByNames: {
|
||||
|
||||
Reference in New Issue
Block a user