-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathlocalStorageMiddleware.ts
More file actions
65 lines (56 loc) · 2.28 KB
/
localStorageMiddleware.ts
File metadata and controls
65 lines (56 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Dispatch, Middleware, UnknownAction } from '@reduxjs/toolkit';
import { collectionsCache } from '../../modules/cache/collections.cache';
import { samplesCache } from '../../modules/cache/samples.cache';
import { AppAction } from '../../types/action';
import { Collection, ResourcePath } from '../../types/resources';
import { CURRENT_THEME } from '../services/graph-constants';
import { getUniquePaths } from '../services/reducers/collections-reducer.util';
import {
CHANGE_THEME_SUCCESS, COLLECTION_CREATE_SUCCESS,
ODATA_ABNF_RULES_CREATE_SUCCESS,
RESOURCEPATHS_ADD_SUCCESS, RESOURCEPATHS_DELETE_SUCCESS, SAMPLES_FETCH_SUCCESS
} from '../services/redux-constants';
import { saveToLocalStorage } from '../utils/local-storage';
import { odataAbnfCache } from '../../modules/cache/odataAbnfRules.cache';
const localStorageMiddleware: Middleware<{}, any, Dispatch<UnknownAction>> = () => (next) => async (value) => {
const action = value as AppAction;
switch (action.type) {
case CHANGE_THEME_SUCCESS:
saveToLocalStorage(CURRENT_THEME, action.payload);
break;
case SAMPLES_FETCH_SUCCESS:
samplesCache.saveSamples(action.payload);
break;
case RESOURCEPATHS_ADD_SUCCESS: {
const collections = await collectionsCache.read();
const item = collections.find(k => k.isDefault)!;
item.paths = getUniquePaths(item.paths, action.payload as ResourcePath[]);
await collectionsCache.update(item.id, item);
break;
}
case RESOURCEPATHS_DELETE_SUCCESS: {
const paths = action.payload as ResourcePath[];
const collections = await collectionsCache.read();
const collection = collections.find(k => k.isDefault)!;
paths.forEach((path: ResourcePath) => {
const index = collection.paths.findIndex(k => k.key === path.key);
if (index > -1) {
collection.paths.splice(index, 1);
}
})
await collectionsCache.update(collection.id, collection);
break;
}
case COLLECTION_CREATE_SUCCESS: {
await collectionsCache.create(action.payload as Collection);
break;
}
case ODATA_ABNF_RULES_CREATE_SUCCESS:
odataAbnfCache.saveGrammar(action.payload as string);
break;
default:
break;
}
return next(action);
}
export default localStorageMiddleware;