2023-03-23 05:31:06 +00:00
|
|
|
// services
|
|
|
|
import APIService from "services/api.service";
|
2023-03-30 13:57:46 +00:00
|
|
|
import trackEventServices from "services/track-event.service";
|
|
|
|
|
2023-03-23 05:31:06 +00:00
|
|
|
// types
|
2023-03-30 13:57:46 +00:00
|
|
|
import { IPage, IPageBlock, RecentPagesResponse, IIssue } from "types";
|
2023-03-23 05:31:06 +00:00
|
|
|
|
|
|
|
const { NEXT_PUBLIC_API_BASE_URL } = process.env;
|
|
|
|
|
2023-03-30 13:57:46 +00:00
|
|
|
const trackEvent =
|
|
|
|
process.env.NEXT_PUBLIC_TRACK_EVENTS === "true" || process.env.NEXT_PUBLIC_TRACK_EVENTS === "1";
|
|
|
|
|
2023-03-23 05:31:06 +00:00
|
|
|
class PageServices extends APIService {
|
|
|
|
constructor() {
|
|
|
|
super(NEXT_PUBLIC_API_BASE_URL || "http://localhost:8000");
|
|
|
|
}
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
async createPage(workspaceSlug: string, projectId: string, data: Partial<IPage>): Promise<IPage> {
|
2023-03-23 05:31:06 +00:00
|
|
|
return this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/`, data)
|
2023-03-30 13:57:46 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (trackEvent) trackEventServices.trackPageEvent(response?.data, "PAGE_CREATE");
|
|
|
|
return response?.data;
|
|
|
|
})
|
2023-03-23 05:31:06 +00:00
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async patchPage(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
pageId: string,
|
2023-03-25 18:09:46 +00:00
|
|
|
data: Partial<IPage>
|
2023-03-23 05:31:06 +00:00
|
|
|
): Promise<IPage> {
|
|
|
|
return this.patch(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/`,
|
|
|
|
data
|
|
|
|
)
|
2023-03-30 13:57:46 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (trackEvent) trackEventServices.trackPageEvent(response?.data, "PAGE_UPDATE");
|
|
|
|
return response?.data;
|
|
|
|
})
|
2023-03-23 05:31:06 +00:00
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async deletePage(workspaceSlug: string, projectId: string, pageId: string): Promise<any> {
|
|
|
|
return this.delete(`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/`)
|
2023-03-30 13:57:46 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (trackEvent) trackEventServices.trackPageEvent(response?.data, "PAGE_DELETE");
|
|
|
|
return response?.data;
|
|
|
|
})
|
2023-03-23 05:31:06 +00:00
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async addPageToFavorites(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
data: {
|
|
|
|
page: string;
|
|
|
|
}
|
2023-03-25 18:09:46 +00:00
|
|
|
): Promise<any> {
|
2023-03-23 05:31:06 +00:00
|
|
|
return this.post(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/user-favorite-pages/`,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async removePageFromFavorites(workspaceSlug: string, projectId: string, pageId: string) {
|
|
|
|
return this.delete(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/user-favorite-pages/${pageId}`
|
|
|
|
)
|
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-26 10:08:44 +00:00
|
|
|
async getPagesWithParams(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
queries: any
|
|
|
|
): Promise<IPage[]> {
|
|
|
|
return this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/`, {
|
|
|
|
params: queries,
|
|
|
|
})
|
2023-03-25 18:09:46 +00:00
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-26 10:08:44 +00:00
|
|
|
async getRecentPages(workspaceSlug: string, projectId: string): Promise<RecentPagesResponse> {
|
|
|
|
return this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/`, {
|
|
|
|
params: {
|
|
|
|
page_view: "recent",
|
|
|
|
},
|
|
|
|
})
|
2023-03-25 18:09:46 +00:00
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPageDetails(workspaceSlug: string, projectId: string, pageId: string): Promise<IPage> {
|
|
|
|
return this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/`)
|
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-23 05:31:06 +00:00
|
|
|
async createPageBlock(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
pageId: string,
|
2023-03-25 18:09:46 +00:00
|
|
|
data: Partial<IPageBlock>
|
|
|
|
): Promise<IPageBlock> {
|
2023-03-23 05:31:06 +00:00
|
|
|
return this.post(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/page-blocks/`,
|
|
|
|
data
|
|
|
|
)
|
2023-03-30 13:57:46 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (trackEvent) trackEventServices.trackPageBlockEvent(response?.data, "PAGE_BLOCK_CREATE");
|
|
|
|
return response?.data;
|
|
|
|
})
|
2023-03-23 05:31:06 +00:00
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPageBlock(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
pageId: string,
|
|
|
|
pageBlockId: string
|
|
|
|
): Promise<IPageBlock[]> {
|
|
|
|
return this.get(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/page-blocks/${pageBlockId}/`
|
|
|
|
)
|
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async patchPageBlock(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
pageId: string,
|
|
|
|
pageBlockId: string,
|
2023-03-25 18:09:46 +00:00
|
|
|
data: Partial<IPageBlock>
|
2023-03-23 05:31:06 +00:00
|
|
|
): Promise<IPage> {
|
|
|
|
return this.patch(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/page-blocks/${pageBlockId}/`,
|
|
|
|
data
|
|
|
|
)
|
2023-03-30 13:57:46 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (trackEvent) trackEventServices.trackPageBlockEvent(response?.data, "PAGE_BLOCK_UPDATE");
|
|
|
|
return response?.data;
|
|
|
|
})
|
2023-03-23 05:31:06 +00:00
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async deletePageBlock(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
pageId: string,
|
|
|
|
pageBlockId: string
|
|
|
|
): Promise<any> {
|
|
|
|
return this.delete(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/page-blocks/${pageBlockId}/`
|
|
|
|
)
|
2023-03-30 13:57:46 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (trackEvent) trackEventServices.trackPageBlockEvent(response?.data, "PAGE_BLOCK_DELETE");
|
|
|
|
return response?.data;
|
|
|
|
})
|
2023-03-23 05:31:06 +00:00
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async listPageBlocks(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
pageId: string
|
|
|
|
): Promise<IPageBlock[]> {
|
|
|
|
return this.get(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/page-blocks/`
|
|
|
|
)
|
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
2023-03-25 18:09:46 +00:00
|
|
|
|
|
|
|
async convertPageBlockToIssue(
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
pageId: string,
|
|
|
|
blockId: string
|
|
|
|
): Promise<IIssue> {
|
|
|
|
return this.post(
|
|
|
|
`/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/page-blocks/${blockId}/issues/`
|
|
|
|
)
|
2023-03-30 13:57:46 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (trackEvent)
|
|
|
|
trackEventServices.trackPageBlockEvent(response?.data, "PAGE_BLOCK_CONVERTED_TO_ISSUE");
|
|
|
|
return response?.data;
|
|
|
|
})
|
2023-03-25 18:09:46 +00:00
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
2023-03-23 05:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new PageServices();
|