feat: CreatePromocodeForm logic
This commit is contained in:
parent
bd6dfc182e
commit
430a6c2436
@ -1,4 +1,6 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import moment from "moment";
|
||||||
|
import { Formik, Field, Form } from "formik";
|
||||||
import {
|
import {
|
||||||
Typography,
|
Typography,
|
||||||
TextField,
|
TextField,
|
||||||
@ -7,43 +9,46 @@ import {
|
|||||||
Radio,
|
Radio,
|
||||||
FormControlLabel,
|
FormControlLabel,
|
||||||
Select,
|
Select,
|
||||||
|
MenuItem,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { Formik, Field, Form } from "formik";
|
|
||||||
import { DesktopDatePicker } from "@mui/x-date-pickers/DesktopDatePicker";
|
import { DesktopDatePicker } from "@mui/x-date-pickers/DesktopDatePicker";
|
||||||
import MenuItem from "@mui/material/MenuItem";
|
|
||||||
import theme from "../../../../theme";
|
|
||||||
import { requestPrivileges } from "@root/services/privilegies.service";
|
import { requestPrivileges } from "@root/services/privilegies.service";
|
||||||
import { usePrivilegeStore } from "@root/stores/privilegesStore";
|
import { usePrivilegeStore } from "@root/stores/privilegesStore";
|
||||||
import { useCartStore } from "@root/stores/cart";
|
|
||||||
|
|
||||||
|
import { SERVICE_LIST } from "@root/model/privilege";
|
||||||
|
import theme from "@root/theme";
|
||||||
|
|
||||||
|
import type { ChangeEvent } from "react";
|
||||||
import type { TextFieldProps } from "@mui/material";
|
import type { TextFieldProps } from "@mui/material";
|
||||||
|
|
||||||
type BonusType = "discount" | "privilege";
|
type BonusType = "discount" | "privilege";
|
||||||
type LayerType = "privilege" | "service";
|
|
||||||
type FormValues = {
|
type FormValues = {
|
||||||
codeword: string;
|
codeword: string;
|
||||||
description: string;
|
description: string;
|
||||||
greetings: string;
|
greetings: string;
|
||||||
dueTo: string;
|
dueTo: number;
|
||||||
activationCount: string;
|
activationCount: number;
|
||||||
privilegeId: string;
|
privilegeId: string;
|
||||||
amount: string;
|
amount: number;
|
||||||
layer: string;
|
layer: 1 | 2;
|
||||||
factor: string;
|
factor: number;
|
||||||
target: string;
|
target: string;
|
||||||
threshold: string;
|
threshold: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
type CustomTextFieldProps = {
|
type CustomTextFieldProps = {
|
||||||
name: string;
|
name: string;
|
||||||
label: string;
|
label: string;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
|
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const CustomTextField = ({
|
const CustomTextField = ({
|
||||||
name,
|
name,
|
||||||
label,
|
label,
|
||||||
required = false,
|
required = false,
|
||||||
|
onChange,
|
||||||
}: CustomTextFieldProps) => (
|
}: CustomTextFieldProps) => (
|
||||||
<Field
|
<Field
|
||||||
name={name}
|
name={name}
|
||||||
@ -52,6 +57,7 @@ const CustomTextField = ({
|
|||||||
variant="filled"
|
variant="filled"
|
||||||
color="secondary"
|
color="secondary"
|
||||||
as={TextField}
|
as={TextField}
|
||||||
|
onChange={onChange}
|
||||||
sx={{ width: "100%", marginTop: "15px" }}
|
sx={{ width: "100%", marginTop: "15px" }}
|
||||||
InputProps={{
|
InputProps={{
|
||||||
style: {
|
style: {
|
||||||
@ -65,51 +71,64 @@ const CustomTextField = ({
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
export const CreatePromocodeForm = () => {
|
const initialValues: FormValues = {
|
||||||
const [dueTo, setDueTo] = useState<Date | null>(new Date());
|
|
||||||
const [bonusType, setBonusType] = useState<BonusType>("discount");
|
|
||||||
const [layerType, setLayerType] = useState<LayerType>("privilege");
|
|
||||||
|
|
||||||
const { privileges } = usePrivilegeStore();
|
|
||||||
const { cartData } = useCartStore();
|
|
||||||
|
|
||||||
const initialValues: FormValues = {
|
|
||||||
codeword: "",
|
codeword: "",
|
||||||
description: "",
|
description: "",
|
||||||
greetings: "",
|
greetings: "",
|
||||||
dueTo: "",
|
dueTo: 0,
|
||||||
activationCount: "",
|
activationCount: 0,
|
||||||
privilegeId: "",
|
privilegeId: "",
|
||||||
amount: "",
|
amount: 0,
|
||||||
layer: "",
|
layer: 1,
|
||||||
factor: "",
|
factor: 0,
|
||||||
target: "",
|
target: "",
|
||||||
threshold: "",
|
threshold: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const CreatePromocodeForm = () => {
|
||||||
|
const [bonusType, setBonusType] = useState<BonusType>("discount");
|
||||||
|
const { privileges } = usePrivilegeStore();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
requestPrivileges();
|
requestPrivileges();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const createPromocode = (values: FormValues) => {
|
const createPromocode = (values: FormValues) => {
|
||||||
console.log(values);
|
const body = {
|
||||||
|
...values,
|
||||||
|
threshold: values.layer === 1 ? values.threshold : values.threshold * 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(body);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Formik initialValues={initialValues} onSubmit={createPromocode}>
|
<Formik initialValues={initialValues} onSubmit={createPromocode}>
|
||||||
{(props) => (
|
{({ values, handleChange, handleBlur, setFieldValue }) => (
|
||||||
<Form
|
<Form
|
||||||
style={{
|
style={{
|
||||||
width: "100%",
|
width: "100%",
|
||||||
maxWidth: "600px",
|
maxWidth: "600px",
|
||||||
|
padding: "0 10px",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CustomTextField name="codeword" label="Кодовое слово" required />
|
<CustomTextField
|
||||||
<CustomTextField name="description" label="Описание" required />
|
name="codeword"
|
||||||
|
label="Кодовое слово"
|
||||||
|
required
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
|
<CustomTextField
|
||||||
|
name="description"
|
||||||
|
label="Описание"
|
||||||
|
required
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
<CustomTextField
|
<CustomTextField
|
||||||
name="greetings"
|
name="greetings"
|
||||||
label="Приветственное сообщение"
|
label="Приветственное сообщение"
|
||||||
required
|
required
|
||||||
|
onChange={handleChange}
|
||||||
/>
|
/>
|
||||||
<Typography
|
<Typography
|
||||||
variant="h4"
|
variant="h4"
|
||||||
@ -126,10 +145,10 @@ export const CreatePromocodeForm = () => {
|
|||||||
name="dueTo"
|
name="dueTo"
|
||||||
as={DesktopDatePicker}
|
as={DesktopDatePicker}
|
||||||
inputFormat="DD/MM/YYYY"
|
inputFormat="DD/MM/YYYY"
|
||||||
value={dueTo}
|
value={values.dueTo ? new Date(Number(values.dueTo) * 1000) : null}
|
||||||
onChange={(date: Date | null) => {
|
onChange={(date: Date | null) => {
|
||||||
if (date) {
|
if (date) {
|
||||||
setDueTo(date);
|
setFieldValue("dueTo", moment(date).unix() || null);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
renderInput={(params: TextFieldProps) => <TextField {...params} />}
|
renderInput={(params: TextFieldProps) => <TextField {...params} />}
|
||||||
@ -146,6 +165,12 @@ export const CreatePromocodeForm = () => {
|
|||||||
<CustomTextField
|
<CustomTextField
|
||||||
name="activationCount"
|
name="activationCount"
|
||||||
label="Количество активаций промокода"
|
label="Количество активаций промокода"
|
||||||
|
onChange={({ target }) =>
|
||||||
|
setFieldValue(
|
||||||
|
"activationCount",
|
||||||
|
Number(target.value.replace(/\D/g, ""))
|
||||||
|
)
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
row
|
row
|
||||||
@ -155,7 +180,7 @@ export const CreatePromocodeForm = () => {
|
|||||||
onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
|
onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
setBonusType(target.value as BonusType);
|
setBonusType(target.value as BonusType);
|
||||||
}}
|
}}
|
||||||
onBlur={props.handleBlur}
|
onBlur={handleBlur}
|
||||||
>
|
>
|
||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
value="discount"
|
value="discount"
|
||||||
@ -173,25 +198,36 @@ export const CreatePromocodeForm = () => {
|
|||||||
<RadioGroup
|
<RadioGroup
|
||||||
row
|
row
|
||||||
name="layer"
|
name="layer"
|
||||||
value={layerType}
|
value={values.layer}
|
||||||
sx={{ marginTop: "15px" }}
|
sx={{ marginTop: "15px" }}
|
||||||
onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
|
onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
setLayerType(target.value as LayerType);
|
setFieldValue("target", "");
|
||||||
|
setFieldValue("layer", Number(target.value));
|
||||||
}}
|
}}
|
||||||
onBlur={props.handleBlur}
|
onBlur={handleBlur}
|
||||||
>
|
>
|
||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
value="privilege"
|
value="1"
|
||||||
control={<Radio color="secondary" />}
|
control={<Radio color="secondary" />}
|
||||||
label="Привилегия"
|
label="Привилегия"
|
||||||
/>
|
/>
|
||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
value="service"
|
value="2"
|
||||||
control={<Radio color="secondary" />}
|
control={<Radio color="secondary" />}
|
||||||
label="Сервис"
|
label="Сервис"
|
||||||
/>
|
/>
|
||||||
</RadioGroup>
|
</RadioGroup>
|
||||||
<CustomTextField name="factor" label="Процент скидки" required />
|
<CustomTextField
|
||||||
|
name="factor"
|
||||||
|
label="Процент скидки"
|
||||||
|
required
|
||||||
|
onChange={({ target }) =>
|
||||||
|
setFieldValue(
|
||||||
|
"factor",
|
||||||
|
Number(target.value.replace(/\D/g, ""))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
<Typography
|
<Typography
|
||||||
variant="h4"
|
variant="h4"
|
||||||
sx={{
|
sx={{
|
||||||
@ -202,14 +238,12 @@ export const CreatePromocodeForm = () => {
|
|||||||
color: theme.palette.secondary.main,
|
color: theme.palette.secondary.main,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{layerType === "privilege"
|
{values.layer === 1 ? "Выбор привилегии" : "Выбор сервиса"}
|
||||||
? "Выбор привилегии"
|
|
||||||
: "Выбор сервиса"}
|
|
||||||
</Typography>
|
</Typography>
|
||||||
<Field
|
<Field
|
||||||
name="target"
|
name="target"
|
||||||
as={Select}
|
as={Select}
|
||||||
label={layerType === "privilege" ? "Привилегия" : "Сервис"}
|
label={values.layer === 1 ? "Привилегия" : "Сервис"}
|
||||||
sx={{
|
sx={{
|
||||||
width: "100%",
|
width: "100%",
|
||||||
border: "2px solid",
|
border: "2px solid",
|
||||||
@ -221,15 +255,15 @@ export const CreatePromocodeForm = () => {
|
|||||||
".MuiSvgIcon-root ": { fill: theme.palette.secondary.main },
|
".MuiSvgIcon-root ": { fill: theme.palette.secondary.main },
|
||||||
}}
|
}}
|
||||||
children={
|
children={
|
||||||
layerType === "privilege"
|
values.layer === 1
|
||||||
? privileges.map(({ name, privilegeId }) => (
|
? privileges.map(({ name, privilegeId }) => (
|
||||||
<MenuItem key={privilegeId} value={privilegeId}>
|
<MenuItem key={privilegeId} value={privilegeId}>
|
||||||
{name}
|
{name}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
))
|
))
|
||||||
: cartData?.services.map(({ serviceKey }) => (
|
: SERVICE_LIST.map(({ displayName, serviceKey }) => (
|
||||||
<MenuItem key={serviceKey} value={serviceKey}>
|
<MenuItem key={serviceKey} value={serviceKey}>
|
||||||
{serviceKey}
|
{displayName}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -237,7 +271,12 @@ export const CreatePromocodeForm = () => {
|
|||||||
<CustomTextField
|
<CustomTextField
|
||||||
name="threshold"
|
name="threshold"
|
||||||
label="При каком значении применяется скидка"
|
label="При каком значении применяется скидка"
|
||||||
required
|
onChange={({ target }) =>
|
||||||
|
setFieldValue(
|
||||||
|
"threshold",
|
||||||
|
Number(target.value.replace(/\D/g, ""))
|
||||||
|
)
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
@ -275,7 +314,17 @@ export const CreatePromocodeForm = () => {
|
|||||||
</MenuItem>
|
</MenuItem>
|
||||||
))}
|
))}
|
||||||
/>
|
/>
|
||||||
<CustomTextField name="amount" label="Количество" required />
|
<CustomTextField
|
||||||
|
name="amount"
|
||||||
|
label="Количество"
|
||||||
|
required
|
||||||
|
onChange={({ target }) =>
|
||||||
|
setFieldValue(
|
||||||
|
"amount",
|
||||||
|
Number(target.value.replace(/\D/g, ""))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
|
Loading…
Reference in New Issue
Block a user