1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

feat: start adding state logic to component.

This commit is contained in:
Thomas Heartman 2022-02-26 21:38:07 +01:00
parent 6e2072bca4
commit 211b18c4a3

View File

@ -1,33 +1,113 @@
import React from 'react'; import React from 'react';
import styles from './styles.module.css'; import styles from './styles.module.css';
import CloseIcon from '@site/src/icons/close' import CloseIcon from '@site/src/icons/close';
const join = (...cs) => cs.join(" ") const join = (...cs) => cs.join(' ');
const Component = ({ text }) => ( const Step1 = () => <></>;
<article className={styles["user-feedback"]}> const Step2 = () => <></>;
<div className={ styles["close-button-row" ]}> const Step3 = () => <></>;
<button className={styles["close-button"]} onClick={() => console.log("add some close button action")}>
<span className="visually-hidden">close feedback popup</span> const initialData = () => ({
initialized: Date.now(),
closedOrCompleted: false,
data: {
score: undefined,
comment: undefined,
customerType: undefined,
},
});
const fetchData = (initialData) => {
const localstorageKey = 'user-feedback';
return initialData;
// check localstorage
// populate if there is
};
const FeedbackWrapper = () => {
const [feedbackIsOpen, setFeedbackIsOpen] = React.useState(false);
const stateReducer = (state, message) => {
switch (message.kind) {
case 'clear':
return initialData();
case 'set score':
return {
...state,
data: { ...state.data, score: message.data },
};
case 'set comment':
return {
...state,
data: { ...state.data, comment: message.data },
};
case 'set customer type':
return {
...state,
data: { ...state.data, customerType: message.data },
};
}
return state;
};
const [state, dispatch] = React.useReducer(
stateReducer,
initialData(),
fetchData,
);
const clear = () => dispatch({ kind: 'clear' });
const setScore = (score) => dispatch({ kind: 'set score', data: score });
const setComment = (comment) =>
dispatch({ kind: 'set comment', data: comment });
const setCustomerType = (customerType) =>
dispatch({ kind: 'set customer type', data: customerType });
return feedbackIsOpen ? (
<Component closeFeedback={() => setFeedbackIsOpen(false)} />
) : (
<OpenFeedbackButton openFeedback={() => setFeedbackIsOpen(true)} />
);
};
const OpenFeedbackButton = ({ openFeedback }) => {
return <button onClick={openFeedback}>Feedback</button>;
};
const Component = ({ closeFeedback = () => undefined }) => {
return (
<article className={styles['user-feedback']}>
<div className={styles['close-button-row']}>
<button
className={styles['close-button']}
onClick={closeFeedback}
>
<span className="visually-hidden">
close feedback popup
</span>
<CloseIcon /> <CloseIcon />
</button> </button>
</div> </div>
<form> <form>
<p> <p>
<span className="visually-hidden"> <span className="visually-hidden">
On a scale from 1 to 5 where 1 is very unsatisfied and 5 is On a scale from 1 to 5 where 1 is very unsatisfied and 5
very satisfied, is very satisfied,
</span>{' '} </span>{' '}
How would you rate your overall satisfaction with the Unleash How would you rate your overall satisfaction with the
documentation? Unleash documentation?
</p> </p>
<div className={styles["satisfaction-input-container"]}> <div className={styles['satisfaction-input-container']}>
<span aria-hidden="true">Very unsatisfied</span> <span aria-hidden="true">Very unsatisfied</span>
{[1, 2, 3, 4, 5].map((n) => ( {[1, 2, 3, 4, 5].map((n) => (
<span key={`input-group-${n}`}> <span key={`input-group-${n}`}>
<input <input
className={join("visually-hidden", styles["user-satisfaction-score-input"]) } className={join(
'visually-hidden',
styles['user-satisfaction-score-input'],
)}
required required
id={`user-satisfaction-score-${n}`} id={`user-satisfaction-score-${n}`}
name="satisfaction-level" name="satisfaction-level"
@ -35,7 +115,9 @@ const Component = ({ text }) => (
value={n} value={n}
/> />
<label <label
className={styles["user-satisfaction-score-label"]} className={
styles['user-satisfaction-score-label']
}
htmlFor={`user-satisfaction-score-${n}`} htmlFor={`user-satisfaction-score-${n}`}
> >
{n} {n}
@ -44,12 +126,13 @@ const Component = ({ text }) => (
))} ))}
<span aria-hidden="true">Very satisfied</span> <span aria-hidden="true">Very satisfied</span>
</div> </div>
<div className={styles["button-container"]}> <div className={styles['button-container']}>
<button className={styles["button-secondary"]}>Skip</button> <button className={styles['button-secondary']}>Skip</button>
<button type="submit">Next</button> <button type="submit">Next</button>
</div> </div>
</form> </form>
</article> </article>
); );
};
export default Component; export default Component;