refactor: move page service to a new folder

This commit is contained in:
Aaryan Khandelwal 2024-06-11 16:09:26 +05:30
parent 05de4d83f3
commit a5a4cde1d7
5 changed files with 27 additions and 26 deletions

View File

@ -6,9 +6,9 @@ import { EditorRefApi, generateJSONfromHTML } from "@plane/editor-core";
// hooks // hooks
import useReloadConfirmations from "@/hooks/use-reload-confirmation"; import useReloadConfirmations from "@/hooks/use-reload-confirmation";
// services // services
import { PageService } from "@/services/page.service"; import { ProjectPageService } from "@/services/page";
import { IPageStore } from "@/store/pages/page.store"; import { IPageStore } from "@/store/pages/page.store";
const pageService = new PageService(); const projectPageService = new ProjectPageService();
type Props = { type Props = {
editorRef: React.RefObject<EditorRefApi>; editorRef: React.RefObject<EditorRefApi>;
@ -32,7 +32,7 @@ export const usePageDescription = (props: Props) => {
const { data: descriptionYJS, mutate: mutateDescriptionYJS } = useSWR( const { data: descriptionYJS, mutate: mutateDescriptionYJS } = useSWR(
workspaceSlug && projectId && pageId ? `PAGE_DESCRIPTION_${workspaceSlug}_${projectId}_${pageId}` : null, workspaceSlug && projectId && pageId ? `PAGE_DESCRIPTION_${workspaceSlug}_${projectId}_${pageId}` : null,
workspaceSlug && projectId && pageId workspaceSlug && projectId && pageId
? () => pageService.fetchDescriptionYJS(workspaceSlug.toString(), projectId.toString(), pageId.toString()) ? () => projectPageService.fetchDescriptionYJS(workspaceSlug.toString(), projectId.toString(), pageId.toString())
: null, : null,
{ {
revalidateOnFocus: false, revalidateOnFocus: false,

View File

@ -0,0 +1 @@
export * from "./project-page.service";

View File

@ -5,7 +5,7 @@ import { API_BASE_URL } from "@/helpers/common.helper";
// services // services
import { APIService } from "@/services/api.service"; import { APIService } from "@/services/api.service";
export class PageService extends APIService { export class ProjectPageService extends APIService {
constructor() { constructor() {
super(API_BASE_URL); super(API_BASE_URL);
} }

View File

@ -6,7 +6,7 @@ import { TLogoProps, TPage } from "@plane/types";
import { EPageAccess } from "@/constants/page"; import { EPageAccess } from "@/constants/page";
import { EUserProjectRoles } from "@/constants/project"; import { EUserProjectRoles } from "@/constants/project";
// services // services
import { PageService } from "@/services/page.service"; import { ProjectPageService } from "@/services/page";
import { RootStore } from "../root.store"; import { RootStore } from "../root.store";
export type TLoader = "submitting" | "submitted" | "saved"; export type TLoader = "submitting" | "submitted" | "saved";
@ -69,7 +69,7 @@ export class PageStore implements IPageStore {
// reactions // reactions
disposers: Array<() => void> = []; disposers: Array<() => void> = [];
// services // services
pageService: PageService; projectPageService: ProjectPageService;
constructor( constructor(
private store: RootStore, private store: RootStore,
@ -144,7 +144,7 @@ export class PageStore implements IPageStore {
removeFromFavorites: action, removeFromFavorites: action,
}); });
this.pageService = new PageService(); this.projectPageService = new ProjectPageService();
const titleDisposer = reaction( const titleDisposer = reaction(
() => this.name, () => this.name,
@ -152,7 +152,7 @@ export class PageStore implements IPageStore {
const { workspaceSlug, projectId } = this.store.router; const { workspaceSlug, projectId } = this.store.router;
if (!workspaceSlug || !projectId || !this.id) return; if (!workspaceSlug || !projectId || !this.id) return;
this.isSubmitting = "submitting"; this.isSubmitting = "submitting";
this.pageService this.projectPageService
.update(workspaceSlug, projectId, this.id, { .update(workspaceSlug, projectId, this.id, {
name, name,
}) })
@ -299,7 +299,7 @@ export class PageStore implements IPageStore {
}); });
}); });
await this.pageService.update(workspaceSlug, projectId, this.id, currentPage); await this.projectPageService.update(workspaceSlug, projectId, this.id, currentPage);
} catch (error) { } catch (error) {
runInAction(() => { runInAction(() => {
Object.keys(pageData).forEach((key) => { Object.keys(pageData).forEach((key) => {
@ -335,7 +335,7 @@ export class PageStore implements IPageStore {
}); });
try { try {
await this.pageService.updateDescriptionYJS(workspaceSlug, projectId, this.id, { await this.projectPageService.updateDescriptionYJS(workspaceSlug, projectId, this.id, {
description_binary: binaryString, description_binary: binaryString,
description_html: descriptionHTML, description_html: descriptionHTML,
}); });
@ -358,7 +358,7 @@ export class PageStore implements IPageStore {
runInAction(() => (this.access = EPageAccess.PUBLIC)); runInAction(() => (this.access = EPageAccess.PUBLIC));
try { try {
await this.pageService.update(workspaceSlug, projectId, this.id, { await this.projectPageService.update(workspaceSlug, projectId, this.id, {
access: EPageAccess.PUBLIC, access: EPageAccess.PUBLIC,
}); });
} catch (error) { } catch (error) {
@ -380,7 +380,7 @@ export class PageStore implements IPageStore {
runInAction(() => (this.access = EPageAccess.PRIVATE)); runInAction(() => (this.access = EPageAccess.PRIVATE));
try { try {
await this.pageService.update(workspaceSlug, projectId, this.id, { await this.projectPageService.update(workspaceSlug, projectId, this.id, {
access: EPageAccess.PRIVATE, access: EPageAccess.PRIVATE,
}); });
} catch (error) { } catch (error) {
@ -401,7 +401,7 @@ export class PageStore implements IPageStore {
const pageIsLocked = this.is_locked; const pageIsLocked = this.is_locked;
runInAction(() => (this.is_locked = true)); runInAction(() => (this.is_locked = true));
await this.pageService.lock(workspaceSlug, projectId, this.id).catch((error) => { await this.projectPageService.lock(workspaceSlug, projectId, this.id).catch((error) => {
runInAction(() => { runInAction(() => {
this.is_locked = pageIsLocked; this.is_locked = pageIsLocked;
}); });
@ -419,7 +419,7 @@ export class PageStore implements IPageStore {
const pageIsLocked = this.is_locked; const pageIsLocked = this.is_locked;
runInAction(() => (this.is_locked = false)); runInAction(() => (this.is_locked = false));
await this.pageService.unlock(workspaceSlug, projectId, this.id).catch((error) => { await this.projectPageService.unlock(workspaceSlug, projectId, this.id).catch((error) => {
runInAction(() => { runInAction(() => {
this.is_locked = pageIsLocked; this.is_locked = pageIsLocked;
}); });
@ -435,7 +435,7 @@ export class PageStore implements IPageStore {
if (!workspaceSlug || !projectId || !this.id) return undefined; if (!workspaceSlug || !projectId || !this.id) return undefined;
try { try {
const response = await this.pageService.archive(workspaceSlug, projectId, this.id); const response = await this.projectPageService.archive(workspaceSlug, projectId, this.id);
runInAction(() => { runInAction(() => {
this.archived_at = response.archived_at; this.archived_at = response.archived_at;
}); });
@ -452,7 +452,7 @@ export class PageStore implements IPageStore {
if (!workspaceSlug || !projectId || !this.id) return undefined; if (!workspaceSlug || !projectId || !this.id) return undefined;
try { try {
await this.pageService.restore(workspaceSlug, projectId, this.id); await this.projectPageService.restore(workspaceSlug, projectId, this.id);
runInAction(() => { runInAction(() => {
this.archived_at = null; this.archived_at = null;
}); });
@ -466,7 +466,7 @@ export class PageStore implements IPageStore {
if (!workspaceSlug || !projectId || !this.id) return undefined; if (!workspaceSlug || !projectId || !this.id) return undefined;
try { try {
await this.pageService.update(workspaceSlug, projectId, this.id, { await this.projectPageService.update(workspaceSlug, projectId, this.id, {
logo_props, logo_props,
}); });
runInAction(() => { runInAction(() => {
@ -489,7 +489,7 @@ export class PageStore implements IPageStore {
this.is_favorite = true; this.is_favorite = true;
}); });
await this.pageService.addToFavorites(workspaceSlug, projectId, this.id).catch((error) => { await this.projectPageService.addToFavorites(workspaceSlug, projectId, this.id).catch((error) => {
runInAction(() => { runInAction(() => {
this.is_favorite = pageIsFavorite; this.is_favorite = pageIsFavorite;
}); });
@ -509,7 +509,7 @@ export class PageStore implements IPageStore {
this.is_favorite = false; this.is_favorite = false;
}); });
await this.pageService.removeFromFavorites(workspaceSlug, projectId, this.id).catch((error) => { await this.projectPageService.removeFromFavorites(workspaceSlug, projectId, this.id).catch((error) => {
runInAction(() => { runInAction(() => {
this.is_favorite = pageIsFavorite; this.is_favorite = pageIsFavorite;
}); });

View File

@ -7,7 +7,7 @@ import { TPage, TPageFilters, TPageNavigationTabs } from "@plane/types";
// helpers // helpers
import { filterPagesByPageType, getPageName, orderPages, shouldFilterPage } from "@/helpers/page.helper"; import { filterPagesByPageType, getPageName, orderPages, shouldFilterPage } from "@/helpers/page.helper";
// services // services
import { PageService } from "@/services/page.service"; import { ProjectPageService } from "@/services/page";
// store // store
import { IPageStore, PageStore } from "@/store/pages/page.store"; import { IPageStore, PageStore } from "@/store/pages/page.store";
import { RootStore } from "../root.store"; import { RootStore } from "../root.store";
@ -48,7 +48,7 @@ export class ProjectPageStore implements IProjectPageStore {
sortBy: "desc", sortBy: "desc",
}; };
// service // service
service: PageService; projectPageService: ProjectPageService;
constructor(private store: RootStore) { constructor(private store: RootStore) {
makeObservable(this, { makeObservable(this, {
@ -67,7 +67,7 @@ export class ProjectPageStore implements IProjectPageStore {
removePage: action, removePage: action,
}); });
// service // service
this.service = new PageService(); this.projectPageService = new ProjectPageService();
// initialize display filters of the current project // initialize display filters of the current project
reaction( reaction(
() => this.store.router.projectId, () => this.store.router.projectId,
@ -159,7 +159,7 @@ export class ProjectPageStore implements IProjectPageStore {
this.error = undefined; this.error = undefined;
}); });
const pages = await this.service.fetchAll(workspaceSlug, projectId); const pages = await this.projectPageService.fetchAll(workspaceSlug, projectId);
runInAction(() => { runInAction(() => {
for (const page of pages) if (page?.id) set(this.data, [page.id], new PageStore(this.store, page)); for (const page of pages) if (page?.id) set(this.data, [page.id], new PageStore(this.store, page));
this.loader = undefined; this.loader = undefined;
@ -193,7 +193,7 @@ export class ProjectPageStore implements IProjectPageStore {
this.error = undefined; this.error = undefined;
}); });
const page = await this.service.fetchById(workspaceSlug, projectId, pageId); const page = await this.projectPageService.fetchById(workspaceSlug, projectId, pageId);
runInAction(() => { runInAction(() => {
if (page?.id) set(this.data, [page.id], new PageStore(this.store, page)); if (page?.id) set(this.data, [page.id], new PageStore(this.store, page));
this.loader = undefined; this.loader = undefined;
@ -226,7 +226,7 @@ export class ProjectPageStore implements IProjectPageStore {
this.error = undefined; this.error = undefined;
}); });
const page = await this.service.create(workspaceSlug, projectId, pageData); const page = await this.projectPageService.create(workspaceSlug, projectId, pageData);
runInAction(() => { runInAction(() => {
if (page?.id) set(this.data, [page.id], new PageStore(this.store, page)); if (page?.id) set(this.data, [page.id], new PageStore(this.store, page));
this.loader = undefined; this.loader = undefined;
@ -254,7 +254,7 @@ export class ProjectPageStore implements IProjectPageStore {
const { workspaceSlug, projectId } = this.store.router; const { workspaceSlug, projectId } = this.store.router;
if (!workspaceSlug || !projectId || !pageId) return undefined; if (!workspaceSlug || !projectId || !pageId) return undefined;
await this.service.remove(workspaceSlug, projectId, pageId); await this.projectPageService.remove(workspaceSlug, projectId, pageId);
runInAction(() => unset(this.data, [pageId])); runInAction(() => unset(this.data, [pageId]));
} catch (error) { } catch (error) {
runInAction(() => { runInAction(() => {