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

131 lines
3.8 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";
2024-02-05 14:39:17 +00:00
export type TLoader = "init-loader" | "mutation-loader" | "submitting" | undefined;
2024-01-30 08:56:59 +00:00
2024-02-02 08:22:38 +00:00
type TViewRootStore = {
2024-01-30 08:56:59 +00:00
// observables
2024-02-05 14:39:17 +00:00
loader: TLoader;
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
2024-02-05 14:39:17 +00:00
fetch: (_loader?: TLoader) => Promise<void>;
localViewCreate: (view: TView) => Promise<void>;
clearLocalView: (viewId: string) => Promise<void>;
2024-01-30 08:56:59 +00:00
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 {
2024-02-05 14:39:17 +00:00
// observables
loader: TLoader = "init-loader";
2024-02-02 08:22:38 +00:00
viewMap: Record<string, ViewStore> = {};
2024-01-30 08:56:59 +00:00
2024-02-05 14:39:17 +00:00
constructor(private store: RootStore, private service: TViewService, private defaultViews: TView[] = []) {
2024-01-30 08:56:59 +00:00
makeObservable(this, {
// observables
2024-02-05 14:39:17 +00:00
loader: observable.ref,
viewMap: observable,
2024-01-30 08:56:59 +00:00
// computed
viewIds: computed,
// actions
fetch: action,
2024-02-05 14:39:17 +00:00
localViewCreate: action,
clearLocalView: action,
2024-01-30 08:56:59 +00:00
create: action,
remove: action,
2024-01-30 08:56:59 +00:00
duplicate: action,
});
}
// computed
get viewIds() {
2024-02-05 14:39:17 +00:00
const views = Object.values(this.viewMap);
return views.filter((view) => !view?.is_create).map((view) => view.id) as string[];
2024-01-30 08:56:59 +00:00
}
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
2024-02-05 14:39:17 +00:00
fetch = async (_loader: TLoader = "init-loader") => {
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-02-05 14:39:17 +00:00
runInAction(() => {
if (this.defaultViews && this.defaultViews.length > 0)
this.defaultViews?.forEach((view) => {
if (view.id) set(this.viewMap, [view.id], new ViewStore(this.store, view, this.service));
});
});
this.loader = _loader;
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-02-05 14:39:17 +00:00
this.loader = undefined;
});
};
localViewCreate = async (view: TView) => {
runInAction(() => {
if (view.id) set(this.viewMap, [view.id], new ViewStore(this.store, view, this.service));
});
};
clearLocalView = async (viewId: string) => {
runInAction(() => {
if (viewId) delete this.viewMap[viewId];
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
};
}