-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into MHV-63837-auto-test-self-entered-records
- Loading branch information
Showing
37 changed files
with
1,838 additions
and
161 deletions.
There are no files selected for viewing
Empty file.
42 changes: 29 additions & 13 deletions
42
src/applications/_mock-form-ae-design-patterns/hooks/useLocalStorage.js
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 |
---|---|---|
@@ -1,20 +1,36 @@ | ||
import React from 'react'; | ||
import { useState, useEffect } from 'react'; | ||
|
||
export const useLocalStorage = (storageKey, fallbackState) => { | ||
const [value, setValue] = React.useState( | ||
JSON.parse(localStorage.getItem(storageKey)) ?? fallbackState, | ||
); | ||
/** | ||
* useLocalStorage is a hook that provides a way to store and retrieve values from localStorage | ||
* @param {string} key - The key to store the value under | ||
* @param {any} defaultValue - The default value to use if the key does not exist | ||
* @returns {array} An array with [value, setValue, clearValue] | ||
*/ | ||
export const useLocalStorage = (key, defaultValue) => { | ||
const [value, setValue] = useState(() => { | ||
let currentValue; | ||
|
||
try { | ||
currentValue = JSON.parse( | ||
localStorage.getItem(key) || String(defaultValue), | ||
); | ||
} catch (error) { | ||
currentValue = defaultValue; | ||
} | ||
|
||
React.useEffect( | ||
return currentValue; | ||
}); | ||
|
||
useEffect( | ||
() => { | ||
if (value === '') { | ||
localStorage.removeItem(storageKey); | ||
return; | ||
} | ||
localStorage.setItem(storageKey, JSON.stringify(value)); | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
}, | ||
[value, storageKey], | ||
[value, key], | ||
); | ||
|
||
return [value, setValue]; | ||
const clearValue = () => { | ||
localStorage.removeItem(key); | ||
}; | ||
|
||
return [value, setValue, clearValue]; | ||
}; |
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.