установка полей перед созданием

This commit is contained in:
Nastya 2024-04-13 15:38:46 +03:00
parent 541dc1610e
commit 1d9be4f223

@ -74,11 +74,28 @@ export const CreatePromocodeForm = ({ createPromocode }: Props) => {
}, []);
const submitForm = (values: FormValues) => {
console.log("полученные вельюсы", values)
const currentPrivilege = privileges.find(
(item) => item.privilegeId === values.privilegeId
);
console.log("привилегия", currentPrivilege)
console.log("bonusType", bonusType)
const body = {
...values,
threshold: values.layer === 1 ? values.threshold : values.threshold * 100,
};
if ((body.layer === 1 && bonusType === "discount") || bonusType === "privilege") {
if (currentPrivilege === undefined) return;
body.serviceKey = currentPrivilege?.serviceKey
body.target = body.privilegeId
}
if ((body.layer === 2 && bonusType === "discount")) {
if (body.serviceKey === undefined) return;
body.target = body.serviceKey
}
const factorFromDiscountValue = 1 - body.factor / 100;
return createPromocode({
@ -239,49 +256,64 @@ export const CreatePromocodeForm = ({ createPromocode }: Props) => {
>
{values.layer === 1 ? "Выбор привилегии" : "Выбор сервиса"}
</Typography>
<Field
name="target"
as={Select}
label={values.layer === 1 ? "Привилегия" : "Сервис"}
onChange={({ target }: SelectChangeProps) => {
if (values.layer === 1) {
const currentPrivilege = privileges.find(
(item) => item.privilegeId === target.value
);
{
values.layer === 1 ?
setFieldValue("serviceKey", currentPrivilege?.serviceKey);
setFieldValue("privilegeId", currentPrivilege?.privilegeId);
return;
}
setFieldValue("privilegeId", target.value);
}}
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 },
}}
value={values.privilegeId}
children={
values.layer === 1
? privileges.map(({ name, privilegeId }) => (
<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 },
}}
onChange={({ target }: SelectChangeProps) => {
setFieldValue("target", target.value)
setFieldValue("privilegeId", target.value)
}}
children={
privileges.map(({ name, privilegeId }) => (
<MenuItem key={privilegeId} value={privilegeId}>
{name}
</MenuItem>
))
: SERVICE_LIST.map(({ displayName, serviceKey }) => (
}
/>
:
<Field
name="serviceKey"
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 },
}}
onChange={({ target }: SelectChangeProps) => {
setFieldValue("target", target.value)
setFieldValue("serviceKey", target.value)
}}
children={
SERVICE_LIST.map(({ displayName, serviceKey }) => (
<MenuItem key={serviceKey} value={serviceKey}>
{displayName}
</MenuItem>
))
}
/>
}
/>
}
<CustomTextField
name="threshold"
label="При каком значении применяется скидка"
@ -312,14 +344,6 @@ export const CreatePromocodeForm = ({ createPromocode }: Props) => {
name="privilegeId"
as={Select}
label="Привилегия"
onChange={({ target }: SelectChangeProps) => {
const currentPrivilege = privileges.find(
(item) => item.privilegeId === target.value
);
setFieldValue("serviceKey", currentPrivilege?.serviceKey);
setFieldValue("privilegeId", currentPrivilege?.privilegeId);
}}
sx={{
width: "100%",
border: "2px solid",