2024-01-30 08:56:59 +00:00
|
|
|
// types
|
2024-01-30 11:15:14 +00:00
|
|
|
import { action, computed, makeObservable, observable, runInAction } from "mobx";
|
2024-01-30 08:56:59 +00:00
|
|
|
// stores
|
|
|
|
import { RootStore } from "store/root.store";
|
2024-01-30 11:15:14 +00:00
|
|
|
import { ViewsStore } from "./view.store";
|
2024-01-30 08:56:59 +00:00
|
|
|
// types
|
|
|
|
import { TViewService } from "services/view/types";
|
|
|
|
import { TView } from "@plane/types";
|
|
|
|
import { set } from "lodash";
|
|
|
|
|
|
|
|
export type TLoader = "" | undefined;
|
|
|
|
|
|
|
|
type TViewRoot = {
|
|
|
|
// observables
|
2024-01-30 11:15:14 +00:00
|
|
|
viewMap: Record<string, ViewsStore>;
|
2024-01-30 08:56:59 +00:00
|
|
|
// computed
|
|
|
|
viewIds: string[];
|
|
|
|
// actions
|
|
|
|
fetch: () => Promise<void>;
|
|
|
|
create: (view: Partial<TView>) => Promise<void>;
|
|
|
|
delete: (viewId: string) => Promise<void>;
|
|
|
|
duplicate: (viewId: string) => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class ViewRoot implements TViewRoot {
|
2024-01-30 11:15:14 +00:00
|
|
|
viewMap: Record<string, ViewsStore> = {};
|
2024-01-30 08:56:59 +00:00
|
|
|
|
|
|
|
constructor(private store: RootStore, private service: TViewService) {
|
|
|
|
makeObservable(this, {
|
|
|
|
// observables
|
|
|
|
viewMap: observable,
|
|
|
|
// computed
|
|
|
|
viewIds: computed,
|
|
|
|
// actions
|
|
|
|
fetch: action,
|
|
|
|
create: action,
|
|
|
|
delete: action,
|
|
|
|
duplicate: action,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// computed
|
|
|
|
get viewIds() {
|
|
|
|
return Object.keys(this.viewMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
get views() {
|
|
|
|
return Object.values(this.viewMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-01-30 11:15:14 +00:00
|
|
|
set(this.viewMap, [view.id], new ViewsStore(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(() => {
|
|
|
|
set(this.viewMap, [view.id], new ViewsStore(this.store, view, this.service));
|
|
|
|
});
|
2024-01-30 08:56:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
delete = async (viewId: string) => {
|
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
|
|
|
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;
|
|
|
|
if (!workspaceSlug) 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(() => {
|
|
|
|
set(this.viewMap, [view.id], new ViewsStore(this.store, view, this.service));
|
|
|
|
});
|
2024-01-30 08:56:59 +00:00
|
|
|
};
|
|
|
|
}
|