sipp11
6 years ago
8 changed files with 199 additions and 53 deletions
@ -0,0 +1,79 @@
|
||||
import { |
||||
FARERULE_CREATE, FARERULE_DELETE, FARERULE_UPDATE, |
||||
FARERULE_REQUEST, FARERULE_SUCCESS, FARERULE_FAILURE, |
||||
} from '../constants/ActionTypes' |
||||
|
||||
|
||||
const fareRuleInitState = { |
||||
results: [], |
||||
next: null, |
||||
count: 0, |
||||
fetching: false, |
||||
query: '', |
||||
} |
||||
const fareRule = (state = fareRuleInitState, action) => { |
||||
switch(action.type) { |
||||
case FARERULE_REQUEST: |
||||
const { query } = action.meta |
||||
return { |
||||
...state, |
||||
fetching: true, |
||||
query, |
||||
} |
||||
case FARERULE_SUCCESS: |
||||
const { count, next, prev, results } = action.payload |
||||
return { |
||||
...state, |
||||
fetching: false, |
||||
count, |
||||
next, |
||||
results: [ |
||||
...( (prev) ? state.results : [] ), |
||||
...results, |
||||
] |
||||
} |
||||
case FARERULE_UPDATE: |
||||
const { id } = action.payload |
||||
const oldResults = state.results |
||||
const targetInd = oldResults.findIndex(ele => ele.id === id) |
||||
return { |
||||
...state, |
||||
fetching: false, |
||||
results: [ |
||||
...oldResults.slice(0, targetInd), |
||||
action.payload, |
||||
...oldResults.slice(targetInd + 1) |
||||
] |
||||
} |
||||
case FARERULE_CREATE: |
||||
return { |
||||
...state, |
||||
fetching: false, |
||||
count: state.count + 1, |
||||
results: [ |
||||
...state.results, |
||||
action.payload, |
||||
] |
||||
} |
||||
case FARERULE_DELETE: |
||||
const deleteInd = state.results.findIndex(ele => ele.id === action.meta.id) |
||||
return { |
||||
...state, |
||||
count: state.count - 1, |
||||
fetching: false, |
||||
results: [ |
||||
...state.results.slice(0, deleteInd), |
||||
...state.results.slice(deleteInd + 1) |
||||
] |
||||
} |
||||
case FARERULE_FAILURE: |
||||
return { |
||||
...state, |
||||
fetching: false, |
||||
} |
||||
default: |
||||
return state; |
||||
} |
||||
} |
||||
|
||||
export default fareRule |
Loading…
Reference in new issue