-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1188 from rdmorganiser/interview-import-dataset
feat(interview): Reuse datasets and values [5]
- Loading branch information
Showing
48 changed files
with
1,476 additions
and
299 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,46 @@ | ||
import { useState } from 'react' | ||
import { isEmpty, isPlainObject, omit as lodashOmit } from 'lodash' | ||
|
||
import { checkStoreId } from '../utils/store' | ||
|
||
const useLsState = (path, initialValue, omit = []) => { | ||
checkStoreId() | ||
|
||
const getLs = (path) => { | ||
const data = JSON.parse(localStorage.getItem(path)) | ||
return isPlainObject(data) ? lodashOmit(data, omit) : data | ||
} | ||
|
||
const setLs = (path, value) => { | ||
const data = isPlainObject(value) ? lodashOmit(value, omit) : value | ||
localStorage.setItem(path, JSON.stringify(data)) | ||
} | ||
|
||
const getInitialState = () => { | ||
// get the value from the local storage | ||
const lsValue = getLs(path) | ||
|
||
// return the state with the value from the local storage or the provided initialValue | ||
if (isPlainObject(lsValue)) { | ||
return { ...initialValue, ...lsValue } | ||
} else if (isEmpty(lsValue)) { | ||
return initialValue | ||
} else { | ||
return lsValue | ||
} | ||
} | ||
|
||
// setup the state | ||
const [value, setValue] = useState(getInitialState()) | ||
|
||
return [ | ||
value, | ||
(value) => { | ||
setLs(path, value) | ||
setValue(value) | ||
}, | ||
() => setValue(getInitialState()) | ||
] | ||
} | ||
|
||
export default useLsState |
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
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
Oops, something went wrong.