[WEB-4852] chore: views refactor (#7729)
* chore: refactored view store and services * chore: removed unused import * chore: refactored update view component * fix: lint errors
This commit is contained in:
parent
8bf059535a
commit
85f23b450d
21 changed files with 156 additions and 473 deletions
|
|
@ -5,7 +5,7 @@ import set from "lodash/set";
|
|||
import { observable, action, makeObservable, runInAction, computed } from "mobx";
|
||||
import { computedFn } from "mobx-utils";
|
||||
import { EIssueFilterType } from "@plane/constants";
|
||||
import { EViewAccess, IIssueFilterOptions, IWorkspaceView } from "@plane/types";
|
||||
import { IIssueFilterOptions, IWorkspaceView } from "@plane/types";
|
||||
// constants
|
||||
// services
|
||||
import { WorkspaceService } from "@/plane-web/services";
|
||||
|
|
@ -50,15 +50,18 @@ export class GlobalViewStore implements IGlobalViewStore {
|
|||
// actions
|
||||
fetchAllGlobalViews: action,
|
||||
fetchGlobalViewDetails: action,
|
||||
createGlobalView: action,
|
||||
updateGlobalView: action,
|
||||
deleteGlobalView: action,
|
||||
updateGlobalView: action,
|
||||
createGlobalView: action,
|
||||
});
|
||||
|
||||
// root store
|
||||
this.rootStore = _rootStore;
|
||||
// services
|
||||
this.workspaceService = new WorkspaceService();
|
||||
|
||||
this.createGlobalView = this.createGlobalView.bind(this);
|
||||
this.updateGlobalView = this.updateGlobalView.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -130,18 +133,19 @@ export class GlobalViewStore implements IGlobalViewStore {
|
|||
* @param workspaceSlug
|
||||
* @param data
|
||||
*/
|
||||
createGlobalView = async (workspaceSlug: string, data: Partial<IWorkspaceView>): Promise<IWorkspaceView> => {
|
||||
const response = await this.workspaceService.createView(workspaceSlug, data);
|
||||
runInAction(() => {
|
||||
set(this.globalViewMap, response.id, response);
|
||||
});
|
||||
async createGlobalView(workspaceSlug: string, data: Partial<IWorkspaceView>) {
|
||||
try {
|
||||
const response = await this.workspaceService.createView(workspaceSlug, data);
|
||||
runInAction(() => {
|
||||
set(this.globalViewMap, response.id, response);
|
||||
});
|
||||
|
||||
if (data.access === EViewAccess.PRIVATE) {
|
||||
await this.updateViewAccess(workspaceSlug, response.id, EViewAccess.PRIVATE);
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description update global view
|
||||
|
|
@ -149,11 +153,11 @@ export class GlobalViewStore implements IGlobalViewStore {
|
|||
* @param viewId
|
||||
* @param data
|
||||
*/
|
||||
updateGlobalView = async (
|
||||
async updateGlobalView(
|
||||
workspaceSlug: string,
|
||||
viewId: string,
|
||||
data: Partial<IWorkspaceView>
|
||||
): Promise<IWorkspaceView | undefined> => {
|
||||
): Promise<IWorkspaceView | undefined> {
|
||||
const currentViewData = this.getViewDetailsById(viewId) ? cloneDeep(this.getViewDetailsById(viewId)) : undefined;
|
||||
try {
|
||||
Object.keys(data).forEach((key) => {
|
||||
|
|
@ -161,14 +165,7 @@ export class GlobalViewStore implements IGlobalViewStore {
|
|||
set(this.globalViewMap, [viewId, currentKey], data[currentKey]);
|
||||
});
|
||||
|
||||
const promiseRequests = [];
|
||||
promiseRequests.push(this.workspaceService.updateView(workspaceSlug, viewId, data));
|
||||
|
||||
if (data.access !== undefined && data.access !== currentViewData?.access) {
|
||||
promiseRequests.push(this.updateViewAccess(workspaceSlug, viewId, data.access));
|
||||
}
|
||||
|
||||
const [currentView] = await Promise.all(promiseRequests);
|
||||
const currentView = await this.workspaceService.updateView(workspaceSlug, viewId, data);
|
||||
|
||||
// applying the filters in the global view
|
||||
if (!isEqual(currentViewData?.filters || {}, currentView?.filters || {})) {
|
||||
|
|
@ -205,7 +202,7 @@ export class GlobalViewStore implements IGlobalViewStore {
|
|||
if (currentViewData) set(this.globalViewMap, [viewId, currentKey], currentViewData[currentKey]);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description delete global view
|
||||
|
|
@ -218,73 +215,4 @@ export class GlobalViewStore implements IGlobalViewStore {
|
|||
delete this.globalViewMap[viewId];
|
||||
});
|
||||
});
|
||||
|
||||
/** Locks view
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @returns
|
||||
*/
|
||||
lockView = async (workspaceSlug: string, viewId: string) => {
|
||||
try {
|
||||
const currentView = this.getViewDetailsById(viewId);
|
||||
if (currentView?.is_locked) return;
|
||||
runInAction(() => {
|
||||
set(this.globalViewMap, [viewId, "is_locked"], true);
|
||||
});
|
||||
await this.workspaceService.lockView(workspaceSlug, viewId);
|
||||
} catch (error) {
|
||||
console.error("Failed to lock the view in view store", error);
|
||||
runInAction(() => {
|
||||
set(this.globalViewMap, [viewId, "is_locked"], false);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* unlocks View
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @returns
|
||||
*/
|
||||
unLockView = async (workspaceSlug: string, viewId: string) => {
|
||||
try {
|
||||
const currentView = this.getViewDetailsById(viewId);
|
||||
if (!currentView?.is_locked) return;
|
||||
runInAction(() => {
|
||||
set(this.globalViewMap, [viewId, "is_locked"], false);
|
||||
});
|
||||
await this.workspaceService.unLockView(workspaceSlug, viewId);
|
||||
} catch (error) {
|
||||
console.error("Failed to unlock view in view store", error);
|
||||
runInAction(() => {
|
||||
set(this.globalViewMap, [viewId, "is_locked"], true);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates View access
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @param access
|
||||
* @returns
|
||||
*/
|
||||
updateViewAccess = async (workspaceSlug: string, viewId: string, access: EViewAccess) => {
|
||||
const currentView = this.getViewDetailsById(viewId);
|
||||
const currentAccess = currentView?.access;
|
||||
try {
|
||||
runInAction(() => {
|
||||
set(this.globalViewMap, [viewId, "access"], access);
|
||||
});
|
||||
await this.workspaceService.updateViewAccess(workspaceSlug, viewId, access);
|
||||
} catch (error) {
|
||||
console.error("Failed to update Access for view", error);
|
||||
runInAction(() => {
|
||||
set(this.globalViewMap, [viewId, "access"], currentAccess);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { set } from "lodash";
|
|||
import { observable, action, makeObservable, runInAction, computed } from "mobx";
|
||||
import { computedFn } from "mobx-utils";
|
||||
// types
|
||||
import { EViewAccess, IProjectView, TPublishViewDetails, TPublishViewSettings, TViewFilters } from "@plane/types";
|
||||
import { IProjectView, TViewFilters } from "@plane/types";
|
||||
// constants
|
||||
// helpers
|
||||
import { getValidatedViewFilters, getViewName, orderViews, shouldFilterView } from "@plane/utils";
|
||||
|
|
@ -41,25 +41,6 @@ export interface IProjectViewStore {
|
|||
// favorites actions
|
||||
addViewToFavorites: (workspaceSlug: string, projectId: string, viewId: string) => Promise<any>;
|
||||
removeViewFromFavorites: (workspaceSlug: string, projectId: string, viewId: string) => Promise<any>;
|
||||
// publish
|
||||
publishView: (
|
||||
workspaceSlug: string,
|
||||
projectId: string,
|
||||
viewId: string,
|
||||
data: TPublishViewSettings
|
||||
) => Promise<TPublishViewDetails | undefined>;
|
||||
fetchPublishDetails: (
|
||||
workspaceSlug: string,
|
||||
projectId: string,
|
||||
viewId: string
|
||||
) => Promise<TPublishViewDetails | undefined>;
|
||||
updatePublishedView: (
|
||||
workspaceSlug: string,
|
||||
projectId: string,
|
||||
viewId: string,
|
||||
data: Partial<TPublishViewSettings>
|
||||
) => Promise<void>;
|
||||
unPublishView: (workspaceSlug: string, projectId: string, viewId: string) => Promise<void>;
|
||||
}
|
||||
|
||||
export class ProjectViewStore implements IProjectViewStore {
|
||||
|
|
@ -101,6 +82,9 @@ export class ProjectViewStore implements IProjectViewStore {
|
|||
this.rootStore = _rootStore;
|
||||
// services
|
||||
this.viewService = new ViewService();
|
||||
|
||||
this.createView = this.createView.bind(this);
|
||||
this.updateView = this.updateView.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -213,19 +197,15 @@ export class ProjectViewStore implements IProjectViewStore {
|
|||
* @param data
|
||||
* @returns Promise<IProjectView>
|
||||
*/
|
||||
createView = async (workspaceSlug: string, projectId: string, data: Partial<IProjectView>): Promise<IProjectView> => {
|
||||
async createView(workspaceSlug: string, projectId: string, data: Partial<IProjectView>): Promise<IProjectView> {
|
||||
const response = await this.viewService.createView(workspaceSlug, projectId, getValidatedViewFilters(data));
|
||||
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [response.id], response);
|
||||
});
|
||||
|
||||
if (data.access === EViewAccess.PRIVATE) {
|
||||
await this.updateViewAccess(workspaceSlug, projectId, response.id, EViewAccess.PRIVATE);
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a view details of specific view and updates it in the store
|
||||
|
|
@ -235,29 +215,22 @@ export class ProjectViewStore implements IProjectViewStore {
|
|||
* @param data
|
||||
* @returns Promise<IProjectView>
|
||||
*/
|
||||
updateView = async (
|
||||
async updateView(
|
||||
workspaceSlug: string,
|
||||
projectId: string,
|
||||
viewId: string,
|
||||
data: Partial<IProjectView>
|
||||
): Promise<IProjectView> => {
|
||||
): Promise<IProjectView> {
|
||||
const currentView = this.getViewById(viewId);
|
||||
|
||||
const promiseRequests = [];
|
||||
promiseRequests.push(this.viewService.patchView(workspaceSlug, projectId, viewId, data));
|
||||
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId], { ...currentView, ...data });
|
||||
});
|
||||
|
||||
if (data.access !== undefined && data.access !== currentView.access) {
|
||||
promiseRequests.push(this.updateViewAccess(workspaceSlug, projectId, viewId, data.access));
|
||||
}
|
||||
|
||||
const [response] = await Promise.all(promiseRequests);
|
||||
const response = await this.viewService.patchView(workspaceSlug, projectId, viewId, data);
|
||||
|
||||
return response;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a view and removes it from the viewMap object
|
||||
|
|
@ -275,75 +248,6 @@ export class ProjectViewStore implements IProjectViewStore {
|
|||
});
|
||||
};
|
||||
|
||||
/** Locks view
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @returns
|
||||
*/
|
||||
lockView = async (workspaceSlug: string, projectId: string, viewId: string) => {
|
||||
try {
|
||||
const currentView = this.getViewById(viewId);
|
||||
if (currentView?.is_locked) return;
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "is_locked"], true);
|
||||
});
|
||||
await this.viewService.lockView(workspaceSlug, projectId, viewId);
|
||||
} catch (error) {
|
||||
console.error("Failed to lock the view in view store", error);
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "is_locked"], false);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* unlocks View
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @returns
|
||||
*/
|
||||
unLockView = async (workspaceSlug: string, projectId: string, viewId: string) => {
|
||||
try {
|
||||
const currentView = this.getViewById(viewId);
|
||||
if (!currentView?.is_locked) return;
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "is_locked"], false);
|
||||
});
|
||||
await this.viewService.unLockView(workspaceSlug, projectId, viewId);
|
||||
} catch (error) {
|
||||
console.error("Failed to unlock view in view store", error);
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "is_locked"], true);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates View access
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @param access
|
||||
* @returns
|
||||
*/
|
||||
updateViewAccess = async (workspaceSlug: string, projectId: string, viewId: string, access: EViewAccess) => {
|
||||
const currentView = this.getViewById(viewId);
|
||||
const currentAccess = currentView?.access;
|
||||
try {
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "access"], access);
|
||||
});
|
||||
await this.viewService.updateViewAccess(workspaceSlug, projectId, viewId, access);
|
||||
} catch (error) {
|
||||
console.error("Failed to update Access for view", error);
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "access"], currentAccess);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a view to favorites
|
||||
* @param workspaceSlug
|
||||
|
|
@ -394,91 +298,4 @@ export class ProjectViewStore implements IProjectViewStore {
|
|||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Publishes View to the Public
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @returns
|
||||
*/
|
||||
publishView = async (workspaceSlug: string, projectId: string, viewId: string, data: TPublishViewSettings) => {
|
||||
try {
|
||||
const response = (await this.viewService.publishView(
|
||||
workspaceSlug,
|
||||
projectId,
|
||||
viewId,
|
||||
data
|
||||
)) as TPublishViewDetails;
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "anchor"], response?.anchor);
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error("Failed to publish view", error);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* fetches Published Details
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @returns
|
||||
*/
|
||||
fetchPublishDetails = async (workspaceSlug: string, projectId: string, viewId: string) => {
|
||||
try {
|
||||
const response = (await this.viewService.getPublishDetails(
|
||||
workspaceSlug,
|
||||
projectId,
|
||||
viewId
|
||||
)) as TPublishViewDetails;
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "anchor"], response?.anchor);
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch published view details", error);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* updates already published view
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @returns
|
||||
*/
|
||||
updatePublishedView = async (
|
||||
workspaceSlug: string,
|
||||
projectId: string,
|
||||
viewId: string,
|
||||
data: Partial<TPublishViewSettings>
|
||||
) => {
|
||||
try {
|
||||
return await this.viewService.updatePublishedView(workspaceSlug, projectId, viewId, data);
|
||||
} catch (error) {
|
||||
console.error("Failed to update published view details", error);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* un publishes the view
|
||||
* @param workspaceSlug
|
||||
* @param projectId
|
||||
* @param viewId
|
||||
* @returns
|
||||
*/
|
||||
unPublishView = async (workspaceSlug: string, projectId: string, viewId: string) => {
|
||||
try {
|
||||
const response = await this.viewService.unPublishView(workspaceSlug, projectId, viewId);
|
||||
runInAction(() => {
|
||||
set(this.viewMap, [viewId, "anchor"], null);
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error("Failed to unPublish view", error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import {
|
|||
TProjectMembership,
|
||||
} from "@plane/types";
|
||||
// plane web imports
|
||||
import { WorkspaceService } from "@/plane-web/services/workspace.service";
|
||||
import { WorkspaceService } from "@/plane-web/services";
|
||||
import type { RootStore } from "@/plane-web/store/root.store";
|
||||
// services
|
||||
import projectMemberService from "@/services/project/project-member.service";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue