Skip to content

Commit

Permalink
fix(accounts): issue where accounts would load twice on create ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jan 2, 2020
1 parent 7ca56f5 commit 6aa507b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/client/actions/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/client/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
10 changes: 5 additions & 5 deletions src/client/containers/Modals/CreateTicketModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
25 changes: 24 additions & 1 deletion src/client/reducers/accountsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import {
DELETE_ACCOUNT,
ENABLE_ACCOUNT,
FETCH_ACCOUNTS,
FETCH_ACCOUNTS_CREATE_TICKET,
SAVE_EDIT_ACCOUNT,
UNLOAD_ACCOUNTS
} from 'actions/types'

const initialState = {
accounts: List([]),
type: 'customers',
loading: false
loading: false,

accountsCreateTicket: List([]),
createTicketLoading: false
}

const reducer = handleActions(
Expand All @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/client/sagas/accounts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
DELETE_ACCOUNT,
ENABLE_ACCOUNT,
FETCH_ACCOUNTS,
FETCH_ACCOUNTS_CREATE_TICKET,
HIDE_MODAL,
SAVE_EDIT_ACCOUNT,
UNLOAD_ACCOUNTS
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6aa507b

Please sign in to comment.