plane/web/store/view/view-root.store.ts

101 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-01-30 11:15:14 +00:00
import { action, computed, makeObservable, observable, runInAction } from "mobx";
2024-02-02 08:22:38 +00:00
import set from "lodash/set";
2024-01-30 08:56:59 +00:00
// stores
import { RootStore } from "store/root.store";
2024-02-02 08:22:38 +00:00
import { ViewStore } from "./view.store";
2024-01-30 08:56:59 +00:00
// types
import { TViewService } from "services/view/types";
import { TView } from "@plane/types";
export type TLoader = "" | undefined;
2024-02-02 08:22:38 +00:00
type TViewRootStore = {
2024-01-30 08:56:59 +00:00
// observables
2024-02-02 08:22:38 +00:00
viewMap: Record<string, ViewStore>;
2024-01-30 08:56:59 +00:00
// computed
viewIds: string[];
2024-02-02 08:22:38 +00:00
// helper actions
viewById: (viewId: string) => ViewStore | undefined;
2024-01-30 08:56:59 +00:00
// actions
fetch: () => Promise<void>;
create: (view: Partial<TView>) => Promise<void>;
remove: (viewId: string) => Promise<void>;
2024-01-30 08:56:59 +00:00
duplicate: (viewId: string) => Promise<void>;
};
2024-02-02 08:22:38 +00:00
export class ViewRootStore implements TViewRootStore {
viewMap: Record<string, ViewStore> = {};
2024-01-30 08:56:59 +00:00
constructor(private store: RootStore, private service: TViewService) {
makeObservable(this, {
// observables
2024-02-02 08:22:38 +00:00
viewMap: observable.ref,
2024-01-30 08:56:59 +00:00
// computed
viewIds: computed,
// actions
fetch: action,
create: action,
remove: action,
2024-01-30 08:56:59 +00:00
duplicate: action,
});
}
// computed
get viewIds() {
return Object.keys(this.viewMap);
}
2024-02-02 08:22:38 +00:00
// helper actions
viewById = (viewId: string) => this.viewMap?.[viewId] || undefined;
2024-01-30 08:56:59 +00:00
// actions
fetch = async () => {
2024-01-30 11:15:14 +00:00
const { workspaceSlug, projectId } = this.store.app.router;
if (!workspaceSlug) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
const views = await this.service.fetch(workspaceSlug, projectId);
if (!views) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
runInAction(() => {
2024-01-30 08:56:59 +00:00
views.forEach((view) => {
2024-02-02 08:22:38 +00:00
if (view.id) set(this.viewMap, [view.id], new ViewStore(this.store, view, this.service));
2024-01-30 08:56:59 +00:00
});
2024-01-30 11:15:14 +00:00
});
2024-01-30 08:56:59 +00:00
};
2024-01-30 11:15:14 +00:00
create = async (data: Partial<TView>) => {
const { workspaceSlug, projectId } = this.store.app.router;
if (!workspaceSlug) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
const view = await this.service.create(workspaceSlug, data, projectId);
if (!view) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
runInAction(() => {
2024-02-02 08:22:38 +00:00
if (view.id) set(this.viewMap, [view.id], new ViewStore(this.store, view, this.service));
2024-01-30 11:15:14 +00:00
});
2024-01-30 08:56:59 +00:00
};
remove = async (viewId: string) => {
2024-01-30 11:15:14 +00:00
const { workspaceSlug, projectId } = this.store.app.router;
2024-02-02 08:22:38 +00:00
if (!workspaceSlug || !viewId) return;
2024-01-30 08:56:59 +00:00
2024-02-02 08:22:38 +00:00
await this.service.remove?.(workspaceSlug, viewId, projectId);
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
runInAction(() => {
2024-01-30 08:56:59 +00:00
delete this.viewMap[viewId];
2024-01-30 11:15:14 +00:00
});
2024-01-30 08:56:59 +00:00
};
duplicate = async (viewId: string) => {
2024-01-30 11:15:14 +00:00
const { workspaceSlug, projectId } = this.store.app.router;
2024-02-02 08:22:38 +00:00
if (!workspaceSlug || !this.service.duplicate) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
const view = await this.service.duplicate(workspaceSlug, viewId, projectId);
if (!view) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
runInAction(() => {
2024-02-02 08:22:38 +00:00
if (view.id) set(this.viewMap, [view.id], new ViewStore(this.store, view, this.service));
2024-01-30 11:15:14 +00:00
});
2024-01-30 08:56:59 +00:00
};
}