-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
197 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const { useState, useEffect } = React | ||
|
||
function Persistence() { | ||
const { | ||
currentToken, | ||
hasToken, | ||
useUpdateToken, | ||
changePageNumber, | ||
changePageSize, | ||
pageNumber, | ||
pageSize, | ||
} = useTokenPagination({ | ||
defaultPageNumber: 1, | ||
defaultPageSize: 5, | ||
persister: useTokenPagination.localPersister('persistence'), | ||
}) | ||
const [data, setData] = useState() | ||
|
||
useEffect(() => { | ||
async function fetchData() { | ||
const params = new URLSearchParams({ pageSize }) | ||
|
||
if (currentToken) { | ||
params.append('pageToken', currentToken) | ||
} | ||
|
||
const res = await fetch(`/api?${params.toString()}`) | ||
const data = await res.json() | ||
|
||
setData(data) | ||
} | ||
|
||
fetchData() | ||
}, [pageSize, currentToken]) | ||
|
||
useUpdateToken(data?.nextPage) | ||
|
||
function previousPage() { | ||
changePageNumber(n => n - 1) | ||
} | ||
function nextPage() { | ||
changePageNumber(n => n + 1) | ||
} | ||
function handleChangePageSize(e) { | ||
changePageSize(+e.target.value) | ||
} | ||
|
||
return ( | ||
<Output | ||
data={data} | ||
pageNumber={pageNumber} | ||
pageSize={pageSize} | ||
changePageSize={handleChangePageSize} | ||
previousPage={previousPage} | ||
nextPage={hasToken(pageNumber + 1) ? nextPage : undefined} | ||
/> | ||
) | ||
} | ||
|
||
Persistence.propTypes = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ export default { | |
dir: 'cjs', | ||
format: 'cjs', | ||
exports: 'default', | ||
esModule: false, | ||
}, | ||
{ | ||
dir: 'es', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export const NULL_PERSISTER = { | ||
hydrate() { | ||
return {} | ||
}, | ||
persist() {}, | ||
} | ||
|
||
function StoragePersister(key, storage) { | ||
return { | ||
hydrate() { | ||
try { | ||
return JSON.parse(storage.getItem(key) || '{}') | ||
} catch (err) { | ||
return {} | ||
} | ||
}, | ||
persist(value) { | ||
storage.setItem(key, JSON.stringify({ ...this.hydrate(), ...value })) | ||
}, | ||
} | ||
} | ||
|
||
export const localPersister = key => | ||
new StoragePersister(key, window.localStorage) | ||
export const sessionPersister = key => | ||
new StoragePersister(key, window.sessionStorage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { localPersister, sessionPersister } from './persisters' | ||
|
||
describe('persisters', () => { | ||
describe('local', persisterTests(localPersister, 'localStorage')) | ||
|
||
describe('session', persisterTests(sessionPersister, 'sessionStorage')) | ||
}) | ||
|
||
function persisterTests(persisterFactory, storageName) { | ||
return function () { | ||
let persister, storage | ||
|
||
beforeEach(() => { | ||
Object.defineProperty(window, storageName, { | ||
value: { setItem: jest.fn(), getItem: jest.fn() }, | ||
}) | ||
storage = window[storageName] | ||
persister = persisterFactory('key') | ||
}) | ||
|
||
it('persists', () => { | ||
persister.persist({ some: 'value' }) | ||
|
||
expect(storage.setItem).toHaveBeenCalledWith( | ||
'key', | ||
JSON.stringify({ some: 'value' }) | ||
) | ||
}) | ||
|
||
it('hydrates', () => { | ||
const stored = { some: 'value' } | ||
storage.getItem.mockReturnValue(JSON.stringify(stored)) | ||
|
||
expect(persister.hydrate()).toEqual(stored) | ||
}) | ||
|
||
it('merges with existing value when persisting', () => { | ||
const stored = { some: 'value' } | ||
storage.getItem.mockReturnValue(JSON.stringify(stored)) | ||
|
||
persister.persist({ another: 'value' }) | ||
|
||
expect(storage.setItem).toHaveBeenCalledWith( | ||
'key', | ||
JSON.stringify({ some: 'value', another: 'value' }) | ||
) | ||
}) | ||
|
||
it('handles errors', () => { | ||
storage.getItem.mockReturnValue('invalid json') | ||
expect(persister.hydrate()).toEqual({}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters