1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

move subcomponent into wrapper

This commit is contained in:
Thomas Heartman 2022-02-27 14:59:33 +01:00
parent 35a939b895
commit 5340ca28fc
2 changed files with 49 additions and 116 deletions

View File

@ -1,21 +1,21 @@
import React from 'react'; import React from 'react';
import Component, { initialData, FeedbackWrapper } from './index'; import { initialData, FeedbackWrapper } from './index';
export default { export default {
title: 'User feedback component', title: 'User feedback component',
component: Component, component: FeedbackWrapper,
}; };
const Template = (args) => <FeedbackWrapper {...args} />; const Template = (args) => <FeedbackWrapper {...args} />;
export const FullComponent = Template.bind({}); export const FullComponent = Template.bind({});
FullComponent.args = { FullComponent.args = {
initialData, // initialData,
}; };
export const Step2 = Template.bind({}); export const Step2 = Template.bind({});
Step2.args = { Step2.args = {
initialData: { seedData: {
...initialData, ...initialData,
currentStep: 2, currentStep: 2,
}, },

View File

@ -17,6 +17,9 @@ const fetchData = (initialData) => {
const localstorageKey = 'user-feedback'; const localstorageKey = 'user-feedback';
return { return {
data: {
score: undefined,
},
...initialData, ...initialData,
initialized: Date.now(), initialized: Date.now(),
closedOrCompleted: false, closedOrCompleted: false,
@ -25,44 +28,45 @@ const fetchData = (initialData) => {
// populate if there is // populate if there is
}; };
export const FeedbackWrapper = ({ initialData }) => { const stateReducer = (state, message) => {
switch (message.kind) {
case 'clear':
return fetchData(seedData);
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 },
};
case 'step forward':
return {
...state,
currentStep: min(state.currentStep + 1, 3),
};
case 'step back':
return {
...state,
currentStep: max(state.currentStep - 1, 1),
};
}
return state;
};
export const FeedbackWrapper = ({ seedData }) => {
const [feedbackIsOpen, setFeedbackIsOpen] = React.useState(false); 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 },
};
case 'step forward':
return {
...state,
currentStep: min(state.currentStep + 1, 3),
};
case 'step back':
return {
...state,
currentStep: max(state.currentStep - 1, 1),
};
}
return state;
};
const [state, dispatch] = React.useReducer( const [state, dispatch] = React.useReducer(
stateReducer, stateReducer,
initialData, seedData,
fetchData, fetchData,
); );
@ -109,7 +113,7 @@ export const FeedbackWrapper = ({ initialData }) => {
onChange={(e) => { onChange={(e) => {
const value = parseInt(e.target.value); const value = parseInt(e.target.value);
console.log('the value is', value); console.log('the value is', value);
setScore(value); /* setScore(value); */
}} }}
/> />
<label <label
@ -134,94 +138,23 @@ export const FeedbackWrapper = ({ initialData }) => {
); );
}; };
return ( console.log('bluh');
<Component
CurrentStep={
<Step1
score={state.data.score}
setScore={setScore}
stepForward={stepForward}
/>
}
/>
);
};
const OpenFeedbackButton = ({ openFeedback }) => {
return <button onClick={openFeedback}>Feedback</button>;
};
export const Step1 = ({ score, stepForward, setScore }) => {
return (
<form>
<p>
<span className="visually-hidden">
On a scale from 1 to 5 where 1 is very unsatisfied and 5 is
very satisfied,
</span>{' '}
How would you rate your overall satisfaction with the Unleash
documentation?
</p>
<div className={styles['satisfaction-input-container']}>
<span aria-hidden="true">Very unsatisfied</span>
{[1, 2, 3, 4, 5].map((n) => (
<span key={`input-group-${n}`}>
<input
className={join(
'visually-hidden',
styles['user-satisfaction-score-input'],
)}
required
id={`user-satisfaction-score-${n}`}
name="satisfaction-level"
type="radio"
value={n}
defaultChecked={n === score}
onChange={(e) => {
const value = parseInt(e.target.value);
console.log('the value is', value);
setScore(value);
}}
/>
<label
className={styles['user-satisfaction-score-label']}
htmlFor={`user-satisfaction-score-${n}`}
>
{n}
</label>
</span>
))}
<span aria-hidden="true">Very satisfied</span>
</div>
<div className={styles['button-container']}>
<button className={styles['button-secondary']}>Skip</button>
<button type="submit" onClick={stepForward}>
Next
</button>
</div>
</form>
);
};
const Component = ({ closeFeedback = () => undefined, CurrentStep }) => {
return ( return (
<article className={styles['user-feedback']}> <article className={styles['user-feedback']}>
<div className={styles['close-button-row']}> <div className={styles['close-button-row']}>
<button <button className={styles['close-button']}>
className={styles['close-button']}
onClick={closeFeedback}
>
<span className="visually-hidden"> <span className="visually-hidden">
close feedback popup close feedback popup
</span> </span>
<CloseIcon /> <CloseIcon />
</button> </button>
</div> </div>
<Step1 />
{CurrentStep}
</article> </article>
); );
}; };
export default Component; const OpenFeedbackButton = ({ openFeedback }) => {
return <button onClick={openFeedback}>Feedback</button>;
};