From 9a92e5a3d0f502374adda3c987a86dcb779cff53 Mon Sep 17 00:00:00 2001 From: sriram veeraghanta Date: Tue, 12 Dec 2023 20:51:09 +0530 Subject: [PATCH] fix: pages fixes --- web/store/page.store.ts | 42 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/web/store/page.store.ts b/web/store/page.store.ts index b27beec86..b787f742b 100644 --- a/web/store/page.store.ts +++ b/web/store/page.store.ts @@ -1,5 +1,6 @@ import { action, computed, makeObservable, observable, runInAction } from "mobx"; import keyBy from "lodash/keyBy"; +import set from "lodash/set"; import isToday from "date-fns/isToday"; import isThisWeek from "date-fns/isThisWeek"; import isYesterday from "date-fns/isYesterday"; @@ -32,8 +33,8 @@ export class PageStore { constructor(_rootStore: RootStore) { makeObservable(this, { - pages: observable.ref, - archivedPages: observable.ref, + pages: observable, + archivedPages: observable, // computed projectPages: computed, favoriteProjectPages: computed, @@ -193,4 +194,41 @@ export class PageStore { throw error; } }; + + /** + * Creates a new page using the api and updated the local state in store + * @param workspaceSlug + * @param projectId + * @param data + */ + createPage = async (workspaceSlug: string, projectId: string, data: Partial) => { + const response = await this.pageService.createPage(workspaceSlug, projectId, data); + runInAction(() => { + this.pages = set(this.pages, [response.id], response); + }); + }; + + /** + * updates the page using the api and updates the local state in store + * @param workspaceSlug + * @param projectId + * @param pageId + * @param data + * @returns + */ + updatePage = async (workspaceSlug: string, projectId: string, pageId: string, data: Partial) => { + const originalPage = this.pages[pageId]; + try { + runInAction(() => { + this.pages[pageId] = { ...originalPage, ...data }; + }); + const response = await this.pageService.patchPage(workspaceSlug, projectId, pageId, data); + return response; + } catch (error) { + runInAction(() => { + this.pages[pageId] = originalPage; + }); + throw error; + } + }; }