-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add debounced redux subscriber to save redux state to localStorage, &…
… load on initialise. Remove use of Set types from redux state
- Loading branch information
1 parent
9be4f47
commit 3caf337
Showing
13 changed files
with
181 additions
and
99 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
120 changes: 57 additions & 63 deletions
120
plugin-hrm-form/src/___tests__/states/contacts/existingContacts.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 @@ | ||
/** | ||
* Copyright (C) 2021-2023 Technology Matters | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
import { Manager } from '@twilio/flex-ui'; | ||
import _ from 'lodash'; | ||
|
||
import { RootState } from '.'; | ||
import { namespace } from './storeNamespaces'; | ||
import { getAseloFeatureFlags } from '../hrmConfig'; | ||
|
||
// Quick & dirty module to persist redux state to localStorage via subscriptions since we can't add middleware like redux-persist to do it for us | ||
export const activateStatePersistence = () => { | ||
if (getAseloFeatureFlags().enable_local_redux_persist) { | ||
const debouncedWrite = _.debounce(() => { | ||
// Exclude configuration from persisted state, since it contains non serializable elements, and is read only in the client anyway | ||
const { | ||
[namespace]: { configuration, ...persistableState }, | ||
} = Manager.getInstance().store.getState() as RootState; | ||
localStorage.setItem('redux-state/plugin-hrm-form', JSON.stringify(persistableState)); | ||
}, 1000); | ||
Manager.getInstance().store.subscribe(debouncedWrite); | ||
} | ||
}; | ||
|
||
export const readPersistedState = (): RootState[typeof namespace] | null => { | ||
if (getAseloFeatureFlags().enable_local_redux_persist) { | ||
const persistedState = localStorage.getItem('redux-state/plugin-hrm-form'); | ||
if (persistedState) { | ||
return JSON.parse(persistedState); | ||
} | ||
} | ||
return undefined; | ||
}; |
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,36 @@ | ||
/** | ||
* Copyright (C) 2021-2023 Technology Matters | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
// The JS set doesn't serialize to JSON, so we need to use a plain object instead if we want to keep it in redux state, now we store it in localStorage | ||
|
||
export type SerializableSet = Record<string, true>; | ||
|
||
export const has = (set: SerializableSet, key: string): boolean => Boolean(set[key]); | ||
|
||
export const add = (set: SerializableSet, key: string): SerializableSet => { | ||
set[key] = true; | ||
return set; | ||
}; | ||
|
||
export const remove = (set: SerializableSet, key: string): boolean => { | ||
if (!has(set, key)) { | ||
return false; | ||
} | ||
delete set[key]; | ||
return true; | ||
}; | ||
|
||
export const size = (set: SerializableSet): number => Object.keys(set).length; |
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