2018-07-09 11:38:13 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
|
|
const FETCH_USERS = "scm/users/FETCH";
|
|
|
|
|
const FETCH_USERS_SUCCESS = "scm/users/FETCH_SUCCESS";
|
|
|
|
|
const FETCH_USERS_FAILURE = "scm/users/FETCH_FAILURE";
|
2018-07-02 16:05:58 +02:00
|
|
|
|
2018-07-10 08:38:38 +02:00
|
|
|
const USERS_URL = "/scm/api/rest/v2/users";
|
|
|
|
|
|
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 08:38:38 +02:00
|
|
|
export function fetchUsers() {
|
2018-07-02 16:05:58 +02:00
|
|
|
return function(dispatch) {
|
2018-07-10 08:38:38 +02:00
|
|
|
// dispatch(requestUsers());
|
|
|
|
|
return fetch(USERS_URL, {
|
|
|
|
|
credentials: "same-origin",
|
|
|
|
|
headers: {
|
|
|
|
|
Cache: "no-cache"
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(response => {
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
return response.json();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
|
|
|
|
dispatch(fetchUsersSuccess(data));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
const users = state.users;
|
2018-07-02 16:05:58 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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-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 08:38:38 +02:00
|
|
|
users: [{ name: "" }]
|
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,
|
|
|
|
|
timestamp: action.timestamp,
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
default:
|
2018-07-09 11:38:13 +02:00
|
|
|
return state;
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
|
|
|
|
}
|