1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-17 01:17:29 +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,
};
export const WithLocalStorage = Template.bind({});
WithLocalStorage.args = {
open: true,
};
export const Closed = Template.bind({});

View File

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