frontPanel/src/pages/PersonalizationAI/GenderAndAgeSelector.tsx

197 lines
6.3 KiB
TypeScript
Raw Normal View History

import { Box, FormControl, FormLabel, RadioGroup, FormControlLabel, Radio, Select, MenuItem, useTheme, Button, useMediaQuery, IconButton } from "@mui/material";
2025-05-25 13:21:19 +00:00
import { InfoPopover } from '@ui_kit/InfoPopover';
import CheckboxIcon from "@icons/Checkbox";
2025-06-01 22:02:43 +00:00
import { useState } from "react";
import { useCurrentQuiz } from "@/stores/quizes/hooks";
import { auditoryAdd } from "@/api/auditory";
import { useSnackbar } from "notistack";
2025-06-03 17:27:14 +00:00
import ArrowDownIcon from "@/assets/icons/ArrowDownIcon";
import AgeInputWithSelect from "./AgeInputWithSelect";
2025-05-25 13:21:19 +00:00
interface GenderAndAgeSelectorProps {
2025-06-01 22:02:43 +00:00
handleAdd: (item: any) => void;
2025-05-25 13:21:19 +00:00
}
2025-06-01 22:02:43 +00:00
export default function GenderAndAgeSelector({ handleAdd }: GenderAndAgeSelectorProps) {
2025-05-25 13:21:19 +00:00
const theme = useTheme();
2025-06-01 22:02:43 +00:00
const [age, setAge] = useState<string>('');
const [gender, setGender] = useState<string>('');
2025-06-03 17:27:14 +00:00
const [selectOpen, setSelectOpen] = useState<boolean>(false);
2025-06-01 22:02:43 +00:00
const quiz = useCurrentQuiz();
const { enqueueSnackbar } = useSnackbar();
const isMobile = useMediaQuery(theme.breakpoints.down(845));
const [ageError, setAgeError] = useState(false);
2025-06-01 22:02:43 +00:00
const addItem = async () => {
if (!quiz?.backendId) {
enqueueSnackbar('Ошибка: не выбран квиз', { variant: 'error' });
return;
}
try {
const [result, error] = await auditoryAdd({
quizId: quiz.backendId,
body: {
sex: gender === "male",
2025-06-01 22:02:43 +00:00
age
}
});
if (error) {
enqueueSnackbar('Не удалось добавить ссылку', { variant: 'error' });
return;
}
if (result) {
handleAdd({
id: result.ID,
quiz_id: quiz.backendId,
sex: gender === "male",
2025-06-01 22:02:43 +00:00
age,
deleted: false,
});
enqueueSnackbar('Ссылка успешно добавлена', { variant: 'success' });
// Очищаем форму
setGender('');
2025-06-01 22:02:43 +00:00
setAge('');
}
} catch (error) {
enqueueSnackbar('Произошла ошибка при добавлении', { variant: 'error' });
}
};
2025-05-25 13:21:19 +00:00
2025-06-01 22:02:43 +00:00
return (
2025-06-02 15:53:46 +00:00
<Box sx={{ display: 'flex', gap: 4, alignItems: "end", flexWrap: "wrap" }}>
2025-06-03 17:27:14 +00:00
<Box sx={{ display: "inline-flex", flexWrap: isMobile ? "wrap" : "initial" }}>
2025-05-25 13:21:19 +00:00
2025-06-01 22:02:43 +00:00
<FormControl component="fieldset" variant="standard">
2025-06-02 15:53:46 +00:00
<Box sx={{ display: 'flex', alignItems: "end", gap: '4px' }}>
2025-06-01 22:02:43 +00:00
<FormLabel
sx={{
'&.Mui-focused': {
color: '#4D4D4D',
},
2025-05-25 13:21:19 +00:00
color: '#4D4D4D',
fontSize: '18px',
2025-06-01 22:02:43 +00:00
fontWeight: 500,
2025-05-25 13:21:19 +00:00
lineHeight: 1,
letterSpacing: 0,
2025-06-01 22:02:43 +00:00
mr: '3px',
}}
component="legend">Пол</FormLabel>
{/* <InfoPopover /> */}
2025-06-01 22:02:43 +00:00
</Box>
<RadioGroup
2025-06-01 22:02:43 +00:00
sx={{
width: "155px",
justifyContent: "space-between",
2025-06-02 15:53:46 +00:00
mt: "20px",
2025-06-01 22:02:43 +00:00
ml: "-9px"
}}
row
aria-label="gender"
name="row-radio-buttons-group"
value={gender}
onChange={(e) => setGender(e.target.value)}
2025-06-01 22:02:43 +00:00
>
<FormControlLabel
sx={{
padding: 0,
'& .MuiTouchRipple-root': {
width: '100%',
height: '100%',
overflow: 'visible',
},
'& .MuiSvgIcon-root': {
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)',
},
'& .MuiFormControlLabel-label': {
fontSize: '18px',
fontWeight: 400,
fontFamily: 'Rubik',
lineHeight: 1,
letterSpacing: 0,
color: '#4D4D4D',
ml: "6px"
},
m: 0,
}}
value="male"
control={<Radio icon={<CheckboxIcon />} checkedIcon={<CheckboxIcon checked />} />}
2025-06-01 22:02:43 +00:00
label="М"
/>
<FormControlLabel
sx={{
padding: 0,
'& .MuiTouchRipple-root': {
width: '100%',
height: '100%',
overflow: 'visible',
},
'& .MuiSvgIcon-root': {
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)',
},
'& .MuiFormControlLabel-label': {
fontSize: '18px',
fontWeight: 400,
fontFamily: 'Rubik',
lineHeight: 1,
letterSpacing: 0,
color: '#4D4D4D',
},
m: 0,
}}
value="female"
control={<Radio icon={<CheckboxIcon />} checkedIcon={<CheckboxIcon checked />} />}
2025-06-01 22:02:43 +00:00
label="Ж"
/>
</RadioGroup>
2025-06-01 22:02:43 +00:00
</FormControl>
2025-06-02 18:22:55 +00:00
<FormControl sx={{ maxWidth: "420px", width: "100%", marginLeft: isMobile ? "0" : "120px", minWidth: "265px" }} variant="filled">
2025-06-01 22:02:43 +00:00
<Box sx={{
display: 'flex',
2025-06-02 15:53:46 +00:00
alignItems: 'end',
2025-06-01 22:02:43 +00:00
gap: '4px'
}}>
<FormLabel sx={{
color: '#4D4D4D',
fontSize: "18px",
fontWeight: 500,
lineHeight: "100%",
'&.Mui-focused': {
2025-05-25 13:21:19 +00:00
color: '#4D4D4D',
},
2025-06-01 22:02:43 +00:00
}}>Возраст</FormLabel>
{/* <InfoPopover /> */}
2025-06-01 22:02:43 +00:00
</Box>
<AgeInputWithSelect value={age} onChange={setAge} onErrorChange={setAgeError} />
2025-06-01 22:02:43 +00:00
</FormControl>
</Box>
<Button
2025-06-02 15:53:46 +00:00
onClick={addItem}
2025-06-01 22:02:43 +00:00
variant="contained"
disabled={!gender || !age || ageError}
2025-06-01 22:02:43 +00:00
sx={{
bgcolor: theme.palette.brightPurple.main,
borderRadius: "8px",
width: "130px",
height: "48px",
boxShadow: "none",
textTransform: "none",
fontSize: "18px",
'&:hover': { bgcolor: theme.palette.brightPurple.main },
}}
>
Ок
</Button>
2025-06-03 17:27:14 +00:00
</Box >
2025-05-25 13:21:19 +00:00
);
}