From 4c73b14d691f36e01d54730c9ace801c999de9cc Mon Sep 17 00:00:00 2001 From: Alexey Safronov Date: Wed, 21 Aug 2019 14:14:18 +0300 Subject: [PATCH] web: People: Re-written updateUserStatus with new action setUser; Moved resetFilter to actions; --- .../pages/Home/Section/Body/index.js | 34 +++-- .../Client/src/store/people/actions.js | 34 ++++- .../Client/src/store/people/reducers.js | 119 +++++++++--------- 3 files changed, 108 insertions(+), 79 deletions(-) diff --git a/products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js b/products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js index 37a37a1ba8..90fe0bde55 100644 --- a/products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js +++ b/products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js @@ -16,8 +16,8 @@ import { selectUser, deselectUser, setSelection, - fetchPeople, - updateUserStatus + updateUserStatus, + resetFilter } from "../../../../../store/people/actions"; import { isUserSelected, @@ -92,27 +92,23 @@ class SectionBodyContent extends React.PureComponent { }; onDisableClick = user => { - const { updateUserStatus, filter, fetchPeople, onLoading } = this.props; + const { updateUserStatus, onLoading } = this.props; onLoading(true); updateUserStatus(EmployeeStatus.Disabled, [user.id]) - .then(fetchPeople(filter)) - .finally(() => { - onLoading(false); - toastr.success("Context action: Enable"); - }); + .then(() => toastr.success("SUCCESS Context action: Disable")) + .catch((e) => toastr.error("FAILED Context action: Disable", e)) + .finally(() => onLoading(false)); }; onEnableClick = user => { - const { updateUserStatus, filter, fetchPeople, onLoading } = this.props; + const { updateUserStatus, onLoading } = this.props; onLoading(true); updateUserStatus(EmployeeStatus.Active, [user.id]) - .then(fetchPeople(filter)) - .finally(() => { - onLoading(false); - toastr.success("Context action: Enable"); - }); + .then(() => toastr.success("SUCCESS Context action: Enable")) + .catch((e) => toastr.error("FAILED Context action: Enable", e)) + .finally(() => onLoading(false)); }; onReassignDataClick = () => { @@ -242,10 +238,9 @@ class SectionBodyContent extends React.PureComponent { }; onResetFilter = () => { - const { filter, fetchPeople, onLoading } = this.props; - const newFilter = filter.clone(true); + const { onLoading } = this.props; onLoading(true); - fetchPeople(newFilter).finally(() => onLoading(false)); + resetFilter().finally(() => onLoading(false)); }; render() { @@ -309,12 +304,11 @@ const mapStateToProps = state => { selected: state.people.selected, users: state.people.users, viewer: state.auth.user, - settings: state.auth.settings, - filter: state.people.filter + settings: state.auth.settings }; }; export default connect( mapStateToProps, - { selectUser, deselectUser, setSelection, fetchPeople, updateUserStatus } + { selectUser, deselectUser, setSelection, updateUserStatus, resetFilter } )(withRouter(withTranslation()(SectionBodyContent))); diff --git a/products/ASC.People/Client/src/store/people/actions.js b/products/ASC.People/Client/src/store/people/actions.js index 27fb92f19c..98bada5992 100644 --- a/products/ASC.People/Client/src/store/people/actions.js +++ b/products/ASC.People/Client/src/store/people/actions.js @@ -3,6 +3,7 @@ import Filter from "./filter"; export const SET_GROUPS = "SET_GROUPS"; export const SET_USERS = "SET_USERS"; +export const SET_USER = "SET_USER"; export const SET_SELECTION = "SET_SELECTION"; export const SELECT_USER = "SELECT_USER"; export const DESELECT_USER = "DESELECT_USER"; @@ -10,6 +11,13 @@ export const SET_SELECTED = "SET_SELECTED"; export const SET_FILTER = "SET_FILTER"; export const SELECT_GROUP = "SELECT_GROUP"; +export function setUser(user) { + return { + type: SET_USER, + user + }; +} + export function setUsers(users) { return { type: SET_USERS, @@ -88,7 +96,27 @@ export async function fetchPeopleAsync(dispatch, filter = null) { } export function updateUserStatus(status, userIds) { - return dispatch => { - return api.updateUserStatus(status, userIds); + return (dispatch) => { + return api + .updateUserStatus(status, userIds) + .then(res => { + if (res && res.data && res.data.error && res.data.error.message) + throw res.data.error.message; + + const users = res.data.response; + + users.forEach(user => { + dispatch(setUser(user)); + }); + }); }; -} \ No newline at end of file +} + +export function resetFilter() { + return (dispatch, getState) => { + const { people } = getState(); + const { filter } = people; + + return fetchPeople(filter); + }; +} diff --git a/products/ASC.People/Client/src/store/people/reducers.js b/products/ASC.People/Client/src/store/people/reducers.js index d1a591dd43..f37f4bfe99 100644 --- a/products/ASC.People/Client/src/store/people/reducers.js +++ b/products/ASC.People/Client/src/store/people/reducers.js @@ -1,67 +1,74 @@ import { - SET_GROUPS, - SET_USERS, - SET_SELECTION, - SELECT_USER, - DESELECT_USER, - SET_SELECTED, - SET_FILTER, - SELECT_GROUP + SET_GROUPS, + SET_USERS, + SET_SELECTION, + SELECT_USER, + DESELECT_USER, + SET_SELECTED, + SET_FILTER, + SELECT_GROUP, + SET_USER } from "./actions"; -import { isUserSelected, skipUser, getUsersBySelected } from './selectors'; +import { isUserSelected, skipUser, getUsersBySelected } from "./selectors"; import Filter from "./filter"; const initialState = { - users: [], - groups: [], - selection: [], - selected: "none", - selectedGroup: null, - filter: Filter.getDefault() + users: [], + groups: [], + selection: [], + selected: "none", + selectedGroup: null, + filter: Filter.getDefault() }; const peopleReducer = (state = initialState, action) => { - switch (action.type) { - case SET_GROUPS: - return Object.assign({}, state, { - groups: action.groups - }); - case SET_USERS: - return Object.assign({}, state, { - users: action.users - }); - case SET_SELECTION: - return Object.assign({}, state, { - selection: action.selection - }); - case SELECT_USER: - if (!isUserSelected(state.selection, action.user.id)) { - return Object.assign({}, state, { - selection: [...state.selection, action.user] - }); - } else return state; - case DESELECT_USER: - if (isUserSelected(state.selection, action.user.id)) { - return Object.assign({}, state, { - selection: skipUser(state.selection, action.user.id) - }); - } else return state; - case SET_SELECTED: - return Object.assign({}, state, { - selected: action.selected, - selection: getUsersBySelected(state.users, action.selected) - }); - case SET_FILTER: - return Object.assign({}, state, { - filter: action.filter - }); - case SELECT_GROUP: - return Object.assign({}, state, { - selectedGroup: action.groupId - }); - default: - return state; - } + switch (action.type) { + case SET_GROUPS: + return Object.assign({}, state, { + groups: action.groups + }); + case SET_USERS: + return Object.assign({}, state, { + users: action.users + }); + case SET_USER: + return Object.assign({}, state, { + users: state.users.map(user => + user.id === action.user.id ? action.user : user + ) + }); + case SET_SELECTION: + return Object.assign({}, state, { + selection: action.selection + }); + case SELECT_USER: + if (!isUserSelected(state.selection, action.user.id)) { + return Object.assign({}, state, { + selection: [...state.selection, action.user] + }); + } else return state; + case DESELECT_USER: + if (isUserSelected(state.selection, action.user.id)) { + return Object.assign({}, state, { + selection: skipUser(state.selection, action.user.id) + }); + } else return state; + case SET_SELECTED: + return Object.assign({}, state, { + selected: action.selected, + selection: getUsersBySelected(state.users, action.selected) + }); + case SET_FILTER: + return Object.assign({}, state, { + filter: action.filter + }); + case SELECT_GROUP: + return Object.assign({}, state, { + selectedGroup: action.groupId + }); + default: + return state; + } }; export default peopleReducer;