plane/web/store/global-view.store.ts
rahulramesha b3ac9def8d
fix: issue property dropdown data flow (#3425)
* dev: workspace states and estimates

* refactor issue dropdown logic to help work properly with issues on global level

* fix: project labels response change

* fix label type

* change store computed actions to computed functions from mobx-utils

* fix: state response change

* chore: project and workspace state change

* fix state and label types

* chore: state and label serializer change

* modify state and label types

* fix dropdown reset on project id change

* fix label sort order

---------

Co-authored-by: pablohashescobar <nikhilschacko@gmail.com>
Co-authored-by: Rahul R <rahulr@Rahuls-MacBook-Pro.local>
Co-authored-by: NarayanBavisetti <narayan3119@gmail.com>
Co-authored-by: Rahul R <rahul.ramesha@plane.so>
2024-01-22 17:07:32 +05:30

163 lines
5.1 KiB
TypeScript

import { observable, action, makeObservable, runInAction, computed } from "mobx";
import { computedFn } from "mobx-utils";
import { set } from "lodash";
// services
import { WorkspaceService } from "services/workspace.service";
// types
import { RootStore } from "store/root.store";
import { IWorkspaceView } from "@plane/types";
export interface IGlobalViewStore {
// observables
globalViewMap: Record<string, IWorkspaceView>;
// computed
currentWorkspaceViews: string[] | null;
// computed actions
getSearchedViews: (searchQuery: string) => string[] | null;
getViewDetailsById: (viewId: string) => IWorkspaceView | null;
// fetch actions
fetchAllGlobalViews: (workspaceSlug: string) => Promise<IWorkspaceView[]>;
fetchGlobalViewDetails: (workspaceSlug: string, viewId: string) => Promise<IWorkspaceView>;
// crud actions
createGlobalView: (workspaceSlug: string, data: Partial<IWorkspaceView>) => Promise<IWorkspaceView>;
updateGlobalView: (workspaceSlug: string, viewId: string, data: Partial<IWorkspaceView>) => Promise<IWorkspaceView>;
deleteGlobalView: (workspaceSlug: string, viewId: string) => Promise<any>;
}
export class GlobalViewStore implements IGlobalViewStore {
// observables
globalViewMap: Record<string, IWorkspaceView> = {};
// root store
rootStore;
// services
workspaceService;
constructor(_rootStore: RootStore) {
makeObservable(this, {
// observables
globalViewMap: observable,
// computed
currentWorkspaceViews: computed,
// actions
fetchAllGlobalViews: action,
fetchGlobalViewDetails: action,
createGlobalView: action,
updateGlobalView: action,
deleteGlobalView: action,
});
// root store
this.rootStore = _rootStore;
// services
this.workspaceService = new WorkspaceService();
}
/**
* @description returns list of views for current workspace
*/
get currentWorkspaceViews() {
const currentWorkspaceDetails = this.rootStore.workspaceRoot.currentWorkspace;
if (!currentWorkspaceDetails) return null;
return (
Object.keys(this.globalViewMap ?? {})?.filter(
(viewId) => this.globalViewMap[viewId]?.workspace === currentWorkspaceDetails.id
) ?? null
);
}
/**
* @description returns list of views for current workspace based on search query
* @param searchQuery
* @returns
*/
getSearchedViews = computedFn((searchQuery: string) => {
const currentWorkspaceDetails = this.rootStore.workspaceRoot.currentWorkspace;
if (!currentWorkspaceDetails) return null;
return (
Object.keys(this.globalViewMap ?? {})?.filter(
(viewId) =>
this.globalViewMap[viewId]?.workspace === currentWorkspaceDetails.id &&
this.globalViewMap[viewId]?.name?.toLowerCase().includes(searchQuery.toLowerCase())
) ?? null
);
});
/**
* @description returns view details for given viewId
* @param viewId
*/
getViewDetailsById = computedFn((viewId: string): IWorkspaceView | null => this.globalViewMap[viewId] ?? null);
/**
* @description fetch all global views for given workspace
* @param workspaceSlug
*/
fetchAllGlobalViews = async (workspaceSlug: string): Promise<IWorkspaceView[]> =>
await this.workspaceService.getAllViews(workspaceSlug).then((response) => {
runInAction(() => {
response.forEach((view) => {
set(this.globalViewMap, view.id, view);
});
});
return response;
});
/**
* @description fetch view details for given viewId
* @param viewId
*/
fetchGlobalViewDetails = async (workspaceSlug: string, viewId: string): Promise<IWorkspaceView> =>
await this.workspaceService.getViewDetails(workspaceSlug, viewId).then((response) => {
runInAction(() => {
set(this.globalViewMap, viewId, response);
});
return response;
});
/**
* @description create new global view
* @param workspaceSlug
* @param data
*/
createGlobalView = async (workspaceSlug: string, data: Partial<IWorkspaceView>): Promise<IWorkspaceView> =>
await this.workspaceService.createView(workspaceSlug, data).then((response) => {
runInAction(() => {
set(this.globalViewMap, response.id, response);
});
return response;
});
/**
* @description update global view
* @param workspaceSlug
* @param viewId
* @param data
*/
updateGlobalView = async (
workspaceSlug: string,
viewId: string,
data: Partial<IWorkspaceView>
): Promise<IWorkspaceView> =>
await this.workspaceService.updateView(workspaceSlug, viewId, data).then((response) => {
const viewToUpdate = { ...this.getViewDetailsById(viewId), ...data };
runInAction(() => {
set(this.globalViewMap, viewId, viewToUpdate);
});
return response;
});
/**
* @description delete global view
* @param workspaceSlug
* @param viewId
*/
deleteGlobalView = async (workspaceSlug: string, viewId: string): Promise<any> =>
await this.workspaceService.deleteView(workspaceSlug, viewId).then(() => {
runInAction(() => {
delete this.globalViewMap[viewId];
});
});
}