diff --git a/plugins/lime-plugin-mesh-wide-config/src/components/ConfigSection.tsx b/plugins/lime-plugin-mesh-wide-config/src/components/ConfigSection.tsx index 9551713c..d85324d9 100644 --- a/plugins/lime-plugin-mesh-wide-config/src/components/ConfigSection.tsx +++ b/plugins/lime-plugin-mesh-wide-config/src/components/ConfigSection.tsx @@ -91,28 +91,30 @@ export const SectionEditOrDelete = ({ name }) => { }; export const AddNewElementBtn = ({ sectionName }: { sectionName?: string }) => { - const { toggleModal: toggleNewSectionModal, actionModal: addSectionModal } = - useAddNewSectionModal(); const { watch, setValue } = useFormContext(); const section = watch(sectionName); - const { showToast } = useToast(); + const sectionAdded = (data) => { + if (!sectionName) { + setValue(data.name, {}); + } else { + const kaka = { ...section, [data.name]: "" }; + setValue(sectionName, kaka); + } + toggleNewSectionModal(); + showToast({ + text: Added section {data.name}, + }); + }; + + const { toggleModal: toggleNewSectionModal, actionModal: addSectionModal } = + useAddNewSectionModal(sectionAdded, sectionName); + return ( { - addSectionModal((data) => { - if (!sectionName) { - setValue(data.name, {}); - } else { - const kaka = { ...section, [data.name]: "" }; - setValue(sectionName, kaka); - } - toggleNewSectionModal(); - showToast({ - text: Added section {data.name}, - }); - }, sectionName); + addSectionModal(); }} /> ); diff --git a/plugins/lime-plugin-mesh-wide-config/src/components/OptionForm.tsx b/plugins/lime-plugin-mesh-wide-config/src/components/OptionForm.tsx index 53eafb6c..06bd917a 100644 --- a/plugins/lime-plugin-mesh-wide-config/src/components/OptionForm.tsx +++ b/plugins/lime-plugin-mesh-wide-config/src/components/OptionForm.tsx @@ -90,7 +90,7 @@ export const OptionContainer = ({ ); }; -const EditableField = ({ +export const EditableField = ({ isList, name, keyString, @@ -98,13 +98,13 @@ const EditableField = ({ }: { isList: boolean; name: string; - setIsEditing: StateUpdater; -} & Omit) => { + keyString?: string; + setIsEditing?: StateUpdater; +}) => { const { control, setValue, watch, getValues } = useFormContext(); - const { showToast } = useToast(); + // const { showToast } = useToast(); const value = watch(name); const [initialState] = useState(value); - // const currentValues = getValues(listName); const removeListItem = (index) => { const updatedValues = value.filter((_, i) => i !== index); @@ -162,32 +162,34 @@ const EditableField = ({ /> )} -
- - -
+ {setIsEditing && ( +
+ + +
+ )} ); }; diff --git a/plugins/lime-plugin-mesh-wide-config/src/components/modals.tsx b/plugins/lime-plugin-mesh-wide-config/src/components/modals.tsx index 73b95c74..4338d9dd 100644 --- a/plugins/lime-plugin-mesh-wide-config/src/components/modals.tsx +++ b/plugins/lime-plugin-mesh-wide-config/src/components/modals.tsx @@ -1,11 +1,15 @@ import { Trans } from "@lingui/macro"; +import { Label } from "@tanstack/react-query-devtools/build/lib/Explorer"; import { ComponentChildren } from "preact"; import { useCallback } from "preact/compat"; -import { useForm } from "react-hook-form"; +import { useEffect } from "preact/hooks"; +import { FormProvider, useForm } from "react-hook-form"; import { ModalActions, useModal } from "components/Modal/Modal"; import InputField from "components/inputs/InputField"; +import switchStyle from "components/switch"; +import { EditableField } from "plugins/lime-plugin-mesh-wide-config/src/components/OptionForm"; import { dataTypeNameMapping } from "plugins/lime-plugin-mesh-wide/src/lib/utils"; import { MeshWideMapDataTypeKeys } from "plugins/lime-plugin-mesh-wide/src/meshWideTypes"; @@ -52,40 +56,67 @@ export const useEditPropModal = () => "success" ); -export const useAddNewSectionModal = () => { - const { toggleModal, setModalState } = useModal(); +export const useAddNewSectionModal = ( + actionCb: (data) => void, + sectionName?: string +) => { + const { toggleModal, setModalState, isModalOpen, openModalKey } = + useModal(); + const modalKey = `addnewSectionModal${sectionName}`; + + const fmethods = useForm({ + defaultValues: { name: "", value: [""] }, + }); const { register, handleSubmit, + setValue, formState: { errors }, - } = useForm({ - defaultValues: { name: "" }, - }); + watch, + } = fmethods; + + const value = watch("value"); + console.log("values! ", value); + + const updateState = useCallback(() => { + let title = Add new section; + if (sectionName) { + title = Add new section for {sectionName}; + } + setModalState({ + content: ( + + Name} + register={register} + /> + {sectionName && ( + <> + + + + )} + + ), + title, + successCb: handleSubmit(actionCb), + successBtnText: Add, + }); + }, [handleSubmit, register, setModalState, toggleModal]); + + const actionModal = useCallback(() => { + updateState(); + toggleModal(modalKey); + }, [toggleModal, updateState]); + + // Update modal state with mutation result + useEffect(() => { + if (isModalOpen && openModalKey === modalKey) { + updateState(); + } + }, [value, isModalOpen, actionModal, openModalKey]); - const actionModal = useCallback( - (actionCb: (data) => void, sectionName?: string) => { - let title = Add new section; - if (sectionName) { - title = Add new section for {sectionName}; - } - setModalState({ - content: ( -
- Name} - register={register} - /> -
- ), - title, - successCb: handleSubmit(actionCb), - successBtnText: Add, - }); - toggleModal(); - }, - [handleSubmit, register, setModalState, toggleModal] - ); return { actionModal, toggleModal }; }; diff --git a/plugins/lime-plugin-mesh-wide-config/src/meshConfigPage.tsx b/plugins/lime-plugin-mesh-wide-config/src/meshConfigPage.tsx index deab7a26..7e74d303 100644 --- a/plugins/lime-plugin-mesh-wide-config/src/meshConfigPage.tsx +++ b/plugins/lime-plugin-mesh-wide-config/src/meshConfigPage.tsx @@ -1,6 +1,7 @@ import { useState } from "preact/hooks"; import React from "react"; +import { Button } from "components/buttons/button"; import WizardWrapper from "components/mesh-wide-wizard/WizardWrapper"; import EditConfiguration from "plugins/lime-plugin-mesh-wide-config/src/containers/EditConfiguration"; @@ -14,18 +15,18 @@ const MeshConfigPage = () => { return setShowEditConfig(false)} />; } - // return ; - return ( - - ); + return ; + // return ( + // + // ); }; export default MeshConfigPage;