feat: branch store

This commit is contained in:
IlyaDoronin 2023-08-16 15:00:35 +03:00
parent 506e93028e
commit 5ac5d8c0af
4 changed files with 85 additions and 55 deletions

@ -13,29 +13,25 @@ import type { SelectChangeEvent, SxProps } from "@mui/material";
type SelectProps = { type SelectProps = {
items: string[]; items: string[];
empty?: boolean; empty?: boolean;
onChange?: (num: number) => void; onChange?: (item: string, num: number) => void;
sx?: SxProps; sx?: SxProps;
}; };
export const Select = ({ items, empty, onChange, sx }: SelectProps) => { export const Select = ({ items, empty, onChange, sx }: SelectProps) => {
const [activeItem, setActiveItem] = useState<number>(empty ? -1 : 0); const [activeItem, setActiveItem] = useState<number>(empty ? -1 : 0);
const theme = useTheme(); const theme = useTheme();
const handleChange = (event: SelectChangeEvent) => { const handleChange = (event: SelectChangeEvent) => {
setActiveItem(Number(event.target.value)); const activeItemIndex = Number(event.target.value);
onChange?.(Number(event.target.value)); setActiveItem(activeItemIndex);
onChange?.(items[activeItemIndex], activeItemIndex);
}; };
return ( return (
<FormControl <FormControl
fullWidth fullWidth
size="small" size="small"
sx={{ sx={{ width: "100%", height: "48px", ...sx }}
width: "100%",
height: "48px",
...sx,
}}
> >
<MuiSelect <MuiSelect
id="display-select" id="display-select"

@ -11,29 +11,37 @@ import {
Typography, Typography,
useTheme, useTheme,
} from "@mui/material"; } from "@mui/material";
import * as React from "react"; import { useState, useEffect } from "react";
import { useState } from "react";
import { questionStore, resetSomeField } from "@root/questions"; import { questionStore, resetSomeField } from "@root/questions";
import { updateBranchState } from "@root/branches";
import { Select } from "./Select"; import { Select } from "./Select";
import DeleteIcon from "@icons/questionsPage/deleteIcon"; import DeleteIcon from "@icons/questionsPage/deleteIcon";
import RadioCheck from "@ui_kit/RadioCheck"; import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon"; import RadioIcon from "@ui_kit/RadioIcon";
import InfoIcon from "@icons/Info"; import InfoIcon from "@icons/Info";
const ACTIONS = ["Показать", "Скрыть"];
const STIPULATION = ["Условие 1", "Условие 2", "Условие 3"];
const ANSWER = ["Условие 1", "Условие 2", "Условие 3"];
const CONDITIONS = [
"Все условия обязательны",
"Обязательно хотя бы одно условие",
];
export default function BranchingQuestions() { export default function BranchingQuestions() {
const [value, setValue] = useState("1"); const [condition, setCondition] = useState<boolean>(false);
const [conditionSelected, setConditionSelected] = useState<boolean>(false); const [conditionSelected, setConditionSelected] = useState<boolean>(false);
const { openedModalSettings } = questionStore(); const { openedModalSettings } = questionStore();
const theme = useTheme(); const theme = useTheme();
const [condition, setCondition] = useState<boolean>(false); useEffect(() => {
updateBranchState({ action: ACTIONS[0], condition: CONDITIONS[0] });
}, []);
const handleClose = () => { const handleClose = () => {
resetSomeField({ openedModalSettings: "" }); resetSomeField({ openedModalSettings: "" });
}; };
const handleChangeRadio = (event: React.ChangeEvent<HTMLInputElement>) => {
setValue((event.target as HTMLInputElement).value);
};
return ( return (
<> <>
<Modal <Modal
@ -85,8 +93,9 @@ export default function BranchingQuestions() {
}} }}
> >
<Select <Select
items={["Показать", "Скрыть"]} items={ACTIONS}
sx={{ maxWidth: "140px" }} sx={{ maxWidth: "140px" }}
onChange={(action) => updateBranchState({ action })}
/> />
<Typography sx={{ color: theme.palette.grey2.main }}> <Typography sx={{ color: theme.palette.grey2.main }}>
если в ответе на вопрос если в ответе на вопрос
@ -118,14 +127,21 @@ export default function BranchingQuestions() {
</Box> </Box>
<Select <Select
empty empty
items={["Условие 1", "Условие 2", "Условие 3"]} items={STIPULATION}
onChange={() => setConditionSelected(true)} onChange={(stipulation) => {
setConditionSelected(true);
updateBranchState({ stipulation });
}}
sx={{ marginBottom: "15px" }} sx={{ marginBottom: "15px" }}
/> />
{conditionSelected && ( {conditionSelected && (
<> <>
<Box <Box
sx={{ display: "flex", alignItems: "center", pb: "10px" }} sx={{
display: "flex",
alignItems: "center",
pb: "10px",
}}
> >
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}> <Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>
Дан ответ Дан ответ
@ -136,7 +152,8 @@ export default function BranchingQuestions() {
</Box> </Box>
<Select <Select
empty empty
items={["Условие 1", "Условие 2", "Условие 3"]} items={ANSWER}
onChange={(answer) => updateBranchState({ answer })}
/> />
</> </>
)} )}
@ -152,7 +169,6 @@ export default function BranchingQuestions() {
}} }}
> >
<Link <Link
component="button"
variant="body2" variant="body2"
sx={{ sx={{
color: theme.palette.brightPurple.main, color: theme.palette.brightPurple.main,
@ -166,32 +182,27 @@ export default function BranchingQuestions() {
<FormControl> <FormControl>
<RadioGroup <RadioGroup
aria-labelledby="demo-controlled-radio-buttons-group" aria-labelledby="demo-controlled-radio-buttons-group"
name="controlled-radio-buttons-group" defaultValue={0}
value={value} onChange={({ target }) =>
onChange={handleChangeRadio} updateBranchState({
condition: CONDITIONS[Number(target.value)],
})
}
> >
{CONDITIONS.map((condition, index) => (
<FormControlLabel <FormControlLabel
key={condition + index}
sx={{ color: theme.palette.grey2.main }} sx={{ color: theme.palette.grey2.main }}
value="1" value={index}
control={ control={
<Radio <Radio
checkedIcon={<RadioCheck />} checkedIcon={<RadioCheck />}
icon={<RadioIcon />} icon={<RadioIcon />}
/> />
} }
label="Все условия обязательны" label={condition}
/>
<FormControlLabel
sx={{ color: theme.palette.grey2.main }}
value="2"
control={
<Radio
checkedIcon={<RadioCheck />}
icon={<RadioIcon />}
/>
}
label="Обязательно хотя бы одно условие"
/> />
))}
</RadioGroup> </RadioGroup>
</FormControl> </FormControl>
</Box> </Box>
@ -199,19 +210,14 @@ export default function BranchingQuestions() {
<Button <Button
variant="outlined" variant="outlined"
onClick={handleClose} onClick={handleClose}
sx={{ sx={{ width: "100%", maxWidth: "130px" }}
width: "100%",
maxWidth: "130px",
}}
> >
Отмена Отмена
</Button> </Button>
<Button <Button
variant="contained" variant="contained"
sx={{ sx={{ width: "100%", maxWidth: "130px" }}
width: "100%", onClick={handleClose}
maxWidth: "130px",
}}
> >
Готово Готово
</Button> </Button>

@ -119,6 +119,7 @@ export default function HelpQuestions() {
borderRadius: "12px", borderRadius: "12px",
boxShadow: 24, boxShadow: 24,
p: 0, p: 0,
overflow: "hidden",
}} }}
> >
<Box <Box

27
src/stores/branches.ts Normal file

@ -0,0 +1,27 @@
import { create } from "zustand";
export type Branch = {
action: string;
stipulation: string;
answer: string;
condition: string;
};
interface BranchStore {
branch: Branch;
}
export const branchStore = create<BranchStore>()(() => ({
branch: {
action: "",
stipulation: "",
answer: "",
condition: "",
},
}));
export const updateBranchState = (data: Partial<Branch>) => {
const state = { ...branchStore.getState().branch };
branchStore.setState({ branch: { ...state, ...data } });
};