Skip to content

Commit 041e1ac

Browse files
committed
Added the ADD method
1 parent 37dfe6f commit 041e1ac

File tree

5 files changed

+61
-33
lines changed

5 files changed

+61
-33
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ this.$store.dispatch("api/queueActionWatcher", {
159159
});
160160
```
161161

162+
### `add`
163+
164+
Add data in the store without fetching it from API. This is useful in cases like Websockets.
165+
166+
```js
167+
this.$store.dispatch('api/add', { type: 'resource' data: {...} });
168+
this.$store.dispatch('api/add', { type: 'resource' data: [{...}] });
169+
```
170+
162171
The store can be emptied with the reset action.
163172

164173
```js

lib/Actions.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,13 @@ class ActionBase<S, R> {
7171
path: string,
7272
data: IndexedObject | Array<IndexedObject>
7373
) {
74-
commit(`SAVE_ORIGIN_${path}`, cloneDeep(data));
7574
const modified = await applyModifier(
7675
ModifierName.afterGet,
7776
type,
7877
this._models,
7978
data
8079
);
81-
80+
commit(`SAVE_ORIGIN_${path}`, cloneDeep(modified));
8281
commit(`ADD_${path}`, modified);
8382
}
8483

@@ -101,6 +100,23 @@ class ActionBase<S, R> {
101100
return resultData;
102101
}
103102

103+
// add Entity to Store
104+
async _addEntity(commit: Commit, payload: Payload) {
105+
if (get(payload, "clear")) {
106+
commit(`CLEAR_${this._getModel(payload).name.toUpperCase()}`);
107+
}
108+
if (payload.data) {
109+
this._addToStore(
110+
commit,
111+
payload.type,
112+
this._getModel(payload).name.toUpperCase(),
113+
payload.data
114+
);
115+
}
116+
117+
return payload.data;
118+
}
119+
104120
// store entity to API
105121
async _storeEntity(
106122
commit: Commit,
@@ -242,6 +258,7 @@ export default class Actions<S, R> implements ActionTree<S, R> {
242258
post: Action<S, R>;
243259
patch: Action<S, R>;
244260
delete: Action<S, R>;
261+
add: Action<S, R>;
245262
// queueActionWatcher: Action<S, R>;
246263
queueAction: Action<S, R>;
247264
processActionQueue: Action<S, R>;
@@ -257,8 +274,9 @@ export default class Actions<S, R> implements ActionTree<S, R> {
257274
const entity = base._getEntity(state, payload);
258275
if (payload.forceFetch || !entity) {
259276
return base._fetchEntity(commit, payload);
277+
} else {
278+
return entity;
260279
}
261-
return entity;
262280
};
263281

264282
this.post = ({ commit }: ActionContext<S, R>, payload: Payload) => {
@@ -273,6 +291,10 @@ export default class Actions<S, R> implements ActionTree<S, R> {
273291
return base._deleteEntity(commit, payload);
274292
};
275293

294+
this.add = ({ commit }: ActionContext<S, R>, payload: Payload) => {
295+
return base._addEntity(commit, payload);
296+
};
297+
276298
this.queueAction = async (
277299
{ commit }: ActionContext<S, R>,
278300
payload: QueuePayload

lib/ApiStore.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ export default class ApiStore<S> implements StoreOptions<S> {
126126
) => {
127127
const state = myState[model.plural];
128128
forEach(state.actionQueue, (actionList, action) => {
129-
if (action === "patch") {
130-
forEach(actionList, (item, id) => this.revertOriginItem(state, id));
131-
}
132-
133129
state.actionQueue[action] = isArray(actionList) ? [] : {};
134130
});
135131
};
@@ -174,7 +170,9 @@ export default class ApiStore<S> implements StoreOptions<S> {
174170
} else {
175171
this.saveOriginItem(state, data);
176172
forEach(model.references, (modelName, prop) => {
177-
this.saveOrigins(store, this.models[modelName], data[prop]);
173+
if (data[prop]) {
174+
this.saveOrigins(store, this.models[modelName], data[prop]);
175+
}
178176
});
179177
}
180178
} catch (e) {
@@ -214,31 +212,34 @@ export default class ApiStore<S> implements StoreOptions<S> {
214212
entity: IndexedObject
215213
// eslint-disable-next-line @typescript-eslint/no-explicit-any
216214
): IndexedObject {
217-
// Patch references
218-
if (model.references) {
219-
forEach(model.references, (modelName, prop) => {
220-
entity[prop] = this.patchReference(store, entity, modelName, prop);
221-
});
222-
}
215+
if (entity && entity.id) {
216+
// Patch references
217+
if (model.references) {
218+
forEach(model.references, (modelName, prop) => {
219+
entity[prop] = this.patchReference(store, entity, modelName, prop);
220+
});
221+
}
223222

224-
const state = store[model.plural];
223+
const state = store[model.plural];
225224

226-
if (has(state.items, entity.id)) {
227-
const storeEntity = state.items[entity.id];
228-
forEach(entity, (value, name: string) => {
229-
if (!isFunction(value) && !has(model.references, name)) {
230-
if (has(entity, name) && !isEqual(value, get(storeEntity, name))) {
231-
Vue.set(storeEntity, name, value);
225+
if (has(state.items, entity.id)) {
226+
const storeEntity = state.items[entity.id];
227+
forEach(entity, (value, name: string) => {
228+
if (!isFunction(value) && !has(model.references, name)) {
229+
if (has(entity, name) && !isEqual(value, get(storeEntity, name))) {
230+
Vue.set(storeEntity, name, value);
231+
}
232232
}
233-
}
234-
});
233+
});
235234

236-
return state.items[entity.id];
237-
} else {
238-
state.items = { ...state.items, ...this.getIndexedMap(entity) };
235+
return state.items[entity.id];
236+
} else {
237+
state.items = { ...state.items, ...this.getIndexedMap(entity) };
239238

240-
return entity;
239+
return entity;
240+
}
241241
}
242+
return entity;
242243
}
243244

244245
private patchReference(

lib/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ export async function applyModifier(
3333
return !isFunction(fn) ? Promise.resolve(data) : await fn(data);
3434
} catch (e) {
3535
// eslint-disable-next-line no-console
36-
console.warn(
37-
`Modifier error on "${modelName}" for data ${JSON.stringify(data)}`
38-
);
36+
console.warn(`Modifier error on "${modelName}". ${e}`);
3937
}
4038
};
4139

tests/unit/apistoreCustomModel.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ describe("ApiStore custom model", function() {
7171
});
7272
console.info("Start expected log");
7373
await flushPromises();
74-
expect(spyWarn).toHaveBeenCalledWith(
75-
"We could not find the model RESOURCES."
76-
);
74+
expect(spyWarn).toHaveBeenCalled();
7775
console.info("End expected log");
7876
});
7977

0 commit comments

Comments
 (0)