mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-04 00:18:01 +01:00
clear form after submission and re-opening
This commit is contained in:
parent
0e96a39ecf
commit
74a3c27b06
@ -4,38 +4,70 @@ import CloseIcon from '@site/src/icons/close';
|
|||||||
|
|
||||||
const join = (...cs) => cs.join(' ');
|
const join = (...cs) => cs.join(' ');
|
||||||
|
|
||||||
export const initialData = {
|
const clearedData = {
|
||||||
currentStep: 1,
|
currentStep: 1,
|
||||||
data: {
|
data: {
|
||||||
score: undefined,
|
score: undefined,
|
||||||
comment: undefined,
|
comment: undefined,
|
||||||
customerType: undefined,
|
customerType: undefined,
|
||||||
},
|
},
|
||||||
|
userClosed: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchData = (initialData) => {
|
const localstorageKey = 'user-feedback-v1';
|
||||||
const localstorageKey = 'user-feedback-v1';
|
const populateData = (initialData) => {
|
||||||
|
// if we get seed data, use that. Otherwise, check if the last entry in
|
||||||
|
// localstorage was completed. If not, use that as base.
|
||||||
|
|
||||||
|
const getSeedData = () => {
|
||||||
|
if (initialData) {
|
||||||
|
return initialData;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userFeedbackLog = localStorage.getItem(localstorageKey);
|
||||||
|
|
||||||
|
if (userFeedbackLog) {
|
||||||
|
const mostRecent = Math.max(
|
||||||
|
Object.keys(userFeedbackLog).map(parseInt),
|
||||||
|
);
|
||||||
|
if (!mostRecent.closedOrCompleted) {
|
||||||
|
return mostRecent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
|
const seedData = getSeedData();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
currentStep: 1,
|
currentStep: 1,
|
||||||
...initialData,
|
...seedData,
|
||||||
data: {
|
data: {
|
||||||
score: undefined,
|
score: undefined,
|
||||||
comment: undefined,
|
comment: undefined,
|
||||||
customerType: undefined,
|
customerType: undefined,
|
||||||
...initialData?.data,
|
...seedData?.data,
|
||||||
},
|
},
|
||||||
initialized: Date.now(),
|
initialized: Date.now(),
|
||||||
closedOrCompleted: false,
|
closedOrCompleted: false,
|
||||||
};
|
};
|
||||||
// check localstorage
|
};
|
||||||
// populate if there is
|
|
||||||
|
const storeData = (data) => {
|
||||||
|
const existingData = localStorage.getItem(localstorageKey);
|
||||||
|
localStorage.setItem(localstorageKey, {
|
||||||
|
...existingData,
|
||||||
|
[data.initialized]: data,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const stateReducer = (state, message) => {
|
const stateReducer = (state, message) => {
|
||||||
switch (message.kind) {
|
switch (message.kind) {
|
||||||
case 'clear':
|
case 'close':
|
||||||
return fetchData(seedData);
|
return { ...state, userClosed: true };
|
||||||
|
case 'reset':
|
||||||
|
return { ...populateData(clearedData), userClosed: false };
|
||||||
case 'set score':
|
case 'set score':
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@ -72,12 +104,13 @@ export const FeedbackWrapper = ({ seedData, open }) => {
|
|||||||
const [state, dispatch] = React.useReducer(
|
const [state, dispatch] = React.useReducer(
|
||||||
stateReducer,
|
stateReducer,
|
||||||
seedData,
|
seedData,
|
||||||
fetchData,
|
populateData,
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(state, state.data);
|
console.log(state, state.data);
|
||||||
|
|
||||||
const clear = () => dispatch({ kind: 'clear' });
|
const close = () => dispatch({ kind: 'close' });
|
||||||
|
|
||||||
const stepForward = () => {
|
const stepForward = () => {
|
||||||
console.log('stepping forward!');
|
console.log('stepping forward!');
|
||||||
dispatch({ kind: 'step forward' });
|
dispatch({ kind: 'step forward' });
|
||||||
@ -298,11 +331,11 @@ export const FeedbackWrapper = ({ seedData, open }) => {
|
|||||||
type="radio"
|
type="radio"
|
||||||
value={key}
|
value={key}
|
||||||
defaultChecked={
|
defaultChecked={
|
||||||
key === state.data.customerType
|
customerType === state.data.customerType
|
||||||
}
|
}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const value = e.target.value;
|
const value = e.target.value;
|
||||||
setValue(value);
|
setValue(customerType);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<label
|
<label
|
||||||
@ -341,7 +374,10 @@ export const FeedbackWrapper = ({ seedData, open }) => {
|
|||||||
<button
|
<button
|
||||||
className={styles['button-secondary']}
|
className={styles['button-secondary']}
|
||||||
disabled={hidden}
|
disabled={hidden}
|
||||||
onClick={() => setFeedbackIsOpen(false)}
|
onClick={() => {
|
||||||
|
setFeedbackIsOpen(false);
|
||||||
|
close();
|
||||||
|
}}
|
||||||
autoFocus
|
autoFocus
|
||||||
>
|
>
|
||||||
close
|
close
|
||||||
@ -352,6 +388,8 @@ export const FeedbackWrapper = ({ seedData, open }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles['user-feedback-container']}>
|
<div className={styles['user-feedback-container']}>
|
||||||
|
<p>State.data is {JSON.stringify(state.data)}</p>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
aria-hidden={feedbackIsOpen}
|
aria-hidden={feedbackIsOpen}
|
||||||
className={join(
|
className={join(
|
||||||
@ -362,6 +400,9 @@ export const FeedbackWrapper = ({ seedData, open }) => {
|
|||||||
onClick={() => {
|
onClick={() => {
|
||||||
setFeedbackIsOpen(true);
|
setFeedbackIsOpen(true);
|
||||||
setManuallyOpened(true);
|
setManuallyOpened(true);
|
||||||
|
if (state.userClosed) {
|
||||||
|
dispatch({ kind: 'reset' });
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span>Feedback</span>
|
<span>Feedback</span>
|
||||||
|
Loading…
Reference in New Issue
Block a user