Skip to content

Commit a08a889

Browse files
committed
Move all async code out of mutations
1 parent 49837a0 commit a08a889

File tree

6 files changed

+272
-199
lines changed

6 files changed

+272
-199
lines changed

lib/Actions.ts

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ import keys from "lodash-es/keys";
1111
import map from "lodash-es/map";
1212
import values from "lodash-es/values";
1313
import ApiState from "./ApiState";
14-
import { ModelTypeTree, Payload, QueuePayload, ModelType } from "./types";
14+
import {
15+
IndexedObject,
16+
ModelTypeTree,
17+
Payload,
18+
QueuePayload,
19+
QueuePayloadWithModifiers,
20+
ModelType,
21+
ModifierName
22+
} from "./types";
1523
import { applyModifier, formatUrl } from "./utils";
1624

1725
class ActionBase<S, R> {
@@ -57,21 +65,40 @@ class ActionBase<S, R> {
5765
return get(state, `${this._getModel(payload).plural}.items`)[payload.id];
5866
}
5967

68+
async _addToStore(
69+
commit: Commit,
70+
type: string,
71+
path: string,
72+
data: IndexedObject | Array<IndexedObject>
73+
) {
74+
commit(`SAVE_ORIGIN_${path}`, cloneDeep(data));
75+
const modified = await applyModifier(
76+
ModifierName.afterGet,
77+
type,
78+
this._models,
79+
data
80+
);
81+
82+
commit(`ADD_${path}`, modified);
83+
}
84+
6085
// fetch entity from API
61-
_fetchEntity(commit: Commit, payload: Payload) {
86+
async _fetchEntity(commit: Commit, payload: Payload) {
6287
if (get(payload, "clear", this._isAll(payload))) {
6388
commit(`CLEAR_${this._getModel(payload).name.toUpperCase()}`);
6489
}
65-
return this._axios
66-
.get(formatUrl(payload), payload.axiosConfig)
67-
.then(async result => {
68-
const resultData = get(result, this._dataPath);
69-
commit(
70-
`ADD_${this._getModel(payload).name.toUpperCase()}`,
71-
cloneDeep(resultData)
72-
);
73-
return resultData;
74-
});
90+
const result = await this._axios.get(
91+
formatUrl(payload),
92+
payload.axiosConfig
93+
);
94+
const resultData = get(result, this._dataPath);
95+
this._addToStore(
96+
commit,
97+
payload.type,
98+
this._getModel(payload).name.toUpperCase(),
99+
resultData
100+
);
101+
return resultData;
75102
}
76103

77104
// store entity to API
@@ -84,21 +111,22 @@ class ActionBase<S, R> {
84111
method,
85112
url: formatUrl(payload),
86113
data: await applyModifier(
87-
"beforeSave",
114+
ModifierName.beforeSave,
88115
payload.type,
89116
this._models,
90117
payload.data
91118
)
92119
};
93120
const config = { ...mainConfig, ...payload.axiosConfig };
94-
return this._axios(config).then(result => {
95-
const resultData = get(result, this._dataPath);
96-
commit(
97-
`ADD_${this._getModel(payload).name.toUpperCase()}`,
98-
cloneDeep(resultData)
99-
);
100-
return resultData;
101-
});
121+
const result = await this._axios(config);
122+
const resultData = get(result, this._dataPath);
123+
this._addToStore(
124+
commit,
125+
payload.type,
126+
this._getModel(payload).name.toUpperCase(),
127+
resultData
128+
);
129+
return resultData;
102130
}
103131

104132
// delete entity to API
@@ -110,7 +138,12 @@ class ActionBase<S, R> {
110138
return this._axios
111139
.patch(
112140
`${formatUrl(payload)}/delete`,
113-
await applyModifier("beforeSave", payload.type, this._models, data),
141+
await applyModifier(
142+
ModifierName.beforeSave,
143+
payload.type,
144+
this._models,
145+
data
146+
),
114147
payload.axiosConfig
115148
)
116149
.then(() => {
@@ -125,6 +158,34 @@ class ActionBase<S, R> {
125158
});
126159
}
127160

161+
async _getQueuePayloadWithModifiers({
162+
data,
163+
type,
164+
action
165+
}: QueuePayload): Promise<QueuePayloadWithModifiers> {
166+
const afterGet = await applyModifier(
167+
ModifierName.afterGet,
168+
type,
169+
this._models,
170+
data
171+
);
172+
173+
return {
174+
action,
175+
id: data.id,
176+
afterGet,
177+
toQueue: {
178+
type,
179+
data: await applyModifier(
180+
ModifierName.beforeSave,
181+
type,
182+
this._models,
183+
data
184+
)
185+
}
186+
};
187+
}
188+
128189
_processAction(action: Method, payload: Payload, commit: Commit) {
129190
if (action === "delete") {
130191
return this._deleteEntity(commit, payload);
@@ -163,7 +224,12 @@ class ActionBase<S, R> {
163224
const origin = at(get(state, `${model.plural}.originItems`), originIds);
164225
commit(
165226
`ADD_${model.name}`,
166-
await applyModifier("afterQueue", queue, this._models, origin)
227+
await applyModifier(
228+
ModifierName.afterQueue,
229+
queue,
230+
this._models,
231+
origin
232+
)
167233
);
168234
commit(`RESET_QUEUE_${model.name}`);
169235
}
@@ -207,11 +273,14 @@ export default class Actions<S, R> implements ActionTree<S, R> {
207273
return base._deleteEntity(commit, payload);
208274
};
209275

210-
this.queueAction = (
276+
this.queueAction = async (
211277
{ commit }: ActionContext<S, R>,
212278
payload: QueuePayload
213279
) => {
214-
return commit(`QUEUE_ACTION_${base._getModel(payload).name}`, payload);
280+
return commit(
281+
`QUEUE_ACTION_${base._getModel(payload).name}`,
282+
await base._getQueuePayloadWithModifiers(payload)
283+
);
215284
};
216285

217286
this.processAction = (

0 commit comments

Comments
 (0)