mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix(frontend): apply feedback
This commit is contained in:
parent
969165f4fe
commit
74beb9426c
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import AddIcon from "@mui/icons-material/Add";
|
import AddIcon from "@mui/icons-material/Add";
|
||||||
import { Button } from "@mui/material";
|
import { Button } from "@mui/material";
|
||||||
import { FC, Fragment, useEffect } from "react";
|
import { FC, Fragment, useMemo } from "react";
|
||||||
import { useFieldArray, useForm } from "react-hook-form";
|
import { useFieldArray, useForm } from "react-hook-form";
|
||||||
|
|
||||||
import { ContentContainer, ContentItem } from "@/app-components/dialogs";
|
import { ContentContainer, ContentItem } from "@/app-components/dialogs";
|
||||||
@ -19,30 +19,45 @@ import { useToast } from "@/hooks/useToast";
|
|||||||
import { useTranslate } from "@/hooks/useTranslate";
|
import { useTranslate } from "@/hooks/useTranslate";
|
||||||
import { EntityType } from "@/services/types";
|
import { EntityType } from "@/services/types";
|
||||||
import { ComponentFormProps } from "@/types/common/dialogs.types";
|
import { ComponentFormProps } from "@/types/common/dialogs.types";
|
||||||
import { ContentFieldType, IContentType } from "@/types/content-type.types";
|
import {
|
||||||
|
ContentFieldType,
|
||||||
|
IContentType,
|
||||||
|
IContentTypeAttributes,
|
||||||
|
} from "@/types/content-type.types";
|
||||||
|
import { generateId } from "@/utils/generateId";
|
||||||
|
|
||||||
import { FieldInput } from "./components/FieldInput";
|
import { FieldInput } from "./components/FieldInput";
|
||||||
import { FIELDS_FORM_DEFAULT_VALUES, READ_ONLY_FIELDS } from "./constants";
|
import { FIELDS_FORM_DEFAULT_VALUES, READ_ONLY_FIELDS } from "./constants";
|
||||||
|
|
||||||
export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
|
export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
|
||||||
data: { defaultValues: contentType },
|
data: { defaultValues: contentTypeWithoutId },
|
||||||
Wrapper = Fragment,
|
Wrapper = Fragment,
|
||||||
WrapperProps,
|
WrapperProps,
|
||||||
...rest
|
...rest
|
||||||
}) => {
|
}) => {
|
||||||
|
const contentType = useMemo(
|
||||||
|
() =>
|
||||||
|
contentTypeWithoutId && {
|
||||||
|
...contentTypeWithoutId,
|
||||||
|
fields: contentTypeWithoutId?.fields?.map((field) => ({
|
||||||
|
...field,
|
||||||
|
uuid: generateId(),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
[contentTypeWithoutId],
|
||||||
|
);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const { t } = useTranslate();
|
const { t } = useTranslate();
|
||||||
const {
|
const {
|
||||||
reset,
|
|
||||||
control,
|
control,
|
||||||
register,
|
register,
|
||||||
setValue,
|
setValue,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
} = useForm<Partial<IContentType>>({
|
} = useForm<IContentType>({
|
||||||
defaultValues: {
|
defaultValues: contentType || {
|
||||||
name: contentType?.name || "",
|
name: "",
|
||||||
fields: contentType?.fields || FIELDS_FORM_DEFAULT_VALUES,
|
fields: FIELDS_FORM_DEFAULT_VALUES,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const { append, fields, remove } = useFieldArray({
|
const { append, fields, remove } = useFieldArray({
|
||||||
@ -67,17 +82,14 @@ export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
|
|||||||
EntityType.CONTENT_TYPE,
|
EntityType.CONTENT_TYPE,
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
const onSubmitForm = (params) => {
|
const onSubmitForm = (params: IContentTypeAttributes) => {
|
||||||
const labelCounts: Record<string, number> = params.fields.reduce(
|
const labelCounts = params.fields?.reduce((acc, field) => {
|
||||||
(acc, field) => {
|
|
||||||
if (!field.label.trim()) return acc;
|
if (!field.label.trim()) return acc;
|
||||||
acc[field.label] = (acc[field.label] || 0) + 1;
|
acc[field.label] = (acc[field.label] || 0) + 1;
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
},
|
}, {} as Record<string, number>);
|
||||||
{} as Record<string, number>,
|
const hasDuplicates = Object.values(labelCounts || {}).some(
|
||||||
);
|
|
||||||
const hasDuplicates = Object.values(labelCounts).some(
|
|
||||||
(count: number) => count > 1,
|
(count: number) => count > 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -87,24 +99,13 @@ export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contentType) {
|
if (contentType?.id) {
|
||||||
updateContentType({ id: contentType.id, params });
|
updateContentType({ id: contentType.id, params });
|
||||||
} else {
|
} else {
|
||||||
createContentType(params);
|
createContentType(params);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (contentType) {
|
|
||||||
reset({
|
|
||||||
name: contentType.name,
|
|
||||||
fields: contentType.fields || FIELDS_FORM_DEFAULT_VALUES,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
reset({ name: "", fields: FIELDS_FORM_DEFAULT_VALUES });
|
|
||||||
}
|
|
||||||
}, [contentType, reset]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Wrapper onSubmit={handleSubmit(onSubmitForm)} {...WrapperProps}>
|
<Wrapper onSubmit={handleSubmit(onSubmitForm)} {...WrapperProps}>
|
||||||
<form onSubmit={handleSubmit(onSubmitForm)}>
|
<form onSubmit={handleSubmit(onSubmitForm)}>
|
||||||
@ -130,12 +131,8 @@ export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
|
|||||||
gap={2}
|
gap={2}
|
||||||
>
|
>
|
||||||
<FieldInput
|
<FieldInput
|
||||||
defaultLabel={contentType?.fields?.[index]?.label}
|
{...{ index, remove, control, setValue }}
|
||||||
defaultName={contentType?.fields?.[index]?.name}
|
uuid={f.uuid}
|
||||||
setValue={setValue}
|
|
||||||
control={control}
|
|
||||||
remove={remove}
|
|
||||||
index={index}
|
|
||||||
disabled={READ_ONLY_FIELDS.includes(f.label as any)}
|
disabled={READ_ONLY_FIELDS.includes(f.label as any)}
|
||||||
/>
|
/>
|
||||||
</ContentItem>
|
</ContentItem>
|
||||||
@ -145,7 +142,11 @@ export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
|
|||||||
startIcon={<AddIcon />}
|
startIcon={<AddIcon />}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
append({ label: "", name: "", type: ContentFieldType.TEXT })
|
append({
|
||||||
|
label: "",
|
||||||
|
name: "",
|
||||||
|
type: ContentFieldType.TEXT,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{t("button.add")}
|
{t("button.add")}
|
||||||
|
@ -8,13 +8,11 @@
|
|||||||
|
|
||||||
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
|
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
|
||||||
import { MenuItem } from "@mui/material";
|
import { MenuItem } from "@mui/material";
|
||||||
import { useEffect } from "react";
|
|
||||||
import {
|
import {
|
||||||
Control,
|
Control,
|
||||||
Controller,
|
Controller,
|
||||||
UseFieldArrayRemove,
|
UseFieldArrayRemove,
|
||||||
UseFormSetValue,
|
UseFormSetValue,
|
||||||
useWatch,
|
|
||||||
} from "react-hook-form";
|
} from "react-hook-form";
|
||||||
|
|
||||||
import { IconButton } from "@/app-components/buttons/IconButton";
|
import { IconButton } from "@/app-components/buttons/IconButton";
|
||||||
@ -26,31 +24,17 @@ import { slugify } from "@/utils/string";
|
|||||||
export const FieldInput = ({
|
export const FieldInput = ({
|
||||||
setValue,
|
setValue,
|
||||||
index,
|
index,
|
||||||
defaultLabel,
|
uuid,
|
||||||
defaultName,
|
|
||||||
...props
|
...props
|
||||||
}: {
|
}: {
|
||||||
index: number;
|
index: number;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
remove: UseFieldArrayRemove;
|
remove: UseFieldArrayRemove;
|
||||||
control: Control<Partial<IContentType>>;
|
control: Control<IContentType>;
|
||||||
setValue: UseFormSetValue<Partial<IContentType>>;
|
setValue: UseFormSetValue<IContentType>;
|
||||||
defaultLabel?: string;
|
uuid?: string;
|
||||||
defaultName?: string;
|
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslate();
|
const { t } = useTranslate();
|
||||||
const label = useWatch({
|
|
||||||
control: props.control,
|
|
||||||
name: `fields.${index}.label`,
|
|
||||||
});
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (defaultLabel && defaultName !== slugify(defaultLabel)) {
|
|
||||||
defaultName && setValue(`fields.${index}.name`, defaultName);
|
|
||||||
} else {
|
|
||||||
setValue(`fields.${index}.name`, label ? slugify(label) : "");
|
|
||||||
}
|
|
||||||
}, [label, setValue, index]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -75,6 +59,24 @@ export const FieldInput = ({
|
|||||||
label={t("label.label")}
|
label={t("label.label")}
|
||||||
error={!!fieldState.error}
|
error={!!fieldState.error}
|
||||||
helperText={fieldState.error?.message}
|
helperText={fieldState.error?.message}
|
||||||
|
onChange={(e) => {
|
||||||
|
const currentValue = e.target.value;
|
||||||
|
const { label, name } =
|
||||||
|
props.control._defaultValues.fields?.find(
|
||||||
|
(field) => field?.uuid === uuid,
|
||||||
|
) || {};
|
||||||
|
|
||||||
|
if (label && name !== slugify(label)) {
|
||||||
|
name && setValue(`fields.${index}.name`, name);
|
||||||
|
} else {
|
||||||
|
setValue(
|
||||||
|
`fields.${index}.name`,
|
||||||
|
currentValue ? slugify(currentValue) : "",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
field.onChange(e);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2024 Hexastack. All rights reserved.
|
* Copyright © 2025 Hexastack. All rights reserved.
|
||||||
*
|
*
|
||||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||||
@ -23,6 +23,7 @@ export type ContentField = {
|
|||||||
name: string;
|
name: string;
|
||||||
label: string;
|
label: string;
|
||||||
type: ContentFieldType;
|
type: ContentFieldType;
|
||||||
|
uuid?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface IContentTypeAttributes {
|
export interface IContentTypeAttributes {
|
||||||
|
Loading…
Reference in New Issue
Block a user