adminFront/src/pages/dashboard/Content/PromocodeManagement/CreatePromocodeForm.tsx

366 lines
11 KiB
TypeScript
Raw Normal View History

2024-02-22 14:12:50 +00:00
import { useEffect, useState } from "react";
2024-02-23 14:10:57 +00:00
import moment from "moment";
import { Formik, Field, Form } from "formik";
2024-02-22 14:12:50 +00:00
import {
Typography,
TextField,
Button,
RadioGroup,
Radio,
FormControlLabel,
Select,
2024-02-23 14:10:57 +00:00
MenuItem,
2024-02-22 14:12:50 +00:00
} from "@mui/material";
import { DesktopDatePicker } from "@mui/x-date-pickers/DesktopDatePicker";
2024-02-23 14:10:57 +00:00
2024-02-22 14:12:50 +00:00
import { requestPrivileges } from "@root/services/privilegies.service";
import { usePrivilegeStore } from "@root/stores/privilegesStore";
2024-02-26 09:27:10 +00:00
import { createPromocode } from "@root/api/promocode";
2024-02-22 14:12:50 +00:00
2024-02-23 14:10:57 +00:00
import { SERVICE_LIST } from "@root/model/privilege";
import theme from "@root/theme";
import type { ChangeEvent } from "react";
2024-02-22 14:12:50 +00:00
import type { TextFieldProps } from "@mui/material";
type BonusType = "discount" | "privilege";
type FormValues = {
codeword: string;
description: string;
greetings: string;
2024-02-23 14:10:57 +00:00
dueTo: number;
activationCount: number;
2024-02-22 14:12:50 +00:00
privilegeId: string;
2024-02-23 14:10:57 +00:00
amount: number;
layer: 1 | 2;
factor: number;
2024-02-22 14:12:50 +00:00
target: string;
2024-02-23 14:10:57 +00:00
threshold: number;
2024-02-22 14:12:50 +00:00
};
type CustomTextFieldProps = {
name: string;
label: string;
required?: boolean;
2024-02-23 14:10:57 +00:00
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
2024-02-22 14:12:50 +00:00
};
const CustomTextField = ({
name,
label,
required = false,
2024-02-23 14:10:57 +00:00
onChange,
2024-02-22 14:12:50 +00:00
}: CustomTextFieldProps) => (
<Field
name={name}
label={label}
required={required}
variant="filled"
color="secondary"
as={TextField}
2024-02-23 14:10:57 +00:00
onChange={onChange}
2024-02-22 14:12:50 +00:00
sx={{ width: "100%", marginTop: "15px" }}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
},
}}
InputLabelProps={{
style: { color: theme.palette.secondary.main },
}}
/>
);
2024-02-23 14:10:57 +00:00
const initialValues: FormValues = {
codeword: "",
description: "",
greetings: "",
dueTo: 0,
activationCount: 0,
privilegeId: "",
amount: 0,
layer: 1,
factor: 0,
target: "",
threshold: 0,
};
2024-02-22 14:12:50 +00:00
export const CreatePromocodeForm = () => {
const [bonusType, setBonusType] = useState<BonusType>("discount");
const { privileges } = usePrivilegeStore();
useEffect(() => {
requestPrivileges();
}, []);
2024-02-26 09:27:10 +00:00
const submitForm = async (values: FormValues) => {
2024-02-23 14:10:57 +00:00
const body = {
...values,
threshold: values.layer === 1 ? values.threshold : values.threshold * 100,
};
2024-02-26 09:27:10 +00:00
await createPromocode({
codeword: body.codeword,
description: body.description,
greetings: body.greetings,
dueTo: body.dueTo,
activationCount: body.activationCount,
bonus: {
privilege: { privilegeID: body.privilegeId, amount: body.amount },
discount: {
layer: body.layer,
factor: body.factor,
target: body.target,
threshold: body.threshold,
},
},
});
2024-02-22 14:12:50 +00:00
};
return (
2024-02-26 09:27:10 +00:00
<Formik initialValues={initialValues} onSubmit={submitForm}>
2024-02-23 14:10:57 +00:00
{({ values, handleChange, handleBlur, setFieldValue }) => (
2024-02-22 14:12:50 +00:00
<Form
style={{
width: "100%",
maxWidth: "600px",
2024-02-23 14:10:57 +00:00
padding: "0 10px",
2024-02-22 14:12:50 +00:00
}}
>
2024-02-23 14:10:57 +00:00
<CustomTextField
name="codeword"
label="Кодовое слово"
required
onChange={handleChange}
/>
<CustomTextField
name="description"
label="Описание"
required
onChange={handleChange}
/>
2024-02-22 14:12:50 +00:00
<CustomTextField
name="greetings"
label="Приветственное сообщение"
required
2024-02-23 14:10:57 +00:00
onChange={handleChange}
2024-02-22 14:12:50 +00:00
/>
<Typography
variant="h4"
sx={{
height: "40px",
fontWeight: "normal",
marginTop: "15px",
color: theme.palette.secondary.main,
}}
>
Время существования промокода
</Typography>
<Field
name="dueTo"
as={DesktopDatePicker}
inputFormat="DD/MM/YYYY"
2024-02-23 14:10:57 +00:00
value={values.dueTo ? new Date(Number(values.dueTo) * 1000) : null}
2024-02-22 14:12:50 +00:00
onChange={(date: Date | null) => {
if (date) {
2024-02-23 14:10:57 +00:00
setFieldValue("dueTo", moment(date).unix() || null);
2024-02-22 14:12:50 +00:00
}
}}
renderInput={(params: TextFieldProps) => <TextField {...params} />}
InputProps={{
sx: {
height: "40px",
color: theme.palette.secondary.main,
border: "1px solid",
borderColor: theme.palette.secondary.main,
"& .MuiSvgIcon-root": { color: theme.palette.secondary.main },
},
}}
/>
<CustomTextField
name="activationCount"
label="Количество активаций промокода"
2024-02-23 14:10:57 +00:00
onChange={({ target }) =>
setFieldValue(
"activationCount",
Number(target.value.replace(/\D/g, ""))
)
}
2024-02-22 14:12:50 +00:00
/>
<RadioGroup
row
name="bonusType"
value={bonusType}
sx={{ marginTop: "15px" }}
onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
setBonusType(target.value as BonusType);
}}
2024-02-23 14:10:57 +00:00
onBlur={handleBlur}
2024-02-22 14:12:50 +00:00
>
<FormControlLabel
value="discount"
control={<Radio color="secondary" />}
label="Скидка"
/>
<FormControlLabel
value="privilege"
control={<Radio color="secondary" />}
label="Привилегия"
/>
</RadioGroup>
{bonusType === "discount" && (
<>
<RadioGroup
row
name="layer"
2024-02-23 14:10:57 +00:00
value={values.layer}
2024-02-22 14:12:50 +00:00
sx={{ marginTop: "15px" }}
onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
2024-02-23 14:10:57 +00:00
setFieldValue("target", "");
setFieldValue("layer", Number(target.value));
2024-02-22 14:12:50 +00:00
}}
2024-02-23 14:10:57 +00:00
onBlur={handleBlur}
2024-02-22 14:12:50 +00:00
>
<FormControlLabel
2024-02-23 14:10:57 +00:00
value="1"
2024-02-22 14:12:50 +00:00
control={<Radio color="secondary" />}
label="Привилегия"
/>
<FormControlLabel
2024-02-23 14:10:57 +00:00
value="2"
2024-02-22 14:12:50 +00:00
control={<Radio color="secondary" />}
label="Сервис"
/>
</RadioGroup>
2024-02-23 14:10:57 +00:00
<CustomTextField
name="factor"
label="Процент скидки"
required
onChange={({ target }) =>
setFieldValue(
"factor",
Number(target.value.replace(/\D/g, ""))
)
}
/>
2024-02-22 14:12:50 +00:00
<Typography
variant="h4"
sx={{
height: "40px",
fontWeight: "normal",
marginTop: "15px",
padding: "0 12px",
color: theme.palette.secondary.main,
}}
>
2024-02-23 14:10:57 +00:00
{values.layer === 1 ? "Выбор привилегии" : "Выбор сервиса"}
2024-02-22 14:12:50 +00:00
</Typography>
<Field
name="target"
as={Select}
2024-02-23 14:10:57 +00:00
label={values.layer === 1 ? "Привилегия" : "Сервис"}
2024-02-22 14:12:50 +00:00
sx={{
width: "100%",
border: "2px solid",
color: theme.palette.secondary.main,
borderColor: theme.palette.secondary.main,
"&.Mui-focused .MuiOutlinedInput-notchedOutline": {
border: "none",
},
".MuiSvgIcon-root ": { fill: theme.palette.secondary.main },
}}
children={
2024-02-23 14:10:57 +00:00
values.layer === 1
2024-02-22 14:12:50 +00:00
? privileges.map(({ name, privilegeId }) => (
<MenuItem key={privilegeId} value={privilegeId}>
{name}
</MenuItem>
))
2024-02-23 14:10:57 +00:00
: SERVICE_LIST.map(({ displayName, serviceKey }) => (
2024-02-22 14:12:50 +00:00
<MenuItem key={serviceKey} value={serviceKey}>
2024-02-23 14:10:57 +00:00
{displayName}
2024-02-22 14:12:50 +00:00
</MenuItem>
))
}
/>
<CustomTextField
name="threshold"
label="При каком значении применяется скидка"
2024-02-23 14:10:57 +00:00
onChange={({ target }) =>
setFieldValue(
"threshold",
Number(target.value.replace(/\D/g, ""))
)
}
2024-02-22 14:12:50 +00:00
/>
</>
)}
{bonusType === "privilege" && (
<>
<Typography
variant="h4"
sx={{
height: "40px",
fontWeight: "normal",
marginTop: "15px",
padding: "0 12px",
color: theme.palette.secondary.main,
}}
>
Выбор привилегии
</Typography>
<Field
name="privilegeId"
as={Select}
label="Привилегия"
sx={{
width: "100%",
border: "2px solid",
color: theme.palette.secondary.main,
borderColor: theme.palette.secondary.main,
"&.Mui-focused .MuiOutlinedInput-notchedOutline": {
border: "none",
},
".MuiSvgIcon-root ": { fill: theme.palette.secondary.main },
}}
children={privileges.map(({ name, privilegeId }) => (
<MenuItem key={privilegeId} value={privilegeId}>
{name}
</MenuItem>
))}
/>
2024-02-23 14:10:57 +00:00
<CustomTextField
name="amount"
label="Количество"
required
onChange={({ target }) =>
setFieldValue(
"amount",
Number(target.value.replace(/\D/g, ""))
)
}
/>
2024-02-22 14:12:50 +00:00
</>
)}
<Button
variant="contained"
sx={{
display: "block",
padding: "10px",
margin: "15px auto 0",
fontWeight: "normal",
fontSize: "18px",
backgroundColor: theme.palette.menu.main,
"&:hover": { backgroundColor: theme.palette.grayMedium.main },
}}
type="submit"
>
Cоздать
</Button>
</Form>
)}
</Formik>
);
};