Skip to content

Commit

Permalink
Added: 2 day cache in local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
john681611 committed Nov 5, 2023
1 parent 512e65a commit b7661f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions application/frontend/src/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const TWO_DAYS_MILLISECONDS = 1.728e8;

export const TYPE_IS_PART_OF = 'Is Part Of';
export const TYPE_LINKED_TO = 'Linked To';
export const TYPE_LINKED_FROM = 'Linked From';
Expand Down
12 changes: 6 additions & 6 deletions application/frontend/src/providers/DataProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import axios from 'axios';
import React, { createContext, useContext, useEffect, useState } from 'react';
import { useQuery } from 'react-query';

import { TWO_DAYS_MILLISECONDS } from '../const';
import { useEnvironment } from '../hooks/useEnvironment';
import { Document, TreeDocument } from '../types';
import { getLocalStorageObject, setLocalStorageObject } from '../utils';
import { getDocumentDisplayName, getInternalUrl } from '../utils/document';

const DATA_STORE_KEY = 'data-store',
Expand All @@ -22,11 +24,9 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => {
const { apiUrl } = useEnvironment();
const [dataLoading, setDataLoading] = useState<boolean>(false);
const [dataStore, setDataStore] = useState<Record<string, TreeDocument>>(
JSON.parse(localStorage.getItem(DATA_STORE_KEY) || '{}')
);
const [dataTree, setDataTree] = useState<TreeDocument[]>(
JSON.parse(localStorage.getItem(DATA_TREE_KEY) || '[]')
getLocalStorageObject(DATA_STORE_KEY) || {}
);
const [dataTree, setDataTree] = useState<TreeDocument[]>(getLocalStorageObject(DATA_TREE_KEY) || []);

const getStoreKey = (doc: Document): string => {
if (doc.doctype === 'CRE') return doc.id;
Expand Down Expand Up @@ -57,7 +57,7 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => {
try {
const result = await axios.get(`${apiUrl}/root_cres`);
const treeData = result.data.data.map((x) => buildTree(x));
localStorage.setItem(DATA_TREE_KEY, JSON.stringify(treeData));
setLocalStorageObject(DATA_TREE_KEY, treeData, TWO_DAYS_MILLISECONDS);
setDataTree(treeData);
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -91,7 +91,7 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => {
...x,
};
});
localStorage.setItem(DATA_STORE_KEY, JSON.stringify(store));
setLocalStorageObject(DATA_STORE_KEY, store, TWO_DAYS_MILLISECONDS);
setDataStore(store);
}
} catch (error) {
Expand Down
26 changes: 26 additions & 0 deletions application/frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
export { groupLinksByType as groupLinksByType, LinksByType, getDocumentDisplayName } from './document';

export const getLocalStorageObject = (key) => {
const itemStr = localStorage.getItem(key);
if (!itemStr) {
return null;
}

const item = JSON.parse(itemStr);
const now = new Date();

if (now.getTime() > item.expiry) {
localStorage.removeItem(key);
return null;
}
return item.value;
};

export const setLocalStorageObject = (key, value, ttl) => {
const now = new Date();

const item = {
value: value,
expiry: now.getTime() + ttl,
};
localStorage.setItem(key, JSON.stringify(item));
};

0 comments on commit b7661f2

Please sign in to comment.