1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-22 01:16:07 +02:00

feat: implement saving and loading from localStorage

This commit is contained in:
Thomas Heartman 2022-02-28 23:41:42 +01:00
parent f9561c3661
commit 98298dc42b
2 changed files with 28 additions and 13 deletions

View File

@ -37,4 +37,9 @@ Step4.args = {
open: true, open: true,
}; };
export const WithLocalStorage = Template.bind({});
WithLocalStorage.args = {
open: true,
};
export const Closed = Template.bind({}); export const Closed = Template.bind({});

View File

@ -24,14 +24,12 @@ const populateData = (initialData) => {
return initialData; return initialData;
} }
const userFeedbackLog = localStorage.getItem(localstorageKey); const userFeedbackLog = getUserDataRecord();
if (userFeedbackLog) { if (userFeedbackLog) {
const mostRecent = Math.max( const mostRecent = Math.max(...Object.keys(userFeedbackLog));
Object.keys(userFeedbackLog).map(parseInt),
);
if (!mostRecent.closedOrCompleted) { if (!mostRecent.closedOrCompleted) {
return mostRecent; return userFeedbackLog[mostRecent];
} }
} }
@ -50,16 +48,23 @@ const populateData = (initialData) => {
...seedData?.data, ...seedData?.data,
}, },
initialized: Date.now(), initialized: Date.now(),
closedOrCompleted: false, userClosed: false,
}; };
}; };
const getUserDataRecord = () =>
JSON.parse(localStorage.getItem(localstorageKey));
const storeData = (data) => { const storeData = (data) => {
const existingData = localStorage.getItem(localstorageKey); const existingData = getUserDataRecord();
localStorage.setItem(localstorageKey, { console.log('this is the existing data:', existingData);
...existingData, localStorage.setItem(
[data.initialized]: data, localstorageKey,
}); JSON.stringify({
...existingData,
[data.initialized]: data,
}),
);
}; };
const stateReducer = (state, message) => { const stateReducer = (state, message) => {
@ -110,12 +115,17 @@ export const FeedbackWrapper = ({ seedData, open }) => {
console.log(state, state.data); console.log(state, state.data);
const close = () => dispatch({ kind: 'close' }); const close = () => dispatch({ kind: 'close' });
if (feedbackIsOpen) {
storeData(state);
}
const stepForward = () => { const stepForward = () => {
console.log('stepping forward!');
dispatch({ kind: 'step forward' }); dispatch({ kind: 'step forward' });
console.log(state);
};
const stepBack = () => {
dispatch({ kind: 'step back' });
}; };
const stepBack = () => dispatch({ kind: 'step back' });
const setScore = (score) => dispatch({ kind: 'set score', data: score }); const setScore = (score) => dispatch({ kind: 'set score', data: score });
const setComment = (comment) => const setComment = (comment) =>
dispatch({ kind: 'set comment', data: comment }); dispatch({ kind: 'set comment', data: comment });