|
| 1 | +import * as t from './actionTypes'; |
| 2 | +import api from 'lib/api'; |
| 3 | +import messages from 'lib/text'; |
| 4 | + |
| 5 | +function requestStatuses() { |
| 6 | + return { |
| 7 | + type: t.STATUSES_REQUEST |
| 8 | + }; |
| 9 | +} |
| 10 | + |
| 11 | +function receiveStatuses(items) { |
| 12 | + return { |
| 13 | + type: t.STATUSES_RECEIVE, |
| 14 | + items |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +function receiveErrorStatuses(error) { |
| 19 | + return { |
| 20 | + type: t.STATUSES_FAILURE, |
| 21 | + error |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +export function selectStatus(id) { |
| 26 | + return { |
| 27 | + type: t.STATUSES_SELECT, |
| 28 | + selectedId: id |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +export function deselectStatus() { |
| 33 | + return { |
| 34 | + type: t.STATUSES_DESELECT |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function requestUpdateStatus(id) { |
| 39 | + return { |
| 40 | + type: t.STATUS_UPDATE_REQUEST |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function receiveUpdateStatus() { |
| 45 | + return { |
| 46 | + type: t.STATUS_UPDATE_SUCCESS |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function errorUpdateStatus(error) { |
| 51 | + return { |
| 52 | + type: t.STATUS_UPDATE_FAILURE, |
| 53 | + error |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function successCreateStatus(id) { |
| 58 | + return { |
| 59 | + type: t.STATUS_CREATE_SUCCESS |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function successDeleteStatus(id) { |
| 64 | + return { |
| 65 | + type: t.STATUS_DELETE_SUCCESS |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +function fetchStatuses() { |
| 70 | + return dispatch => { |
| 71 | + dispatch(requestStatuses()); |
| 72 | + return api.orderStatuses |
| 73 | + .list() |
| 74 | + .then(({ status, json }) => { |
| 75 | + json = json.sort((a, b) => a.position - b.position); |
| 76 | + |
| 77 | + json.forEach((element, index, theArray) => { |
| 78 | + if (theArray[index].name === '') { |
| 79 | + theArray[index].name = `<${messages.draft}>`; |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + dispatch(receiveStatuses(json)); |
| 84 | + }) |
| 85 | + .catch(error => { |
| 86 | + dispatch(receiveErrorStatuses(error)); |
| 87 | + }); |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +function shouldFetchStatuses(state) { |
| 92 | + const statuses = state.orderStatuses; |
| 93 | + if (statuses.isFetched || statuses.isFetching) { |
| 94 | + return false; |
| 95 | + } else { |
| 96 | + return true; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export function fetchStatusesIfNeeded() { |
| 101 | + return (dispatch, getState) => { |
| 102 | + if (shouldFetchStatuses(getState())) { |
| 103 | + return dispatch(fetchStatuses()); |
| 104 | + } |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +export function updateStatus(data) { |
| 109 | + return (dispatch, getState) => { |
| 110 | + dispatch(requestUpdateStatus(data.id)); |
| 111 | + return api.orderStatuses |
| 112 | + .update(data.id, data) |
| 113 | + .then(({ status, json }) => { |
| 114 | + dispatch(receiveUpdateStatus()); |
| 115 | + dispatch(fetchStatuses()); |
| 116 | + }) |
| 117 | + .catch(error => { |
| 118 | + dispatch(errorUpdateStatus(error)); |
| 119 | + }); |
| 120 | + }; |
| 121 | +} |
| 122 | + |
| 123 | +export function createStatus(data) { |
| 124 | + return (dispatch, getState) => { |
| 125 | + return api.orderStatuses |
| 126 | + .create(data) |
| 127 | + .then(({ status, json }) => { |
| 128 | + dispatch(successCreateStatus(json.id)); |
| 129 | + dispatch(fetchStatuses()); |
| 130 | + dispatch(selectStatus(json.id)); |
| 131 | + }) |
| 132 | + .catch(error => { |
| 133 | + //dispatch error |
| 134 | + console.log(error); |
| 135 | + }); |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +export function deleteStatus(id) { |
| 140 | + return (dispatch, getState) => { |
| 141 | + return api.orderStatuses |
| 142 | + .delete(id) |
| 143 | + .then(({ status, json }) => { |
| 144 | + if (status === 200) { |
| 145 | + dispatch(successDeleteStatus(id)); |
| 146 | + dispatch(deselectStatus()); |
| 147 | + dispatch(fetchStatuses()); |
| 148 | + } else { |
| 149 | + throw status; |
| 150 | + } |
| 151 | + }) |
| 152 | + .catch(error => { |
| 153 | + //dispatch error |
| 154 | + console.log(error); |
| 155 | + }); |
| 156 | + }; |
| 157 | +} |
0 commit comments