feat: 🎸 Cache prefixas med personnummer (#8)
* feat: 🎸 Cache prefixas med personnummer
För enklare, selektive wipe
This commit is contained in:
parent
01081609c6
commit
fc146ea7fc
13
src/hooks.ts
13
src/hooks.ts
|
@ -34,15 +34,16 @@ const hook = <T>(
|
|||
selector: StoreSelector<T>,
|
||||
apiCaller: (api: Api) => ApiCall<T>,
|
||||
): EntityHookResult<T> => {
|
||||
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 = <T>(
|
|||
|
||||
// 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<T>(entityName, extra)
|
||||
dispatch(action)
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
Loading…
Reference in New Issue