fix: fixed showing default option label

This commit is contained in:
2025-11-03 10:42:13 +04:00
parent 03be3903cb
commit 311210394f
14 changed files with 98 additions and 60 deletions

View File

@ -1,11 +1,11 @@
import { useEffect, useState } from "react";
import { omit } from "lodash";
import useAttributeOptionsList from "@/app/module-editor/[moduleId]/components/shared/AttrOptionSelect/useAttributeOptionsList";
import ObjectSelect from "@/components/selects/ObjectSelect/ObjectSelect";
import useAttributeOptionsList from "@/hooks/lists/useAttributeOptionsList";
import { AttrOptionSchema } from "@/lib/client";
type Props = {
value: any;
value: number;
onChange: (val: any) => void;
selectId: number;
error?: string;
@ -22,7 +22,7 @@ const AttrOptionSelect = (props: Props) => {
setSelectedOption(undefined);
return;
}
setSelectedOption(options.find(option => option.value === props.value));
setSelectedOption(options.find(option => option.id === props.value));
}, [props.value, options]);
const restProps = omit(props, ["value, onChange", "selectId"]);
@ -35,7 +35,7 @@ const AttrOptionSelect = (props: Props) => {
value={selectedOption}
onChange={option => {
setSelectedOption(option);
props.onChange(option.value);
props.onChange(option.id);
}}
onClear={() => {
setSelectedOption(undefined);

View File

@ -1,7 +1,7 @@
import { CSSProperties, FC, JSX } from "react";
import { Checkbox, NumberInput, TextInput } from "@mantine/core";
import { DatePickerInput, DateTimePicker } from "@mantine/dates";
import AttrOptionSelect from "@/app/module-editor/[moduleId]/components/shared/AttrOptionSelect/AttrOptionSelect";
import AttrOptionSelect from "@/app/deals/drawers/DealEditorDrawer/components/AttrOptionSelect";
import { DealModuleAttributeSchema } from "@/lib/client";
import { naiveDateTimeStringToUtc } from "@/utils/datetime";

View File

@ -1,19 +0,0 @@
import { useQuery } from "@tanstack/react-query";
import { getAttrSelectOptionsOptions } from "@/lib/client/@tanstack/react-query.gen";
type Props = {
selectId: number;
};
const useAttributeSelectsList = ({ selectId }: Props) => {
const { data, refetch } = useQuery(
getAttrSelectOptionsOptions({ path: { selectId } })
);
return {
options: data?.items ?? [],
refetch,
};
};
export default useAttributeSelectsList;

View File

@ -3,14 +3,14 @@
import { useMemo } from "react";
import { DataTableColumn } from "mantine-datatable";
import { Center } from "@mantine/core";
import { useAttributesContext } from "@/app/attributes/contexts/AttributesContext";
import AttributeTableActions from "@/app/module-editor/[moduleId]/components/shared/AttributeTableActions/AttributeTableActions";
import { useModuleEditorContext } from "@/app/module-editor/[moduleId]/contexts/ModuleEditorContext";
import useIsMobile from "@/hooks/utils/useIsMobile";
import { AttributeSchema } from "@/lib/client";
const useAttributesTableColumns = () => {
const isMobile = useIsMobile();
const { attributesActions } = useAttributesContext();
const { attributeActions } = useModuleEditorContext();
return useMemo(
() =>
@ -24,7 +24,7 @@ const useAttributesTableColumns = () => {
accessor: "type.name",
render: attr =>
attr.type.type === "select"
? `Выбор "${attr.label}"`
? `Выбор "${attr.select?.label}"`
: attr.type.name,
},
{
@ -34,7 +34,7 @@ const useAttributesTableColumns = () => {
render: attribute => (
<AttributeTableActions
attribute={attribute}
{...attributesActions}
{...attributeActions}
/>
),
},

View File

@ -0,0 +1,29 @@
import ObjectSelect from "@/components/selects/ObjectSelect/ObjectSelect";
import useAttributeOptionsList from "@/hooks/lists/useAttributeOptionsList";
import { AttrOptionSchema } from "@/lib/client";
type Props = {
value?: AttrOptionSchema | null;
onChange: (val: AttrOptionSchema | null) => void;
selectId: number;
error?: string;
label?: string;
};
const DefaultAttrOptionSelect = ({ selectId, ...props }: Props) => {
const { options } = useAttributeOptionsList({ selectId });
return (
<ObjectSelect
label={"Значение"}
{...props}
data={options}
onClear={() => props.onChange(null)}
getLabelFn={(option: AttrOptionSchema) => option.label}
clearable
searchable
/>
);
};
export default DefaultAttrOptionSelect;

View File

@ -1,7 +1,7 @@
import { Checkbox, NumberInput, TextInput } from "@mantine/core";
import { DatePickerInput, DateTimePicker } from "@mantine/dates";
import { UseFormReturnType } from "@mantine/form";
import AttrOptionSelect from "@/app/module-editor/[moduleId]/components/shared/AttrOptionSelect/AttrOptionSelect";
import DefaultAttrOptionSelect from "@/app/module-editor/[moduleId]/components/shared/DefaultAttrOptionSelect/DefaultAttrOptionSelect";
import { AttributeSchema } from "@/lib/client";
import { naiveDateTimeStringToUtc } from "@/utils/datetime";
@ -91,11 +91,14 @@ const DefaultAttributeValueInput = ({ form }: Props) => {
} else if (type === "select") {
if (!form.values.select?.id) return <></>;
return (
<AttrOptionSelect
<DefaultAttrOptionSelect
label={"Значение по умолчанию"}
{...form.getInputProps("defaultValue")}
value={form.values.defaultValue}
onChange={value => form.setFieldValue("defaultValue", value)}
{...form.getInputProps("defaultOption")}
value={form.values.defaultOption}
onChange={value => {
form.setFieldValue("defaultOption", value);
form.setFieldValue("defaultOptionId", value?.id);
}}
selectId={form.values.select?.id}
/>
);

View File

@ -11,6 +11,8 @@ import PageBlock from "@/components/layout/PageBlock/PageBlock";
const PageBody = () => {
const { module } = useModuleEditorContext();
if (!module) return;
return (
<Stack h={"100%"}>
<PageBlock

View File

@ -67,11 +67,10 @@ const useAttributesCrud = ({
_id?: string;
path?: { moduleId?: number };
};
const isMatch =
return (
key?._id === "getDealModuleAttributes" &&
key?.path?.moduleId === module.id;
if (isMatch) console.log(isMatch);
return isMatch;
key?.path?.moduleId === module.id
);
},
});
};

View File

@ -44,13 +44,19 @@ const AttributeEditorModal = ({
isApplicableToGroup: false,
isNullable: false,
defaultValue: null,
defaultOptionId: null,
defaultOption: null,
description: "",
} as Partial<AttributeSchema>),
validate: {
label: label => !label?.trim() && "Название не заполнено",
type: type => !type && "Тип атрибута не выбран",
defaultValue: (defaultValue, values) => {
if (defaultValue === null && !values.isNullable) {
isNullable: (isNullable, values) => {
if (
values.defaultValue === null &&
values.defaultOption === null &&
!isNullable
) {
return "Укажите значение по умолчанию или разрешите пустое значение.";
}
return false;

View File

@ -25,7 +25,7 @@ const useAttributesInnerTableColumns = () => {
accessor: "type.name",
render: attr =>
attr.type.type === "select"
? `Выбор "${attr.label}"`
? `Выбор "${attr.select?.label}"`
: attr.type.name,
},
{