diff --git a/frontend/app/components/Client/Sites/NewSiteForm.tsx b/frontend/app/components/Client/Sites/NewSiteForm.tsx index 47eecdfc3..fdb380e75 100644 --- a/frontend/app/components/Client/Sites/NewSiteForm.tsx +++ b/frontend/app/components/Client/Sites/NewSiteForm.tsx @@ -38,14 +38,15 @@ const NewSiteForm = ({ location: { pathname }, onClose }: Props) => { const onSubmit = (e: FormEvent) => { e.preventDefault(); - if (site?.id && site.exists()) { + if (!projectsStore.instance) return; + if (projectsStore.instance.id && projectsStore.instance.exists()) { projectsStore - .updateProject(site.id, site.toData()) + .updateProject(projectsStore.instance.id, projectsStore.instance.toData()) .then((response: any) => { if (!response || !response.errors || response.errors.size === 0) { onClose(null); if (!pathname.includes('onboarding')) { - fetchList(); + void fetchList(); } toast.success('Project updated successfully'); } else { @@ -53,7 +54,7 @@ const NewSiteForm = ({ location: { pathname }, onClose }: Props) => { } }); } else { - saveProject(site!).then((response: any) => { + saveProject(projectsStore.instance!).then((response: any) => { if (!response || !response.errors || response.errors.size === 0) { onClose(null); searchStore.clearSearch(); diff --git a/frontend/app/mstore/projectsStore.ts b/frontend/app/mstore/projectsStore.ts index 843853f3e..c9e1703b2 100644 --- a/frontend/app/mstore/projectsStore.ts +++ b/frontend/app/mstore/projectsStore.ts @@ -55,13 +55,7 @@ export default class ProjectsStore { editInstance = (instance: Partial) => { if (!this.instance) return; - Object.keys(instance).forEach((key) => { - if (key in this.instance) { - this.instance[key] = instance[key]; - } else { - console.error(`Project: Unknown key ${key}`); - } - }) + this.instance = this.instance.edit(instance); } fetchGDPR = async (siteId: string) => { @@ -189,7 +183,7 @@ export default class ProjectsStore { } catch (error) { console.error('Failed to update site:', error); } finally { - this.setLoading + this.setLoading(false) } } } diff --git a/frontend/app/mstore/types/project.ts b/frontend/app/mstore/types/project.ts index 5e496e192..f3dc75f95 100644 --- a/frontend/app/mstore/types/project.ts +++ b/frontend/app/mstore/types/project.ts @@ -1,4 +1,4 @@ -import { makeAutoObservable } from 'mobx'; +import { makeAutoObservable, runInAction } from 'mobx'; import GDPR from './gdpr'; export default class Project { @@ -27,7 +27,7 @@ export default class Project { exists = () => { return !!this.id; - } + }; get validate() { return this.name.length > 0; @@ -40,8 +40,9 @@ export default class Project { } else { console.error(`Project: Unknown key ${key}`); } - }) - } + }); + return this; + }; toData = () => { return { @@ -60,5 +61,5 @@ export default class Project { sampleRate: this.sampleRate, conditionsCount: this.conditionsCount, }; - } + }; }