fix: fixed showing default option label
This commit is contained in:
@ -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);
|
||||
@ -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";
|
||||
|
||||
|
||||
@ -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}
|
||||
/>
|
||||
),
|
||||
},
|
||||
|
||||
@ -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;
|
||||
@ -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}
|
||||
/>
|
||||
);
|
||||
|
||||
@ -11,6 +11,8 @@ import PageBlock from "@/components/layout/PageBlock/PageBlock";
|
||||
const PageBody = () => {
|
||||
const { module } = useModuleEditorContext();
|
||||
|
||||
if (!module) return;
|
||||
|
||||
return (
|
||||
<Stack h={"100%"}>
|
||||
<PageBlock
|
||||
|
||||
@ -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
|
||||
);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -25,7 +25,7 @@ const useAttributesInnerTableColumns = () => {
|
||||
accessor: "type.name",
|
||||
render: attr =>
|
||||
attr.type.type === "select"
|
||||
? `Выбор "${attr.label}"`
|
||||
? `Выбор "${attr.select?.label}"`
|
||||
: attr.type.name,
|
||||
},
|
||||
{
|
||||
|
||||
@ -12,9 +12,8 @@ type Props = {
|
||||
};
|
||||
|
||||
const AttributeDefaultValue: FC<Props> = ({ attribute }) => {
|
||||
if (!attribute.defaultValue) return <>-</>;
|
||||
if (!attribute.defaultValue && !attribute.defaultOption) return <>-</>;
|
||||
const value = attribute.defaultValue;
|
||||
if (value === null) return <>-</>;
|
||||
|
||||
const type = attribute.type.type;
|
||||
if (type === "datetime") {
|
||||
@ -28,6 +27,9 @@ const AttributeDefaultValue: FC<Props> = ({ attribute }) => {
|
||||
if (type === "bool") {
|
||||
return value ? <IconCheck /> : <IconX />;
|
||||
}
|
||||
if (type === "select") {
|
||||
return attribute.defaultOption?.label;
|
||||
}
|
||||
|
||||
return <>{value}</>;
|
||||
};
|
||||
|
||||
@ -266,9 +266,9 @@ export const mergeHeaders = (
|
||||
delete mergedHeaders[key];
|
||||
} else if (Array.isArray(value)) {
|
||||
for (const v of value) {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error
|
||||
mergedHeaders[key] = [
|
||||
...(mergedHeaders[key] ?? []) as any,
|
||||
...(mergedHeaders[key] ?? []),
|
||||
v as string,
|
||||
];
|
||||
}
|
||||
|
||||
@ -22,10 +22,6 @@ export type AttrOptionSchema = {
|
||||
* Label
|
||||
*/
|
||||
label: string;
|
||||
/**
|
||||
* Value
|
||||
*/
|
||||
value: unknown;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -66,6 +62,10 @@ export type AttributeSchema = {
|
||||
* Defaultvalue
|
||||
*/
|
||||
defaultValue: unknown | null;
|
||||
/**
|
||||
* Defaultoptionid
|
||||
*/
|
||||
defaultOptionId?: number | null;
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
@ -87,21 +87,8 @@ export type AttributeSchema = {
|
||||
*/
|
||||
isBuiltIn: boolean;
|
||||
type: AttributeTypeSchema;
|
||||
select: AttributeSelectSchema | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* AttributeSelectSchema
|
||||
*/
|
||||
export type AttributeSelectSchema = {
|
||||
/**
|
||||
* Id
|
||||
*/
|
||||
id: number;
|
||||
/**
|
||||
* Label
|
||||
*/
|
||||
label: string;
|
||||
defaultOption?: AttrOptionSchema | null;
|
||||
select: AttrSelectSchema | null;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -345,6 +332,10 @@ export type CreateAttributeSchema = {
|
||||
* Defaultvalue
|
||||
*/
|
||||
defaultValue: unknown | null;
|
||||
/**
|
||||
* Defaultoptionid
|
||||
*/
|
||||
defaultOptionId?: number | null;
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
@ -1084,7 +1075,7 @@ export type DealModuleAttributeSchema = {
|
||||
*/
|
||||
value: unknown | null;
|
||||
type: AttributeTypeSchema;
|
||||
select: AttributeSelectSchema | null;
|
||||
select: AttrSelectSchema | null;
|
||||
/**
|
||||
* Defaultvalue
|
||||
*/
|
||||
@ -1847,6 +1838,10 @@ export type ModuleAttributeSchema = {
|
||||
* Defaultvalue
|
||||
*/
|
||||
defaultValue: unknown | null;
|
||||
/**
|
||||
* Defaultoptionid
|
||||
*/
|
||||
defaultOptionId?: number | null;
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
@ -1868,7 +1863,8 @@ export type ModuleAttributeSchema = {
|
||||
*/
|
||||
isBuiltIn: boolean;
|
||||
type: AttributeTypeSchema;
|
||||
select: AttributeSelectSchema | null;
|
||||
defaultOption?: AttrOptionSchema | null;
|
||||
select: AttrSelectSchema | null;
|
||||
/**
|
||||
* Originallabel
|
||||
*/
|
||||
@ -2454,6 +2450,10 @@ export type UpdateAttributeSchema = {
|
||||
* Defaultvalue
|
||||
*/
|
||||
defaultValue?: unknown | null;
|
||||
/**
|
||||
* Defaultoptionid
|
||||
*/
|
||||
defaultOptionId?: number | null;
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
@ -15,7 +15,6 @@ export const zAddAttributeResponse = z.object({
|
||||
export const zAttrOptionSchema = z.object({
|
||||
id: z.int(),
|
||||
label: z.string(),
|
||||
value: z.unknown(),
|
||||
});
|
||||
|
||||
/**
|
||||
@ -36,14 +35,6 @@ export const zAttributeTypeSchema = z.object({
|
||||
name: z.string(),
|
||||
});
|
||||
|
||||
/**
|
||||
* AttributeSelectSchema
|
||||
*/
|
||||
export const zAttributeSelectSchema = z.object({
|
||||
id: z.int(),
|
||||
label: z.string(),
|
||||
});
|
||||
|
||||
/**
|
||||
* AttributeSchema
|
||||
*/
|
||||
@ -52,13 +43,15 @@ export const zAttributeSchema = z.object({
|
||||
isApplicableToGroup: z.boolean(),
|
||||
isNullable: z.boolean(),
|
||||
defaultValue: z.union([z.unknown(), z.null()]),
|
||||
defaultOptionId: z.optional(z.union([z.int(), z.null()])),
|
||||
description: z.string(),
|
||||
typeId: z.int(),
|
||||
selectId: z.optional(z.union([z.int(), z.null()])),
|
||||
id: z.int(),
|
||||
isBuiltIn: z.boolean(),
|
||||
type: zAttributeTypeSchema,
|
||||
select: z.union([zAttributeSelectSchema, z.null()]),
|
||||
defaultOption: z.optional(z.union([zAttrOptionSchema, z.null()])),
|
||||
select: z.union([zAttrSelectSchema, z.null()]),
|
||||
});
|
||||
|
||||
/**
|
||||
@ -122,14 +115,14 @@ export const zBoardSchema = z.object({
|
||||
* Body_upload_product_barcode_image
|
||||
*/
|
||||
export const zBodyUploadProductBarcodeImage = z.object({
|
||||
upload_file: z.any(),
|
||||
upload_file: z.string(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Body_upload_product_image
|
||||
*/
|
||||
export const zBodyUploadProductImage = z.object({
|
||||
upload_file: z.any(),
|
||||
upload_file: z.string(),
|
||||
});
|
||||
|
||||
/**
|
||||
@ -162,6 +155,7 @@ export const zCreateAttributeSchema = z.object({
|
||||
isApplicableToGroup: z.boolean(),
|
||||
isNullable: z.boolean(),
|
||||
defaultValue: z.union([z.unknown(), z.null()]),
|
||||
defaultOptionId: z.optional(z.union([z.int(), z.null()])),
|
||||
description: z.string(),
|
||||
typeId: z.int(),
|
||||
selectId: z.optional(z.union([z.int(), z.null()])),
|
||||
@ -854,7 +848,7 @@ export const zDealModuleAttributeSchema = z.object({
|
||||
originalLabel: z.string(),
|
||||
value: z.union([z.unknown(), z.null()]),
|
||||
type: zAttributeTypeSchema,
|
||||
select: z.union([zAttributeSelectSchema, z.null()]),
|
||||
select: z.union([zAttrSelectSchema, z.null()]),
|
||||
defaultValue: z.unknown(),
|
||||
description: z.string(),
|
||||
isApplicableToGroup: z.boolean(),
|
||||
@ -1053,13 +1047,15 @@ export const zModuleAttributeSchema = z.object({
|
||||
isApplicableToGroup: z.boolean(),
|
||||
isNullable: z.boolean(),
|
||||
defaultValue: z.union([z.unknown(), z.null()]),
|
||||
defaultOptionId: z.optional(z.union([z.int(), z.null()])),
|
||||
description: z.string(),
|
||||
typeId: z.int(),
|
||||
selectId: z.optional(z.union([z.int(), z.null()])),
|
||||
id: z.int(),
|
||||
isBuiltIn: z.boolean(),
|
||||
type: zAttributeTypeSchema,
|
||||
select: z.union([zAttributeSelectSchema, z.null()]),
|
||||
defaultOption: z.optional(z.union([zAttrOptionSchema, z.null()])),
|
||||
select: z.union([zAttrSelectSchema, z.null()]),
|
||||
originalLabel: z.string(),
|
||||
});
|
||||
|
||||
@ -1389,6 +1385,7 @@ export const zUpdateAttributeSchema = z.object({
|
||||
isApplicableToGroup: z.optional(z.union([z.boolean(), z.null()])),
|
||||
isNullable: z.optional(z.union([z.boolean(), z.null()])),
|
||||
defaultValue: z.optional(z.union([z.unknown(), z.null()])),
|
||||
defaultOptionId: z.optional(z.union([z.int(), z.null()])),
|
||||
description: z.optional(z.union([z.string(), z.null()])),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user