From 6aa507b49c46c07fa558e3116c1e78e87a9ae183 Mon Sep 17 00:00:00 2001 From: Chris Brame Date: Wed, 1 Jan 2020 23:51:25 -0500 Subject: [PATCH] fix(accounts): issue where accounts would load twice on create ticket --- src/client/actions/accounts.js | 6 +++++ src/client/actions/types.js | 1 + .../containers/Modals/CreateTicketModal.jsx | 10 ++++---- src/client/reducers/accountsReducer.js | 25 ++++++++++++++++++- src/client/sagas/accounts/index.js | 17 +++++++++++++ 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/client/actions/accounts.js b/src/client/actions/accounts.js index 986aeae30..41efa2617 100644 --- a/src/client/actions/accounts.js +++ b/src/client/actions/accounts.js @@ -18,11 +18,17 @@ import { DELETE_ACCOUNT, ENABLE_ACCOUNT, FETCH_ACCOUNTS, + FETCH_ACCOUNTS_CREATE_TICKET, SAVE_EDIT_ACCOUNT, UNLOAD_ACCOUNTS } from 'actions/types' export const fetchAccounts = createAction(FETCH_ACCOUNTS.ACTION, payload => payload, () => ({ thunk: true })) +export const fetchAccountsCreateTicket = createAction( + FETCH_ACCOUNTS_CREATE_TICKET.ACTION, + payload => payload, + () => ({ thunk: true }) +) export const createAccount = createAction(CREATE_ACCOUNT.ACTION) export const saveEditAccount = createAction(SAVE_EDIT_ACCOUNT.ACTION) export const deleteAccount = createAction(DELETE_ACCOUNT.ACTION) diff --git a/src/client/actions/types.js b/src/client/actions/types.js index 958cb4eea..e9a0bdd44 100644 --- a/src/client/actions/types.js +++ b/src/client/actions/types.js @@ -48,6 +48,7 @@ export const CREATE_TAG = defineAction('CREATE_TAG', [SUCCESS, ERROR]) // Accounts export const FETCH_ACCOUNTS = defineAction('FETCH_ACCOUNTS', [PENDING, SUCCESS, ERROR]) +export const FETCH_ACCOUNTS_CREATE_TICKET = defineAction('FETCH_ACCOUNTS_CREATE_TICKET', [PENDING, SUCCESS, ERROR]) export const CREATE_ACCOUNT = defineAction('CREATE_ACCOUNT', [PENDING, SUCCESS, ERROR]) export const SAVE_EDIT_ACCOUNT = defineAction('SAVE_EDIT_ACCOUNT', [PENDING, SUCCESS, ERROR]) export const DELETE_ACCOUNT = defineAction('DELETE_ACCOUNT', [PENDING, SUCCESS, ERROR]) diff --git a/src/client/containers/Modals/CreateTicketModal.jsx b/src/client/containers/Modals/CreateTicketModal.jsx index f88620b49..868b71a4f 100644 --- a/src/client/containers/Modals/CreateTicketModal.jsx +++ b/src/client/containers/Modals/CreateTicketModal.jsx @@ -22,7 +22,7 @@ import axios from 'axios' import Log from '../../logger' import { createTicket } from 'actions/tickets' import { fetchGroups } from 'actions/groups' -import { fetchAccounts } from 'actions/accounts' +import { fetchAccountsCreateTicket } from 'actions/accounts' import $ from 'jquery' import helpers from 'lib/helpers' @@ -50,7 +50,7 @@ class CreateTicketModal extends React.Component { componentDidMount () { this.props.fetchGroups() - this.props.fetchAccounts({ type: 'all', limit: 1000 }) + this.props.fetchAccountsCreateTicket({ type: 'all', limit: 1000 }) helpers.UI.inputs() helpers.formvalidator() this.defaultTicketTypeWatcher = when( @@ -323,17 +323,17 @@ CreateTicketModal.propTypes = { groups: PropTypes.object.isRequired, createTicket: PropTypes.func.isRequired, fetchGroups: PropTypes.func.isRequired, - fetchAccounts: PropTypes.func.isRequired + fetchAccountsCreateTicket: PropTypes.func.isRequired } const mapStateToProps = state => ({ shared: state.shared, viewdata: state.common, groups: state.groupsState.groups, - accounts: state.accountsState.accounts + accounts: state.accountsState.accountsCreateTicket }) export default connect( mapStateToProps, - { createTicket, fetchGroups, fetchAccounts } + { createTicket, fetchGroups, fetchAccountsCreateTicket } )(CreateTicketModal) diff --git a/src/client/reducers/accountsReducer.js b/src/client/reducers/accountsReducer.js index 07027cd10..710262b9b 100644 --- a/src/client/reducers/accountsReducer.js +++ b/src/client/reducers/accountsReducer.js @@ -19,6 +19,7 @@ import { DELETE_ACCOUNT, ENABLE_ACCOUNT, FETCH_ACCOUNTS, + FETCH_ACCOUNTS_CREATE_TICKET, SAVE_EDIT_ACCOUNT, UNLOAD_ACCOUNTS } from 'actions/types' @@ -26,7 +27,10 @@ import { const initialState = { accounts: List([]), type: 'customers', - loading: false + loading: false, + + accountsCreateTicket: List([]), + createTicketLoading: false } const reducer = handleActions( @@ -51,6 +55,25 @@ const reducer = handleActions( } }, + [FETCH_ACCOUNTS_CREATE_TICKET.PENDING]: state => { + return { + ...state, + createTicketLoading: true + } + }, + + [FETCH_ACCOUNTS_CREATE_TICKET.SUCCESS]: (state, action) => { + let arr = state.accountsCreateTicket.toArray() + action.payload.response.accounts.map(i => { + arr.push(i) + }) + return { + ...state, + accountsCreateTicket: fromJS(arr), + createTicketLoading: false + } + }, + [CREATE_ACCOUNT.SUCCESS]: (state, action) => { const resAccount = action.response.account diff --git a/src/client/sagas/accounts/index.js b/src/client/sagas/accounts/index.js index 874f389fa..05da8ed95 100644 --- a/src/client/sagas/accounts/index.js +++ b/src/client/sagas/accounts/index.js @@ -18,6 +18,7 @@ import { DELETE_ACCOUNT, ENABLE_ACCOUNT, FETCH_ACCOUNTS, + FETCH_ACCOUNTS_CREATE_TICKET, HIDE_MODAL, SAVE_EDIT_ACCOUNT, UNLOAD_ACCOUNTS @@ -41,6 +42,21 @@ function * fetchAccounts ({ payload, meta }) { } } +function * fetchAccountsCreateTicket ({ payload, meta }) { + try { + const response = yield call(api.accounts.getWithPage, payload) + yield put({ type: FETCH_ACCOUNTS_CREATE_TICKET.SUCCESS, payload: { response, payload }, meta }) + } catch (error) { + const errorText = error.response ? error.response.data.error : error + if (error.response && error.response.status !== (401 || 403)) { + Log.error(errorText, error) + helpers.UI.showSnackbar(`Error: ${errorText}`, true) + } + + yield put({ type: FETCH_ACCOUNTS_CREATE_TICKET.ERROR, error }) + } +} + function * createAccount ({ payload }) { try { const response = yield call(api.accounts.create, payload) @@ -109,6 +125,7 @@ function * unloadThunk ({ payload, meta }) { export default function * watcher () { yield takeLatest(CREATE_ACCOUNT.ACTION, createAccount) yield takeLatest(FETCH_ACCOUNTS.ACTION, fetchAccounts) + yield takeLatest(FETCH_ACCOUNTS_CREATE_TICKET.ACTION, fetchAccountsCreateTicket) yield takeLatest(SAVE_EDIT_ACCOUNT.ACTION, saveEditAccount) yield takeEvery(DELETE_ACCOUNT.ACTION, deleteAccount) yield takeEvery(ENABLE_ACCOUNT.ACTION, enableAccount)