diff --git a/web/components/issues/issue-layouts/filters/applied-filters/roots/global-view-root.tsx b/web/components/issues/issue-layouts/filters/applied-filters/roots/global-view-root.tsx index 09282743f3..7dc7fefb39 100644 --- a/web/components/issues/issue-layouts/filters/applied-filters/roots/global-view-root.tsx +++ b/web/components/issues/issue-layouts/filters/applied-filters/roots/global-view-root.tsx @@ -96,12 +96,13 @@ export const GlobalViewsAppliedFiltersRoot = observer((props: Props) => { ...(appliedFilters ?? {}), }, }).then((res) => { - captureEvent(GLOBAL_VIEW_UPDATED, { - view_id: res.id, - applied_filters: res.filters, - state: "SUCCESS", - element: "Spreadsheet view", - }); + if (res) + captureEvent(GLOBAL_VIEW_UPDATED, { + view_id: res.id, + applied_filters: res.filters, + state: "SUCCESS", + element: "Spreadsheet view", + }); }); }; diff --git a/web/components/workspace/views/modal.tsx b/web/components/workspace/views/modal.tsx index b9eee70040..15f94ced59 100644 --- a/web/components/workspace/views/modal.tsx +++ b/web/components/workspace/views/modal.tsx @@ -84,17 +84,19 @@ export const CreateUpdateWorkspaceViewModal: React.FC = observer((props) await updateGlobalView(workspaceSlug.toString(), data.id, payloadData) .then((res) => { - captureEvent(GLOBAL_VIEW_UPDATED, { - view_id: res.id, - applied_filters: res.filters, - state: "SUCCESS", - }); - setToast({ - type: TOAST_TYPE.SUCCESS, - title: "Success!", - message: "View updated successfully.", - }); - handleClose(); + if (res) { + captureEvent(GLOBAL_VIEW_UPDATED, { + view_id: res.id, + applied_filters: res.filters, + state: "SUCCESS", + }); + setToast({ + type: TOAST_TYPE.SUCCESS, + title: "Success!", + message: "View updated successfully.", + }); + handleClose(); + } }) .catch(() => { captureEvent(GLOBAL_VIEW_UPDATED, { diff --git a/web/store/global-view.store.ts b/web/store/global-view.store.ts index c6d3267845..742d7af328 100644 --- a/web/store/global-view.store.ts +++ b/web/store/global-view.store.ts @@ -1,11 +1,16 @@ -import { set } from "lodash"; +import cloneDeep from "lodash/cloneDeep"; +import isEmpty from "lodash/isEmpty"; +import isEqual from "lodash/isEqual"; +import set from "lodash/set"; import { observable, action, makeObservable, runInAction, computed } from "mobx"; import { computedFn } from "mobx-utils"; +import { IIssueFilterOptions, IWorkspaceView } from "@plane/types"; +// constants +import { EIssueFilterType } from "@/constants/issue"; // services import { WorkspaceService } from "@/services/workspace.service"; // types import { RootStore } from "@/store/root.store"; -import { IWorkspaceView } from "@plane/types"; export interface IGlobalViewStore { // observables @@ -20,7 +25,11 @@ export interface IGlobalViewStore { fetchGlobalViewDetails: (workspaceSlug: string, viewId: string) => Promise; // crud actions createGlobalView: (workspaceSlug: string, data: Partial) => Promise; - updateGlobalView: (workspaceSlug: string, viewId: string, data: Partial) => Promise; + updateGlobalView: ( + workspaceSlug: string, + viewId: string, + data: Partial + ) => Promise; deleteGlobalView: (workspaceSlug: string, viewId: string) => Promise; } @@ -139,14 +148,50 @@ export class GlobalViewStore implements IGlobalViewStore { workspaceSlug: string, viewId: string, data: Partial - ): Promise => - await this.workspaceService.updateView(workspaceSlug, viewId, data).then((response) => { - const viewToUpdate = { ...this.getViewDetailsById(viewId), ...data }; - runInAction(() => { - set(this.globalViewMap, viewId, viewToUpdate); + ): Promise => { + const currentViewData = this.getViewDetailsById(viewId) ? cloneDeep(this.getViewDetailsById(viewId)) : undefined; + try { + Object.keys(data).forEach((key) => { + const currentKey = key as keyof IWorkspaceView; + set(this.globalViewMap, [viewId, currentKey], data[currentKey]); }); - return response; - }); + const currentView = await this.workspaceService.updateView(workspaceSlug, viewId, data); + // applying the filters in the global view + if (!isEqual(currentViewData?.filters || {}, currentView?.filters || {})) { + if (isEmpty(currentView?.filters)) { + const currentGlobalViewFilters: IIssueFilterOptions = this.rootStore.issue.workspaceIssuesFilter.filters[ + viewId + ].filters as IIssueFilterOptions; + const newFilters: IIssueFilterOptions = {}; + Object.keys(currentGlobalViewFilters ?? {}).forEach((key) => { + newFilters[key as keyof IIssueFilterOptions] = []; + }); + await this.rootStore.issue.workspaceIssuesFilter.updateFilters( + workspaceSlug, + undefined, + EIssueFilterType.FILTERS, + newFilters, + viewId + ); + } else { + await this.rootStore.issue.workspaceIssuesFilter.updateFilters( + workspaceSlug, + undefined, + EIssueFilterType.FILTERS, + currentView.filters, + viewId + ); + } + this.rootStore.issue.workspaceIssues.fetchIssues(workspaceSlug, viewId, "mutation"); + } + return currentView; + } catch { + Object.keys(data).forEach((key) => { + const currentKey = key as keyof IWorkspaceView; + if (currentViewData) set(this.globalViewMap, [viewId, currentKey], currentViewData[currentKey]); + }); + } + }; /** * @description delete global view