| Lines of Code | 53 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | import { debounce } from './../../util/throttle'; |
||
| 2 | |||
| 3 | export class LocalStorageManager { |
||
| 4 | |||
| 5 | setStateItem({ stateKey, property, value }) { |
||
| 6 | const json = JSON.stringify(value); |
||
| 7 | |||
| 8 | if (!window.localStorage) { |
||
| 9 | return value; |
||
| 10 | } |
||
| 11 | |||
| 12 | window.localStorage.setItem( |
||
| 13 | this.getKey({ stateKey, property }), json |
||
| 14 | ); |
||
|
|
|||
| 15 | } |
||
| 16 | |||
| 17 | debouncedSetStateItem() { |
||
| 18 | return debounce(this.setStateItem.bind(this), 500, false); |
||
| 19 | } |
||
| 20 | |||
| 21 | getStateItem({ stateKey, property, value, shouldSave = true }) { |
||
| 22 | |||
| 23 | if (!window.localStorage) { |
||
| 24 | return value; |
||
| 25 | } |
||
| 26 | |||
| 27 | const item = window.localStorage.getItem( |
||
| 28 | this.getKey({ stateKey, property }) |
||
| 29 | ); |
||
| 30 | |||
| 31 | if (item) { |
||
| 32 | return JSON.parse(item); |
||
| 33 | } |
||
| 34 | |||
| 35 | if (value && shouldSave) { |
||
| 36 | this.setStateItem({ stateKey, property, value }); |
||
| 37 | } |
||
| 38 | |||
| 39 | return value; |
||
| 40 | } |
||
| 41 | |||
| 42 | getKey({ stateKey, property }) { |
||
| 43 | |||
| 44 | if (!stateKey || !property) { |
||
| 45 | throw new Error('stateKey and property are required params'); |
||
| 46 | } |
||
| 47 | |||
| 48 | return `react-grid-${stateKey}-${property}`; |
||
| 49 | } |
||
| 50 | |||
| 51 | } |
||
| 52 | |||
| 53 | export default new LocalStorageManager(); |
||
| 54 |