From 23de2afc69cb362bde7d8288060b20d6ce3dd479 Mon Sep 17 00:00:00 2001 From: Moiz Adnan <67912355+MoizAdnan@users.noreply.github.com> Date: Tue, 11 Jun 2024 13:19:30 +0500 Subject: [PATCH] Fixed Project Settings table (#10360) * chore: Refactor project settings tab to use displayedSettings instead of updatedSettings * updated use effect * Fixed condition * Fixed lint * Added originalSettings * Fixed issue with setting array * Added refetch * Resolved comment --- .../components/settings/tabs/project.tsx | 54 ++++++++----------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/packages/client-core/src/admin/components/settings/tabs/project.tsx b/packages/client-core/src/admin/components/settings/tabs/project.tsx index 51324f7a76..bfd19cea90 100644 --- a/packages/client-core/src/admin/components/settings/tabs/project.tsx +++ b/packages/client-core/src/admin/components/settings/tabs/project.tsx @@ -50,7 +50,8 @@ const ProjectTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableRe const projectState = useMutableState(ProjectState) const projects = projectState.projects - const updatedSettings = useHookstate([]) + const displayedSettings = useHookstate([]) + const originalSettings = useHookstate([]) const selectedProjectId = useHookstate(projects.get(NO_PROXY).length > 0 ? projects.get(NO_PROXY)[0].id : '') const project = useGet(projectPath, selectedProjectId.value, { query: { $select: ['settings'] } }) @@ -62,30 +63,33 @@ const ProjectTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableRe }, []) useEffect(() => { - if (selectedProjectId.value) { - updatedSettings.set([]) + if (project.data && project.data.settings) { + originalSettings.set(JSON.parse(JSON.stringify(project.data.settings))) + displayedSettings.set(originalSettings.value.slice()) } - }, [selectedProjectId]) + }, [project]) const handleClear = () => { - updatedSettings.set([]) + displayedSettings.set(originalSettings.value.slice()) } const handleSubmit = async () => { try { state.loading.set(true) - for (const updatedSetting of updatedSettings.value) { - await patchProjectSetting( - updatedSetting.id, - { value: updatedSetting.value }, - { - query: { - projectId: selectedProjectId.value + for (const [index, displayedSetting] of displayedSettings.value.entries()) { + if (displayedSetting.value !== originalSettings.value[index].value) + await patchProjectSetting( + displayedSetting.id, + { value: displayedSetting.value }, + { + query: { + projectId: selectedProjectId.value + } } - } - ) + ) } state.set({ loading: false, errorMessage: '' }) + project.refetch() } catch (err) { state.set({ loading: false, errorMessage: err.message }) } @@ -98,22 +102,8 @@ const ProjectTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableRe } }) - const handleSettingsChange = (e: React.ChangeEvent, setting: ProjectSettingType) => { - const index = updatedSettings.value.findIndex((item) => item.id === setting.id) - if (index === -1) { - updatedSettings.set([...updatedSettings.value, { ...setting, value: e.target.value }]) - } else { - updatedSettings[index].nested('value').set(e.target.value) - } - } - - const displayedSettings = JSON.parse(JSON.stringify(project.data?.settings || [])) as ProjectSettingType[] - for (const updatedSetting of updatedSettings.value) { - const index = displayedSettings.findIndex((setting) => setting.id === updatedSetting.id) - - if (index !== -1) { - displayedSettings[index] = updatedSetting - } + const handleSettingsChange = (e: React.ChangeEvent, setting: ProjectSettingType, index: number) => { + displayedSettings[index].set({ ...setting, value: e.target.value }) } return ( @@ -135,7 +125,7 @@ const ProjectTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableRe {displayedSettings.length > 0 ? ( <> - {displayedSettings.map((setting: ProjectSettingType, index: number) => ( + {displayedSettings.value.map((setting: ProjectSettingType, index: number) => (
) } - onChange={(e) => handleSettingsChange(e, setting)} + onChange={(e) => handleSettingsChange(e, setting, index)} />
))}