|
| 1 | +# redux-compose-async |
| 2 | + |
| 3 | +## TOC |
| 4 | +### Actions |
| 5 | +- createActionCreator() |
| 6 | +- createActionFetchParent() |
| 7 | +- createActionFetchParents() |
| 8 | +- createActionParent() |
| 9 | +- promisifyActionCreator() |
| 10 | + |
| 11 | +### Reducer |
| 12 | +- createDataReducer() |
| 13 | + |
| 14 | +### Sagas |
| 15 | +- createFetchSaga() |
| 16 | +- createRootSaga() |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +# Actions |
| 21 | +## `createActionCreator()` |
| 22 | +### signature |
| 23 | +```js |
| 24 | +createActionCreator(MY_ACTION_PARENT: { action: string }): (payload, resolve, reject) => ({ |
| 25 | + type: action, |
| 26 | + payload?: {}, |
| 27 | + resolve: () => void, |
| 28 | + reject: () => void |
| 29 | +}) |
| 30 | +``` |
| 31 | +### example |
| 32 | +```js |
| 33 | +createActionFetchParent({ action: 'FETCH_FROM_API' }) |
| 34 | +``` |
| 35 | +### returns |
| 36 | +```js |
| 37 | +(payload, resolve, reject) => ({ |
| 38 | + type: 'FETCH_FROM_API', |
| 39 | + () => {}, |
| 40 | + () => {} |
| 41 | +}) |
| 42 | +``` |
| 43 | +## `createActionFetchParent()` |
| 44 | +### signature |
| 45 | +```js |
| 46 | +createActionFetchParent(actionName: string): { |
| 47 | + [actionName]: { |
| 48 | + action: actionName, |
| 49 | + success: `${actionName}_SUCCESS`, |
| 50 | + error: `${actionName}_ERROR`, |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | +### example |
| 55 | +```js |
| 56 | +createActionFetchParent('FETCH_FROM_API') |
| 57 | +``` |
| 58 | +### returns |
| 59 | +```js |
| 60 | +{ |
| 61 | + FETCH_FROM_API: { |
| 62 | + action: 'FETCH_FROM_API', |
| 63 | + success: 'FETCH_FROM_API_SUCCESS', |
| 64 | + error: 'FETCH_FROM_API_ERROR', |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | +## `createActionFetchParents()` |
| 69 | +### signature |
| 70 | +```js |
| 71 | +createActionFetchParents(actionNames: string[]): { |
| 72 | + [actionName]: { |
| 73 | + action: actionName, |
| 74 | + success: `${actionName}_SUCCESS`, |
| 75 | + error: `${actionName}_ERROR`, |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | +### example |
| 80 | +```js |
| 81 | +createActionFetchParents(['FETCH_FROM_API', 'PUT_TO_API']) |
| 82 | +``` |
| 83 | +### returns |
| 84 | +```js |
| 85 | +{ |
| 86 | + FETCH_FROM_API: { |
| 87 | + action: 'FETCH_FROM_API', |
| 88 | + success: 'FETCH_FROM_API_SUCCESS', |
| 89 | + error: 'FETCH_FROM_API_ERROR', |
| 90 | + }, |
| 91 | + PUT_TO_API: { |
| 92 | + action: 'PUT_TO_API', |
| 93 | + success: 'PUT_TO_API_SUCCESS', |
| 94 | + error: 'PUT_TO_API_ERROR', |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | +## `createActionParent()` |
| 99 | +### signature |
| 100 | +```js |
| 101 | +createActionParent(actionName: string): { |
| 102 | + [actionName]: { |
| 103 | + action: actionName |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | +### example |
| 108 | +```js |
| 109 | +createActionParent('SET_FILES') |
| 110 | +``` |
| 111 | +### returns |
| 112 | +```js |
| 113 | +{ |
| 114 | + SET_FILES: { |
| 115 | + action: 'SET_FILES', |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | +## `promisifyActionCreator()` |
| 120 | +### signature |
| 121 | +```js |
| 122 | +promisifyActionCreator(dispatch: () => void, actionCreator: () => void): Promise() |
| 123 | +``` |
| 124 | +### example |
| 125 | +```js |
| 126 | + dispatch => ({ |
| 127 | + fetchFromMyApi: promisifyActionCreator(dispatch, fetchFromMyApi) |
| 128 | +}) |
| 129 | +``` |
| 130 | +### returns |
| 131 | +```js |
| 132 | +Promise |
| 133 | +``` |
| 134 | +# Reducer |
| 135 | +## `createDataReducer()` |
| 136 | +### signature |
| 137 | +```js |
| 138 | +createDataReducer(actionFetchParent): (state: {}, {type: string, payload: {}}) => state |
| 139 | +``` |
| 140 | +### example |
| 141 | +```js |
| 142 | +createDataReducer({ |
| 143 | + 'FETCH_API': { |
| 144 | + action: 'FETCH_API', |
| 145 | + success: 'FETCH_API_SUCCESS', |
| 146 | + error: 'FETCH_API_ERROR', |
| 147 | + } |
| 148 | +}) |
| 149 | +``` |
| 150 | +### returns |
| 151 | +```js |
| 152 | +(state = { |
| 153 | + data: null, |
| 154 | + loading: null, |
| 155 | + error: null, |
| 156 | + }, |
| 157 | + { |
| 158 | + type, |
| 159 | + payload |
| 160 | + } |
| 161 | +) => { |
| 162 | + switch (type) { |
| 163 | + case action: |
| 164 | + return { |
| 165 | + ...state, |
| 166 | + error: null, |
| 167 | + loading: true, |
| 168 | + data: null, |
| 169 | + } |
| 170 | + case success: |
| 171 | + return { |
| 172 | + ...state, |
| 173 | + error: null, |
| 174 | + loading: false, |
| 175 | + data: payload.data, |
| 176 | + } |
| 177 | + case error: |
| 178 | + return { |
| 179 | + ...state, |
| 180 | + error: payload.error, |
| 181 | + loading: false, |
| 182 | + data: null, |
| 183 | + } |
| 184 | + default: |
| 185 | + return { ...state } |
| 186 | + } |
| 187 | +} |
| 188 | +``` |
0 commit comments