From fc146ea7fc9f705b88bbc75a6cc01b6f2235bfaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20=C3=96brink?= Date: Sun, 14 Feb 2021 23:16:01 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Cache=20prefixas=20med?= =?UTF-8?q?=20personnummer=20(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 🎸 Cache prefixas med personnummer För enklare, selektive wipe --- src/hooks.ts | 13 ++++++++----- src/logout.test.js | 3 ++- src/useCalendar.test.js | 7 ++++--- src/useChildlist.test.js | 7 ++++--- src/useClassmates.test.js | 7 ++++--- src/useMenu.test.js | 7 ++++--- src/useNews.test.js | 7 ++++--- src/useNewsDetails.test.js | 7 ++++--- src/useNotifications.test.js | 7 ++++--- src/useSchedule.test.js | 7 ++++--- src/useUser.test.js | 7 ++++--- 11 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index f75c3edcf..aaa95d506 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -34,15 +34,16 @@ const hook = ( selector: StoreSelector, apiCaller: (api: Api) => ApiCall, ): EntityHookResult => { + const { + api, isLoggedIn, reporter, storage, + } = useApi() + const getState = (): EntityStoreRootState => store.getState() as unknown as EntityStoreRootState const select = (storeState: EntityStoreRootState) => { const stateMap = selector(storeState) || {} const state = stateMap[key] || { status: 'pending', data: defaultValue } return state } - const { - api, isLoggedIn, reporter, storage, - } = useApi() const initialState = select(getState()) const [state, setState] = useState(initialState) const dispatch = useDispatch() @@ -58,11 +59,13 @@ const hook = ( // Only use cache when not in fake mode if (!api.isFake) { + const pnr = api.getPersonalNumber() + // Only get from cache first time if (state.status === 'pending') { - extra.getFromCache = () => storage.getItem(key) + extra.getFromCache = () => storage.getItem(`${pnr}_${key}`) } - extra.saveToCache = (value: string) => storage.setItem(key, value) + extra.saveToCache = (value: string) => storage.setItem(`${pnr}_${key}`, value) } const action = loadAction(entityName, extra) dispatch(action) diff --git a/src/logout.test.js b/src/logout.test.js index 5e5c714b0..649cc0312 100644 --- a/src/logout.test.js +++ b/src/logout.test.js @@ -25,13 +25,14 @@ describe('logout - cleanup', () => { beforeEach(() => { response = [{ id: 1 }] api = init() + api.getPersonalNumber.mockReturnValue('123') api.getChildren.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - children: [{ id: 2 }], + '123_children': [{ id: 2 }], }, 2) }) afterEach(async () => { diff --git a/src/useCalendar.test.js b/src/useCalendar.test.js index 40bbe3c78..ef9a57959 100644 --- a/src/useCalendar.test.js +++ b/src/useCalendar.test.js @@ -26,13 +26,14 @@ describe('useCalendar(child)', () => { beforeEach(() => { response = [{ id: 1 }] api = init() + api.getPersonalNumber.mockReturnValue('123') api.getCalendar.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - calendar_10: [{ id: 2 }], + '123_calendar_10': [{ id: 2 }], }, 2) child = { id: 10 } }) @@ -134,7 +135,7 @@ describe('useCalendar(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.calendar_10).toEqual('[{"id":1}]') + expect(storage.cache['123_calendar_10']).toEqual('[{"id":1}]') }) }) it('does not store in cache if fake', async () => { @@ -148,7 +149,7 @@ describe('useCalendar(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.calendar_10).toEqual('[{"id":2}]') + expect(storage.cache['123_calendar_10']).toEqual('[{"id":2}]') }) }) it('retries if api fails', async () => { diff --git a/src/useChildlist.test.js b/src/useChildlist.test.js index 2c8a73469..1cecbb15c 100644 --- a/src/useChildlist.test.js +++ b/src/useChildlist.test.js @@ -25,13 +25,14 @@ describe('useChildList()', () => { beforeEach(() => { response = [{ id: 1 }] api = init() + api.getPersonalNumber.mockReturnValue('123') api.getChildren.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - children: [{ id: 2 }], + '123_children': [{ id: 2 }], }, 2) }) afterEach(async () => { @@ -120,7 +121,7 @@ describe('useChildList()', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.children).toEqual('[{"id":1}]') + expect(storage.cache['123_children']).toEqual('[{"id":1}]') }) }) it('does not store in cache if fake', async () => { @@ -134,7 +135,7 @@ describe('useChildList()', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.children).toEqual('[{"id":2}]') + expect(storage.cache['123_children']).toEqual('[{"id":2}]') }) }) it('retries if api fails', async () => { diff --git a/src/useClassmates.test.js b/src/useClassmates.test.js index b5601f3af..fb95930fb 100644 --- a/src/useClassmates.test.js +++ b/src/useClassmates.test.js @@ -26,13 +26,14 @@ describe('useClassmates(child)', () => { beforeEach(() => { response = [{ id: 1 }] api = init() + api.getPersonalNumber.mockReturnValue('123') api.getClassmates.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - classmates_10: [{ id: 2 }], + '123_classmates_10': [{ id: 2 }], }, 2) child = { id: 10 } }) @@ -122,7 +123,7 @@ describe('useClassmates(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.classmates_10).toEqual('[{"id":1}]') + expect(storage.cache['123_classmates_10']).toEqual('[{"id":1}]') }) }) it('does not store in cache if fake', async () => { @@ -136,7 +137,7 @@ describe('useClassmates(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.classmates_10).toEqual('[{"id":2}]') + expect(storage.cache['123_classmates_10']).toEqual('[{"id":2}]') }) }) it('retries if api fails', async () => { diff --git a/src/useMenu.test.js b/src/useMenu.test.js index f52fb09b4..e09c4c5db 100644 --- a/src/useMenu.test.js +++ b/src/useMenu.test.js @@ -26,13 +26,14 @@ describe('useMenu(child)', () => { beforeEach(() => { response = [{ id: 1 }] api = init() + api.getPersonalNumber.mockReturnValue('123') api.getMenu.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - menu_10: [{ id: 2 }], + '123_menu_10': [{ id: 2 }], }, 2) child = { id: 10 } }) @@ -122,7 +123,7 @@ describe('useMenu(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.menu_10).toEqual('[{"id":1}]') + expect(storage.cache['123_menu_10']).toEqual('[{"id":1}]') }) }) it('does not store in cache if fake', async () => { @@ -136,7 +137,7 @@ describe('useMenu(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.menu_10).toEqual('[{"id":2}]') + expect(storage.cache['123_menu_10']).toEqual('[{"id":2}]') }) }) it('retries if api fails', async () => { diff --git a/src/useNews.test.js b/src/useNews.test.js index bf551e6a1..35c9d590d 100644 --- a/src/useNews.test.js +++ b/src/useNews.test.js @@ -26,13 +26,14 @@ describe('useNews(child)', () => { beforeEach(() => { response = [{ id: 1 }] api = init() + api.getPersonalNumber.mockReturnValue('123') api.getNews.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - news_10: [{ id: 2 }], + '123_news_10': [{ id: 2 }], }, 2) child = { id: 10 } }) @@ -122,7 +123,7 @@ describe('useNews(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.news_10).toEqual('[{"id":1}]') + expect(storage.cache['123_news_10']).toEqual('[{"id":1}]') }) }) it('does not store in cache if fake', async () => { @@ -136,7 +137,7 @@ describe('useNews(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.news_10).toEqual('[{"id":2}]') + expect(storage.cache['123_news_10']).toEqual('[{"id":2}]') }) }) it('retries if api fails', async () => { diff --git a/src/useNewsDetails.test.js b/src/useNewsDetails.test.js index 1e337940d..e30c17e9a 100644 --- a/src/useNewsDetails.test.js +++ b/src/useNewsDetails.test.js @@ -29,13 +29,14 @@ describe('useNewsDetails(child, newsItem)', () => { cached = { id: '1337', modified: 'yesterday', body: 'rich and old' } response = { id: '1337', modified: 'now', body: 'rich and new' } api = init() + api.getPersonalNumber.mockReturnValue('123') api.getNewsDetails.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - news_details_1337: { ...cached }, + '123_news_details_1337': { ...cached }, }, 2) child = { id: 10 } newsItem = { id: '1337', modified: 'now', body: 'simple' } @@ -126,7 +127,7 @@ describe('useNewsDetails(child, newsItem)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.news_details_1337).toEqual(JSON.stringify(response)) + expect(storage.cache['123_news_details_1337']).toEqual(JSON.stringify(response)) }) }) it('does not store in cache if fake', async () => { @@ -140,7 +141,7 @@ describe('useNewsDetails(child, newsItem)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.news_details_1337).toEqual(JSON.stringify(cached)) + expect(storage.cache['123_news_details_1337']).toEqual(JSON.stringify(cached)) }) }) it('retries if api fails', async () => { diff --git a/src/useNotifications.test.js b/src/useNotifications.test.js index aa9f28475..cc2c0f204 100644 --- a/src/useNotifications.test.js +++ b/src/useNotifications.test.js @@ -26,13 +26,14 @@ describe('useNotifications(child)', () => { beforeEach(() => { response = [{ id: 1 }] api = init() + api.getPersonalNumber.mockReturnValue('123') api.getNotifications.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - notifications_10: [{ id: 2 }], + '123_notifications_10': [{ id: 2 }], }, 2) child = { id: 10 } }) @@ -122,7 +123,7 @@ describe('useNotifications(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.notifications_10).toEqual('[{"id":1}]') + expect(storage.cache['123_notifications_10']).toEqual('[{"id":1}]') }) }) it('does not store in cache if fake', async () => { @@ -136,7 +137,7 @@ describe('useNotifications(child)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.notifications_10).toEqual('[{"id":2}]') + expect(storage.cache['123_notifications_10']).toEqual('[{"id":2}]') }) }) it('retries if api fails', async () => { diff --git a/src/useSchedule.test.js b/src/useSchedule.test.js index d44f3fa83..610e62596 100644 --- a/src/useSchedule.test.js +++ b/src/useSchedule.test.js @@ -28,13 +28,14 @@ describe('useSchedule(child, from, to)', () => { beforeEach(() => { response = [{ id: 1 }] api = init() + api.getPersonalNumber.mockReturnValue('123') api.getSchedule.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - 'schedule_10_2021-01-01_2021-01-08': [{ id: 2 }], + '123_schedule_10_2021-01-01_2021-01-08': [{ id: 2 }], }, 2) child = { id: 10 } from = '2021-01-01' @@ -126,7 +127,7 @@ describe('useSchedule(child, from, to)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache['schedule_10_2021-01-01_2021-01-08']).toEqual('[{"id":1}]') + expect(storage.cache['123_schedule_10_2021-01-01_2021-01-08']).toEqual('[{"id":1}]') }) }) it('does not store in cache if fake', async () => { @@ -140,7 +141,7 @@ describe('useSchedule(child, from, to)', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache['schedule_10_2021-01-01_2021-01-08']).toEqual('[{"id":2}]') + expect(storage.cache['123_schedule_10_2021-01-01_2021-01-08']).toEqual('[{"id":2}]') }) }) it('retries if api fails', async () => { diff --git a/src/useUser.test.js b/src/useUser.test.js index 19e9ade53..26db7218e 100644 --- a/src/useUser.test.js +++ b/src/useUser.test.js @@ -25,13 +25,14 @@ describe('useUser()', () => { beforeEach(() => { response = { id: 1 } api = init() + api.getPersonalNumber.mockReturnValue('123') api.getUser.mockImplementation(() => ( new Promise((res) => { setTimeout(() => res(response), 50) }) )) storage = createStorage({ - user: { id: 2 }, + '123_user': { id: 2 }, }, 2) }) afterEach(async () => { @@ -120,7 +121,7 @@ describe('useUser()', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.user).toEqual('{"id":1}') + expect(storage.cache['123_user']).toEqual('{"id":1}') }) }) it('does not store in cache if fake', async () => { @@ -134,7 +135,7 @@ describe('useUser()', () => { await waitForNextUpdate() await pause(20) - expect(storage.cache.user).toEqual('{"id":2}') + expect(storage.cache['123_user']).toEqual('{"id":2}') }) }) it('retries if api fails', async () => {