From ea6a6df371dd78bdf5fc85359df8992439d70f06 Mon Sep 17 00:00:00 2001 From: AlexSserb Date: Tue, 28 Oct 2025 11:43:13 +0400 Subject: [PATCH] fix: applied timezone to default values, removed value nesting --- .../components/AttributeValueInput.tsx | 5 +-- .../tabs/CustomTab/CustomTab.tsx | 21 +++++++---- .../DefaultAttributeValueInput.tsx | 37 +++++++++++-------- .../hooks/useAttributesInnerTableColumns.tsx | 7 +++- src/lib/client/types.gen.ts | 32 ++++++---------- src/lib/client/zod.gen.ts | 15 ++++---- 6 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/app/deals/drawers/DealEditorDrawer/components/AttributeValueInput.tsx b/src/app/deals/drawers/DealEditorDrawer/components/AttributeValueInput.tsx index d6d160d..5a9f68b 100644 --- a/src/app/deals/drawers/DealEditorDrawer/components/AttributeValueInput.tsx +++ b/src/app/deals/drawers/DealEditorDrawer/components/AttributeValueInput.tsx @@ -26,11 +26,10 @@ const AttributeValueInput: FC = ({ }; const renderCheckbox = () => { - const localValue = value === undefined ? false : value; return ( onChange(e.currentTarget.checked)} /> ); @@ -80,7 +79,7 @@ const AttributeValueInput: FC = ({ onChange(Number(value))} /> ); diff --git a/src/app/deals/drawers/DealEditorDrawer/tabs/CustomTab/CustomTab.tsx b/src/app/deals/drawers/DealEditorDrawer/tabs/CustomTab/CustomTab.tsx index 6d547d5..f6adbce 100644 --- a/src/app/deals/drawers/DealEditorDrawer/tabs/CustomTab/CustomTab.tsx +++ b/src/app/deals/drawers/DealEditorDrawer/tabs/CustomTab/CustomTab.tsx @@ -23,8 +23,9 @@ type Props = { deal: DealSchema; }; -type Value = { +type AttrInfo = { value?: any; + isApplicableToGroup: boolean; }; const CustomTab: FC = ({ module, deal }) => { @@ -35,16 +36,19 @@ const CustomTab: FC = ({ module, deal }) => { }); const [attributeValuesMap, setAttributeValuesMap] = useState< - Map + Map >(new Map()); const [attributeErrorsMap, setAttributeErrorsMap] = useState< Map >(new Map()); useEffect(() => { - const values = new Map(); + const values = new Map(); 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 = ({ module, deal }) => { attributeValuesMap .entries() .map( - ([attributeId, value]) => + ([attributeId, info]) => ({ attributeId, - value, + ...info, }) as UpdateDealModuleAttributeSchema ) .toArray(); @@ -83,7 +87,10 @@ const CustomTab: FC = ({ 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)); diff --git a/src/app/module-editor/[moduleId]/components/shared/DefaultAttributeValueInput/DefaultAttributeValueInput.tsx b/src/app/module-editor/[moduleId]/components/shared/DefaultAttributeValueInput/DefaultAttributeValueInput.tsx index d067e36..70a3930 100644 --- a/src/app/module-editor/[moduleId]/components/shared/DefaultAttributeValueInput/DefaultAttributeValueInput.tsx +++ b/src/app/module-editor/[moduleId]/components/shared/DefaultAttributeValueInput/DefaultAttributeValueInput.tsx @@ -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>; @@ -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)) } /> ); diff --git a/src/app/modules/hooks/useAttributesInnerTableColumns.tsx b/src/app/modules/hooks/useAttributesInnerTableColumns.tsx index 7a2ddaa..ccb532b 100644 --- a/src/app/modules/hooks/useAttributesInnerTableColumns.tsx +++ b/src/app/modules/hooks/useAttributesInnerTableColumns.tsx @@ -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); diff --git a/src/lib/client/types.gen.ts b/src/lib/client/types.gen.ts index 6c7362a..b7d5918 100644 --- a/src/lib/client/types.gen.ts +++ b/src/lib/client/types.gen.ts @@ -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; }; /** diff --git a/src/lib/client/zod.gen.ts b/src/lib/client/zod.gen.ts index d0a7c12..4aa0e73 100644 --- a/src/lib/client/zod.gen.ts +++ b/src/lib/client/zod.gen.ts @@ -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()])), }); /**