fix: blockForm reset values

This commit is contained in:
yassinedorbozgithub 2024-11-26 12:18:27 +01:00
parent 3c5a45fca5
commit d4c3e59e62

View File

@ -35,7 +35,7 @@ import { useTranslate } from "@/hooks/useTranslate";
import { EntityType } from "@/services/types"; import { EntityType } from "@/services/types";
import { OutgoingMessageFormat } from "@/types/message.types"; import { OutgoingMessageFormat } from "@/types/message.types";
import { IBlockAttributes, IBlock } from "../../types/block.types"; import { IBlock, IBlockAttributes } from "../../types/block.types";
import BlockFormProvider from "./form/BlockFormProvider"; import BlockFormProvider from "./form/BlockFormProvider";
import { MessageForm } from "./form/MessageForm"; import { MessageForm } from "./form/MessageForm";
@ -69,24 +69,25 @@ const BlockDialog: FC<BlockDialogProps> = ({
toast.success(t("message.success_save")); toast.success(t("message.success_save"));
}, },
}); });
const methods = useForm<IBlockAttributes>({ const DEFAULT_VALUES = {
defaultValues: { name: block?.name || "",
name: block?.name || "", patterns: block?.patterns || [],
patterns: block?.patterns || [], trigger_labels: block?.trigger_labels || [],
trigger_labels: block?.trigger_labels || [], trigger_channels: block?.trigger_channels || [],
trigger_channels: block?.trigger_channels || [], options: block?.options || {
options: block?.options || { typing: 0,
typing: 0, content: {
content: { display: OutgoingMessageFormat.list,
display: OutgoingMessageFormat.list, top_element_style: "compact",
top_element_style: "compact", limit: 2,
limit: 2,
},
assignTo: block?.options?.assignTo,
}, },
assign_labels: block?.assign_labels || [], assignTo: block?.options?.assignTo,
message: block?.message || [""],
}, },
assign_labels: block?.assign_labels || [],
message: block?.message || [""],
} as IBlockAttributes;
const methods = useForm<IBlockAttributes>({
defaultValues: DEFAULT_VALUES,
}); });
const { const {
reset, reset,
@ -114,10 +115,8 @@ const BlockDialog: FC<BlockDialogProps> = ({
}, [open, reset]); }, [open, reset]);
useEffect(() => { useEffect(() => {
if (block) { if (block && open) {
reset({ reset(DEFAULT_VALUES);
name: block.name,
});
} else { } else {
reset(); reset();
} }