fix: applied timezone to default values, removed value nesting

This commit is contained in:
2025-10-28 11:43:13 +04:00
parent 3575b9f34a
commit ea6a6df371
6 changed files with 61 additions and 56 deletions

View File

@ -26,11 +26,10 @@ const AttributeValueInput: FC<Props> = ({
};
const renderCheckbox = () => {
const localValue = value === undefined ? false : value;
return (
<Checkbox
{...commonProps}
checked={localValue}
checked={Boolean(value)}
onChange={e => onChange(e.currentTarget.checked)}
/>
);
@ -80,7 +79,7 @@ const AttributeValueInput: FC<Props> = ({
<NumberInput
{...commonProps}
allowDecimal={attrInfo.type.type === "float"}
value={Number(value)}
value={value ? Number(value) : undefined}
onChange={value => onChange(Number(value))}
/>
);

View File

@ -23,8 +23,9 @@ type Props = {
deal: DealSchema;
};
type Value = {
type AttrInfo = {
value?: any;
isApplicableToGroup: boolean;
};
const CustomTab: FC<Props> = ({ module, deal }) => {
@ -35,16 +36,19 @@ const CustomTab: FC<Props> = ({ module, deal }) => {
});
const [attributeValuesMap, setAttributeValuesMap] = useState<
Map<number, Value | null>
Map<number, AttrInfo | null>
>(new Map());
const [attributeErrorsMap, setAttributeErrorsMap] = useState<
Map<number, string>
>(new Map());
useEffect(() => {
const values = new Map<number, Value | null>();
const values = new Map<number, AttrInfo | null>();
for (const dealAttr of dealAttributes) {
values.set(dealAttr.attributeId, dealAttr?.value);
values.set(dealAttr.attributeId, {
...dealAttr,
value: dealAttr.value,
});
}
setAttributeValuesMap(values);
}, [dealAttributes]);
@ -65,10 +69,10 @@ const CustomTab: FC<Props> = ({ module, deal }) => {
attributeValuesMap
.entries()
.map(
([attributeId, value]) =>
([attributeId, info]) =>
({
attributeId,
value,
...info,
}) as UpdateDealModuleAttributeSchema
)
.toArray();
@ -83,7 +87,10 @@ const CustomTab: FC<Props> = ({ module, deal }) => {
attrInfo={attribute}
value={attributeValuesMap.get(attribute.attributeId)?.value}
onChange={value => {
attributeValuesMap.set(attribute.attributeId, { value });
attributeValuesMap.set(attribute.attributeId, {
...attribute,
value,
});
setAttributeValuesMap(new Map(attributeValuesMap));
attributeErrorsMap.delete(attribute.attributeId);
setAttributeErrorsMap(new Map(attributeErrorsMap));

View File

@ -2,6 +2,7 @@ import { Checkbox, NumberInput, TextInput } from "@mantine/core";
import { DatePickerInput, DateTimePicker } from "@mantine/dates";
import { UseFormReturnType } from "@mantine/form";
import { UpdateAttributeSchema } from "@/lib/client";
import { naiveDateTimeStringToUtc } from "@/utils/datetime";
type Props = {
form: UseFormReturnType<Partial<UpdateAttributeSchema>>;
@ -12,7 +13,7 @@ const DefaultAttributeValueInput = ({ form }: Props) => {
const label = "Значение по умолчанию";
const inputName = "defaultValue";
const value = form.getValues().defaultValue?.value;
const value = form.getValues().defaultValue;
if (type === "bool") {
return (
@ -21,9 +22,7 @@ const DefaultAttributeValueInput = ({ form }: Props) => {
{...form.getInputProps(inputName, { type: "checkbox" })}
checked={value as boolean}
onChange={e =>
form.setFieldValue("defaultValue", {
value: e.currentTarget.checked,
})
form.setFieldValue("defaultValue", e.currentTarget.checked)
}
/>
);
@ -33,13 +32,11 @@ const DefaultAttributeValueInput = ({ form }: Props) => {
label={label}
{...form.getInputProps(inputName)}
value={
form.values.defaultValue?.value
? new Date(String(form.values.defaultValue.value))
form.values.defaultValue
? new Date(String(form.values.defaultValue))
: null
}
onChange={value =>
form.setFieldValue("defaultValue", { value })
}
onChange={value => form.setFieldValue("defaultValue", value)}
clearable
locale={"ru"}
valueFormat={"DD.MM.YYYY"}
@ -51,13 +48,21 @@ const DefaultAttributeValueInput = ({ form }: Props) => {
label={label}
{...form.getInputProps(inputName)}
value={
form.values.defaultValue?.value
? new Date(String(form.values.defaultValue.value))
form.values.defaultValue
? naiveDateTimeStringToUtc(
form.values.defaultValue as string
)
: null
}
onChange={value =>
form.setFieldValue("defaultValue", { value })
}
onChange={val => {
if (!val) return;
const localDate = new Date(val.replace(" ", "T"));
const utcString = localDate
.toISOString()
.substring(0, 19)
.replace("T", " ");
form.setFieldValue("defaultValue", utcString);
}}
clearable
locale={"ru"}
valueFormat={"DD.MM.YYYY HH:mm"}
@ -76,9 +81,9 @@ const DefaultAttributeValueInput = ({ form }: Props) => {
allowDecimal={type === "float"}
label={label}
{...form.getInputProps(inputName)}
value={Number(form.values.defaultValue?.value)}
value={Number(form.values.defaultValue)}
onChange={value =>
form.setFieldValue("defaultValue", { value: Number(value) })
form.setFieldValue("defaultValue", Number(value))
}
/>
);

View File

@ -7,6 +7,7 @@ import { Box } from "@mantine/core";
import useIsMobile from "@/hooks/utils/useIsMobile";
import { ModuleAttributeSchema } from "@/lib/client";
import {
naiveDateTimeStringToUtc,
utcDateTimeToLocalString,
utcDateToLocalString,
} from "@/utils/datetime";
@ -32,12 +33,14 @@ const useAttributesInnerTableColumns = () => {
accessor: "defaultValue",
render: attr => {
if (!attr.defaultValue) return <>-</>;
const value = attr.defaultValue.value;
const value = attr.defaultValue;
if (value === null) return <>-</>;
const type = attr.type.type;
if (type === "datetime") {
return utcDateTimeToLocalString(value as string);
return utcDateTimeToLocalString(
naiveDateTimeStringToUtc(value as string)
);
}
if (type === "date") {
return utcDateToLocalString(value as string);

View File

@ -43,9 +43,7 @@ export type AttributeSchema = {
/**
* Defaultvalue
*/
defaultValue: {
[key: string]: unknown;
} | null;
defaultValue: unknown | null;
/**
* Description
*/
@ -305,9 +303,7 @@ export type CreateAttributeSchema = {
/**
* Defaultvalue
*/
defaultValue: {
[key: string]: unknown;
} | null;
defaultValue: unknown | null;
/**
* Description
*/
@ -1037,16 +1033,12 @@ export type DealModuleAttributeSchema = {
/**
* Value
*/
value: {
[key: string]: unknown;
} | null;
value: unknown | null;
type: AttributeTypeSchema;
/**
* Defaultvalue
*/
defaultValue: {
[key: string]: unknown;
};
defaultValue: unknown;
/**
* Description
*/
@ -1770,9 +1762,7 @@ export type ModuleAttributeSchema = {
/**
* Defaultvalue
*/
defaultValue: {
[key: string]: unknown;
} | null;
defaultValue: unknown | null;
/**
* Description
*/
@ -2374,9 +2364,7 @@ export type UpdateAttributeSchema = {
/**
* Defaultvalue
*/
defaultValue?: {
[key: string]: unknown;
} | null;
defaultValue?: unknown | null;
/**
* Description
*/
@ -2530,12 +2518,14 @@ export type UpdateDealModuleAttributeSchema = {
* Attributeid
*/
attributeId: number;
/**
* Isapplicabletogroup
*/
isApplicableToGroup: boolean;
/**
* Value
*/
value: {
[key: string]: unknown;
} | null;
value?: unknown | null;
};
/**

View File

@ -33,7 +33,7 @@ export const zAttributeSchema = z.object({
label: z.string(),
isApplicableToGroup: z.boolean(),
isNullable: z.boolean(),
defaultValue: z.union([z.object({}), z.null()]),
defaultValue: z.union([z.unknown(), z.null()]),
description: z.string(),
typeId: z.int(),
id: z.int(),
@ -141,7 +141,7 @@ export const zCreateAttributeSchema = z.object({
label: z.string(),
isApplicableToGroup: z.boolean(),
isNullable: z.boolean(),
defaultValue: z.union([z.object({}), z.null()]),
defaultValue: z.union([z.unknown(), z.null()]),
description: z.string(),
typeId: z.int(),
});
@ -830,9 +830,9 @@ export const zDealModuleAttributeSchema = z.object({
attributeId: z.int(),
label: z.string(),
originalLabel: z.string(),
value: z.union([z.object({}), z.null()]),
value: z.union([z.unknown(), z.null()]),
type: zAttributeTypeSchema,
defaultValue: z.object({}),
defaultValue: z.unknown(),
description: z.string(),
isApplicableToGroup: z.boolean(),
isNullable: z.boolean(),
@ -1023,7 +1023,7 @@ export const zModuleAttributeSchema = z.object({
label: z.string(),
isApplicableToGroup: z.boolean(),
isNullable: z.boolean(),
defaultValue: z.union([z.object({}), z.null()]),
defaultValue: z.union([z.unknown(), z.null()]),
description: z.string(),
typeId: z.int(),
id: z.int(),
@ -1341,7 +1341,7 @@ export const zUpdateAttributeSchema = z.object({
label: z.optional(z.union([z.string(), z.null()])),
isApplicableToGroup: z.optional(z.union([z.boolean(), z.null()])),
isNullable: z.optional(z.union([z.boolean(), z.null()])),
defaultValue: z.optional(z.union([z.object({}), z.null()])),
defaultValue: z.optional(z.union([z.unknown(), z.null()])),
description: z.optional(z.union([z.string(), z.null()])),
type: z.optional(z.union([zAttributeTypeSchema, z.null()])),
});
@ -1460,7 +1460,8 @@ export const zUpdateDealGroupResponse = z.object({
*/
export const zUpdateDealModuleAttributeSchema = z.object({
attributeId: z.int(),
value: z.union([z.object({}), z.null()]),
isApplicableToGroup: z.boolean(),
value: z.optional(z.union([z.unknown(), z.null()])),
});
/**