2024-04-11 15:58:59 +00:00
|
|
|
import set from "lodash/set";
|
|
|
|
import unset from "lodash/unset";
|
2024-04-15 07:20:22 +00:00
|
|
|
import { makeObservable, observable, runInAction, action } from "mobx";
|
2024-04-11 15:58:59 +00:00
|
|
|
import { computedFn } from "mobx-utils";
|
2024-04-15 07:20:22 +00:00
|
|
|
// types
|
2024-04-11 15:58:59 +00:00
|
|
|
import { TPage, TPageFilters, TPageNavigationTabs } from "@plane/types";
|
|
|
|
// helpers
|
|
|
|
import { filterPagesByPageType, orderPages, shouldFilterPage } from "@/helpers/page.helper";
|
|
|
|
// services
|
|
|
|
import { PageService } from "@/services/page.service";
|
2024-04-15 07:20:22 +00:00
|
|
|
// store
|
2024-04-11 15:58:59 +00:00
|
|
|
import { IPageStore, PageStore } from "@/store/pages/page.store";
|
|
|
|
import { RootStore } from "../root.store";
|
|
|
|
|
|
|
|
type TLoader = "init-loader" | "mutation-loader" | undefined;
|
|
|
|
|
|
|
|
type TError = { title: string; description: string };
|
|
|
|
|
|
|
|
export interface IProjectPageStore {
|
|
|
|
// observables
|
|
|
|
loader: TLoader;
|
|
|
|
data: Record<string, IPageStore>; // pageId => PageStore
|
|
|
|
error: TError | undefined;
|
|
|
|
filters: TPageFilters;
|
|
|
|
// helper actions
|
2024-04-15 07:20:22 +00:00
|
|
|
getCurrentProjectPageIds: (pageType: TPageNavigationTabs) => string[] | undefined;
|
|
|
|
getCurrentProjectFilteredPageIds: (pageType: TPageNavigationTabs) => string[] | undefined;
|
2024-04-11 15:58:59 +00:00
|
|
|
pageById: (pageId: string) => IPageStore | undefined;
|
|
|
|
updateFilters: <T extends keyof TPageFilters>(filterKey: T, filterValue: TPageFilters[T]) => void;
|
|
|
|
clearAllFilters: () => void;
|
|
|
|
// actions
|
|
|
|
getAllPages: (pageType: TPageNavigationTabs) => Promise<TPage[] | undefined>;
|
|
|
|
getPageById: (pageId: string) => Promise<TPage | undefined>;
|
|
|
|
createPage: (pageData: Partial<TPage>) => Promise<TPage | undefined>;
|
|
|
|
removePage: (pageId: string) => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ProjectPageStore implements IProjectPageStore {
|
|
|
|
// observables
|
|
|
|
loader: TLoader = "init-loader";
|
|
|
|
data: Record<string, IPageStore> = {}; // pageId => PageStore
|
|
|
|
error: TError | undefined = undefined;
|
|
|
|
filters: TPageFilters = {
|
|
|
|
searchQuery: "",
|
|
|
|
sortKey: "name",
|
|
|
|
sortBy: "asc",
|
|
|
|
};
|
|
|
|
// service
|
|
|
|
service: PageService;
|
|
|
|
|
|
|
|
constructor(private store: RootStore) {
|
|
|
|
makeObservable(this, {
|
|
|
|
// observables
|
|
|
|
loader: observable.ref,
|
|
|
|
data: observable,
|
|
|
|
error: observable,
|
|
|
|
filters: observable,
|
|
|
|
// helper actions
|
|
|
|
updateFilters: action,
|
|
|
|
clearAllFilters: action,
|
|
|
|
// actions
|
|
|
|
getAllPages: action,
|
|
|
|
getPageById: action,
|
|
|
|
createPage: action,
|
|
|
|
removePage: action,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.service = new PageService();
|
|
|
|
}
|
|
|
|
|
2024-04-15 07:20:22 +00:00
|
|
|
/**
|
|
|
|
* @description get the current project page ids based on the pageType
|
|
|
|
* @param {TPageNavigationTabs} pageType
|
|
|
|
*/
|
|
|
|
getCurrentProjectPageIds = computedFn((pageType: TPageNavigationTabs) => {
|
2024-04-11 15:58:59 +00:00
|
|
|
const { projectId } = this.store.app.router;
|
|
|
|
if (!projectId) return undefined;
|
|
|
|
// helps to filter pages based on the pageType
|
2024-04-15 07:20:22 +00:00
|
|
|
let pagesByType = filterPagesByPageType(pageType, Object.values(this?.data || {}));
|
|
|
|
pagesByType = pagesByType.filter((p) => p.project === projectId);
|
2024-04-11 15:58:59 +00:00
|
|
|
|
|
|
|
const pages = (pagesByType.map((page) => page.id) as string[]) || undefined;
|
|
|
|
|
|
|
|
return pages ?? undefined;
|
2024-04-15 07:20:22 +00:00
|
|
|
});
|
2024-04-11 15:58:59 +00:00
|
|
|
|
2024-04-15 07:20:22 +00:00
|
|
|
/**
|
|
|
|
* @description get the current project filtered page ids based on the pageType
|
|
|
|
* @param {TPageNavigationTabs} pageType
|
|
|
|
*/
|
|
|
|
getCurrentProjectFilteredPageIds = computedFn((pageType: TPageNavigationTabs) => {
|
2024-04-11 15:58:59 +00:00
|
|
|
const { projectId } = this.store.app.router;
|
|
|
|
if (!projectId) return undefined;
|
|
|
|
|
|
|
|
// helps to filter pages based on the pageType
|
2024-04-15 07:20:22 +00:00
|
|
|
const pagesByType = filterPagesByPageType(pageType, Object.values(this?.data || {}));
|
2024-04-11 15:58:59 +00:00
|
|
|
let filteredPages = pagesByType.filter(
|
|
|
|
(p) =>
|
2024-04-15 07:20:22 +00:00
|
|
|
p.project === projectId &&
|
2024-04-11 15:58:59 +00:00
|
|
|
p.name?.toLowerCase().includes(this.filters.searchQuery.toLowerCase()) &&
|
|
|
|
shouldFilterPage(p, this.filters.filters)
|
|
|
|
);
|
|
|
|
filteredPages = orderPages(filteredPages, this.filters.sortKey, this.filters.sortBy);
|
|
|
|
|
|
|
|
const pages = (filteredPages.map((page) => page.id) as string[]) || undefined;
|
|
|
|
|
|
|
|
return pages ?? undefined;
|
|
|
|
});
|
|
|
|
|
2024-04-15 07:20:22 +00:00
|
|
|
/**
|
|
|
|
* @description get the page store by id
|
|
|
|
* @param {string} pageId
|
|
|
|
*/
|
|
|
|
pageById = computedFn((pageId: string) => this.data?.[pageId] || undefined);
|
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
updateFilters = <T extends keyof TPageFilters>(filterKey: T, filterValue: TPageFilters[T]) => {
|
|
|
|
runInAction(() => {
|
|
|
|
set(this.filters, [filterKey], filterValue);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description clear all the filters
|
|
|
|
*/
|
|
|
|
clearAllFilters = () =>
|
|
|
|
runInAction(() => {
|
|
|
|
set(this.filters, ["filters"], {});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2024-04-15 07:20:22 +00:00
|
|
|
* @description fetch all the pages
|
2024-04-11 15:58:59 +00:00
|
|
|
*/
|
|
|
|
getAllPages = async (pageType: TPageNavigationTabs) => {
|
|
|
|
try {
|
|
|
|
const { workspaceSlug, projectId } = this.store.app.router;
|
|
|
|
if (!workspaceSlug || !projectId) return undefined;
|
|
|
|
|
2024-04-15 07:20:22 +00:00
|
|
|
const currentPageIds = this.getCurrentProjectPageIds(pageType);
|
2024-04-11 15:58:59 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.loader = currentPageIds && currentPageIds.length > 0 ? `mutation-loader` : `init-loader`;
|
|
|
|
this.error = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
const pages = await this.service.fetchAll(workspaceSlug, projectId);
|
|
|
|
runInAction(() => {
|
|
|
|
for (const page of pages) if (page?.id) set(this.data, [page.id], new PageStore(this.store, page));
|
|
|
|
this.loader = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
return pages;
|
2024-04-17 09:23:01 +00:00
|
|
|
} catch (error) {
|
2024-04-11 15:58:59 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.loader = undefined;
|
|
|
|
this.error = {
|
|
|
|
title: "Failed",
|
|
|
|
description: "Failed to fetch the pages, Please try again later.",
|
|
|
|
};
|
|
|
|
});
|
2024-04-17 09:23:01 +00:00
|
|
|
throw error;
|
2024-04-11 15:58:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description fetch the details of a page
|
|
|
|
* @param {string} pageId
|
|
|
|
*/
|
|
|
|
getPageById = async (pageId: string) => {
|
|
|
|
try {
|
|
|
|
const { workspaceSlug, projectId } = this.store.app.router;
|
|
|
|
if (!workspaceSlug || !projectId || !pageId) return undefined;
|
|
|
|
|
|
|
|
const currentPageId = this.pageById(pageId);
|
|
|
|
runInAction(() => {
|
|
|
|
this.loader = currentPageId ? `mutation-loader` : `init-loader`;
|
|
|
|
this.error = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
const page = await this.service.fetchById(workspaceSlug, projectId, pageId);
|
|
|
|
runInAction(() => {
|
|
|
|
if (page?.id) set(this.data, [page.id], new PageStore(this.store, page));
|
|
|
|
this.loader = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
return page;
|
2024-04-17 09:23:01 +00:00
|
|
|
} catch (error) {
|
2024-04-11 15:58:59 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.loader = undefined;
|
|
|
|
this.error = {
|
|
|
|
title: "Failed",
|
|
|
|
description: "Failed to fetch the page, Please try again later.",
|
|
|
|
};
|
|
|
|
});
|
2024-04-17 09:23:01 +00:00
|
|
|
throw error;
|
2024-04-11 15:58:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description create a page
|
|
|
|
* @param {Partial<TPage>} pageData
|
|
|
|
*/
|
|
|
|
createPage = async (pageData: Partial<TPage>) => {
|
|
|
|
try {
|
|
|
|
const { workspaceSlug, projectId } = this.store.app.router;
|
|
|
|
if (!workspaceSlug || !projectId) return undefined;
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.loader = "mutation-loader";
|
|
|
|
this.error = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
const page = await this.service.create(workspaceSlug, projectId, pageData);
|
|
|
|
runInAction(() => {
|
|
|
|
if (page?.id) set(this.data, [page.id], new PageStore(this.store, page));
|
|
|
|
this.loader = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
return page;
|
2024-04-17 09:23:01 +00:00
|
|
|
} catch (error) {
|
2024-04-11 15:58:59 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.loader = undefined;
|
|
|
|
this.error = {
|
|
|
|
title: "Failed",
|
|
|
|
description: "Failed to create a page, Please try again later.",
|
|
|
|
};
|
|
|
|
});
|
2024-04-17 09:23:01 +00:00
|
|
|
throw error;
|
2024-04-11 15:58:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description delete a page
|
|
|
|
* @param {string} pageId
|
|
|
|
*/
|
|
|
|
removePage = async (pageId: string) => {
|
|
|
|
try {
|
|
|
|
const { workspaceSlug, projectId } = this.store.app.router;
|
|
|
|
if (!workspaceSlug || !projectId || !pageId) return undefined;
|
|
|
|
|
|
|
|
await this.service.remove(workspaceSlug, projectId, pageId);
|
|
|
|
runInAction(() => unset(this.data, [pageId]));
|
2024-04-17 09:23:01 +00:00
|
|
|
} catch (error) {
|
2024-04-11 15:58:59 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.loader = undefined;
|
|
|
|
this.error = {
|
|
|
|
title: "Failed",
|
|
|
|
description: "Failed to delete a page, Please try again later.",
|
|
|
|
};
|
|
|
|
});
|
2024-04-17 09:23:01 +00:00
|
|
|
throw error;
|
2024-04-11 15:58:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|