mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-09 00:18:00 +01:00
feat: start splitting component up into multiple pieces.
This commit is contained in:
parent
211b18c4a3
commit
35a939b895
@ -1,16 +1,25 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Component from './index';
|
import Component, { initialData, FeedbackWrapper } from './index';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
title: 'User feedback component',
|
title: 'User feedback component',
|
||||||
component: Component,
|
component: Component,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Template = (args) => <Component {...args} />;
|
const Template = (args) => <FeedbackWrapper {...args} />;
|
||||||
|
|
||||||
export const A = Template.bind({});
|
export const FullComponent = Template.bind({});
|
||||||
A.args = {
|
FullComponent.args = {
|
||||||
x: true,
|
initialData,
|
||||||
y: 45,
|
|
||||||
text: 'blah blah blah',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const Step2 = Template.bind({});
|
||||||
|
Step2.args = {
|
||||||
|
initialData: {
|
||||||
|
...initialData,
|
||||||
|
currentStep: 2,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Step3 = Template.bind({});
|
||||||
|
export const Closed = Template.bind({});
|
||||||
|
@ -4,29 +4,28 @@ import CloseIcon from '@site/src/icons/close';
|
|||||||
|
|
||||||
const join = (...cs) => cs.join(' ');
|
const join = (...cs) => cs.join(' ');
|
||||||
|
|
||||||
const Step1 = () => <></>;
|
export const initialData = {
|
||||||
const Step2 = () => <></>;
|
currentStep: 1,
|
||||||
const Step3 = () => <></>;
|
|
||||||
|
|
||||||
const initialData = () => ({
|
|
||||||
initialized: Date.now(),
|
|
||||||
closedOrCompleted: false,
|
|
||||||
data: {
|
data: {
|
||||||
score: undefined,
|
score: undefined,
|
||||||
comment: undefined,
|
comment: undefined,
|
||||||
customerType: undefined,
|
customerType: undefined,
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
const fetchData = (initialData) => {
|
const fetchData = (initialData) => {
|
||||||
const localstorageKey = 'user-feedback';
|
const localstorageKey = 'user-feedback';
|
||||||
|
|
||||||
return initialData;
|
return {
|
||||||
|
...initialData,
|
||||||
|
initialized: Date.now(),
|
||||||
|
closedOrCompleted: false,
|
||||||
|
};
|
||||||
// check localstorage
|
// check localstorage
|
||||||
// populate if there is
|
// populate if there is
|
||||||
};
|
};
|
||||||
|
|
||||||
const FeedbackWrapper = () => {
|
export const FeedbackWrapper = ({ initialData }) => {
|
||||||
const [feedbackIsOpen, setFeedbackIsOpen] = React.useState(false);
|
const [feedbackIsOpen, setFeedbackIsOpen] = React.useState(false);
|
||||||
const stateReducer = (state, message) => {
|
const stateReducer = (state, message) => {
|
||||||
switch (message.kind) {
|
switch (message.kind) {
|
||||||
@ -47,48 +46,41 @@ const FeedbackWrapper = () => {
|
|||||||
...state,
|
...state,
|
||||||
data: { ...state.data, customerType: message.data },
|
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;
|
return state;
|
||||||
};
|
};
|
||||||
|
|
||||||
const [state, dispatch] = React.useReducer(
|
const [state, dispatch] = React.useReducer(
|
||||||
stateReducer,
|
stateReducer,
|
||||||
initialData(),
|
initialData,
|
||||||
fetchData,
|
fetchData,
|
||||||
);
|
);
|
||||||
|
|
||||||
const clear = () => dispatch({ kind: 'clear' });
|
const clear = () => dispatch({ kind: 'clear' });
|
||||||
|
const stepForward = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
console.log('stepping forward!');
|
||||||
|
dispatch({ kind: 'step forward' });
|
||||||
|
};
|
||||||
|
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 });
|
||||||
const setCustomerType = (customerType) =>
|
const setCustomerType = (customerType) =>
|
||||||
dispatch({ kind: 'set customer type', data: customerType });
|
dispatch({ kind: 'set customer type', data: customerType });
|
||||||
|
|
||||||
return feedbackIsOpen ? (
|
const Step1 = () => {
|
||||||
<Component closeFeedback={() => setFeedbackIsOpen(false)} />
|
|
||||||
) : (
|
|
||||||
<OpenFeedbackButton openFeedback={() => setFeedbackIsOpen(true)} />
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const OpenFeedbackButton = ({ openFeedback }) => {
|
|
||||||
return <button onClick={openFeedback}>Feedback</button>;
|
|
||||||
};
|
|
||||||
|
|
||||||
const Component = ({ closeFeedback = () => undefined }) => {
|
|
||||||
return (
|
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 />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<form>
|
<form>
|
||||||
<p>
|
<p>
|
||||||
<span className="visually-hidden">
|
<span className="visually-hidden">
|
||||||
@ -113,6 +105,12 @@ const Component = ({ closeFeedback = () => undefined }) => {
|
|||||||
name="satisfaction-level"
|
name="satisfaction-level"
|
||||||
type="radio"
|
type="radio"
|
||||||
value={n}
|
value={n}
|
||||||
|
defaultChecked={n === state.data.score}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = parseInt(e.target.value);
|
||||||
|
console.log('the value is', value);
|
||||||
|
setScore(value);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<label
|
<label
|
||||||
className={
|
className={
|
||||||
@ -128,9 +126,100 @@ const Component = ({ closeFeedback = () => undefined }) => {
|
|||||||
</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" onClick={stepForward}>
|
||||||
|
Next
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<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 (
|
||||||
|
<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 />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{CurrentStep}
|
||||||
</article>
|
</article>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user