-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (37 loc) 路 1.05 KB
/
index.js
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
/* Data layer for data and cache management */
/* basic flags that has been used */
export const FLAGS = {
DATA_SAVED: "01",
DATA_EXIST: "02",
DATA_WIPED: "03",
DATA_UPDATED: "04",
DATA_NOT_FOUND: "05",
FAILED_TO_FETCH: "06",
};
/* save new data */
export const checkData = (dataKey) => {
/* check of the data is exist */
const data = JSON.parse(sessionStorage.getItem(dataKey));
if (data === null || data.length == 0) {
return FLAGS.DATA_NOT_FOUND;
} else {
return FLAGS.DATA_EXIST;
}
};
/* fetch data from the cache */
export const fetchData = (dataKey) => {
/* get the data from localStorage */
const data = JSON.parse(sessionStorage.getItem(dataKey));
/* check if it is undefined or empty */
if (data === undefined || Object.keys(data).length == 0) {
return FLAGS.DATA_NOT_FOUND;
} else {
return data;
}
};
/* update new data */
export const updateNewData = (requestData, keyData) => {
/* save data to sessionStorage */
sessionStorage.setItem(keyData, JSON.stringify(requestData));
return FLAGS.DATA_SAVED;
};