From b4daf8fc98da50520e2114d29f81f3256f8a99b9 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Wed, 25 Oct 2017 19:19:59 +0200 Subject: [PATCH] Move searchValue to Redux --- client/coral-admin/src/actions/community.js | 6 +++++ client/coral-admin/src/constants/community.js | 2 ++ client/coral-admin/src/reducers/community.js | 7 +++++ .../src/routes/Community/containers/People.js | 26 ++++++++----------- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/client/coral-admin/src/actions/community.js b/client/coral-admin/src/actions/community.js index 89427f7bb..6a091d214 100644 --- a/client/coral-admin/src/actions/community.js +++ b/client/coral-admin/src/actions/community.js @@ -6,6 +6,7 @@ import { FETCH_USERS_FAILURE, SORT_UPDATE, SET_PAGE, + SET_SEARCH_VALUE, SET_ROLE, SET_COMMENTER_STATUS, SHOW_BANUSER_DIALOG, @@ -50,6 +51,11 @@ export const setPage = (page) => ({ page, }); +export const setSearchValue = (value) => ({ + type: SET_SEARCH_VALUE, + value, +}); + export const setRole = (id, role) => (dispatch, _, {rest}) => { return rest(`/users/${id}/role`, {method: 'POST', body: {role}}) .then(() => { diff --git a/client/coral-admin/src/constants/community.js b/client/coral-admin/src/constants/community.js index 6e8a82bfe..eea946efb 100644 --- a/client/coral-admin/src/constants/community.js +++ b/client/coral-admin/src/constants/community.js @@ -18,3 +18,5 @@ export const HIDE_BANUSER_DIALOG = `${prefix}_HIDE_BANUSER_DIALOG`; export const SHOW_REJECT_USERNAME_DIALOG = `${prefix}_SHOW_REJECT_USERNAME_DIALOG`; export const HIDE_REJECT_USERNAME_DIALOG = `${prefix}_HIDE_REJECT_USERNAME_DIALOG`; + +export const SET_SEARCH_VALUE = `${prefix}_SET_SEARCH_VALUE`; diff --git a/client/coral-admin/src/reducers/community.js b/client/coral-admin/src/reducers/community.js index cb71634cc..170804b60 100644 --- a/client/coral-admin/src/reducers/community.js +++ b/client/coral-admin/src/reducers/community.js @@ -4,6 +4,7 @@ import { FETCH_USERS_FAILURE, SORT_UPDATE, SET_PAGE, + SET_SEARCH_VALUE, SET_ROLE, SET_COMMENTER_STATUS, SHOW_BANUSER_DIALOG, @@ -17,6 +18,7 @@ const initialState = { isFetchingPeople: false, errorPeople: '', users: [], + searchValue: '', fieldPeople: 'created_at', ascPeople: false, totalPagesPeople: 0, @@ -107,6 +109,11 @@ export default function community (state = initialState, action) { user: action.user, rejectUsernameDialog: true }; + case SET_SEARCH_VALUE: + return { + ...state, + searchValue: action.value, + }; default : return state; } diff --git a/client/coral-admin/src/routes/Community/containers/People.js b/client/coral-admin/src/routes/Community/containers/People.js index 1f52846c9..063eaef2f 100644 --- a/client/coral-admin/src/routes/Community/containers/People.js +++ b/client/coral-admin/src/routes/Community/containers/People.js @@ -13,19 +13,17 @@ import { hideRejectUsernameDialog, setCommenterStatus, setRole, + setSearchValue, } from '../../../actions/community'; class PeopleContainer extends React.Component { - state = { - searchValue: '', - timer: null - }; + timer=null; fetchUsers = (query = {}) => { const {community} = this.props; this.props.fetchUsers({ - value: this.state.searchValue, + value: community.searchValue, field: community.fieldPeople, asc: community.ascPeople, ...query @@ -46,15 +44,11 @@ class PeopleContainer extends React.Component { onSearchChange = (e) => { const value = e.target.value; - this.setState((prevState) => { - prevState.searchValue = value; - clearTimeout(prevState.timer); - - prevState.timer = setTimeout(() => { - this.fetchUsers({value}); - }, 350); - return prevState; - }); + this.props.setSearchValue(value); + clearTimeout(this.timer); + this.timer = setTimeout(() => { + this.fetchUsers({value}); + }, 350); } onHeaderClickHandler = (sort) => { @@ -71,7 +65,7 @@ class PeopleContainer extends React.Component { render() { return setCommenterStatus, setRole, viewUserDetail, + setSearchValue, }, dispatch); export default connect(null, mapDispatchToProps)(PeopleContainer);