[FIX] Pages Malfunctioning on Load and Recent Pages Computation (#3359)

* fix: fixed `usePage` hook returning context instead of IPageStore

* fix: updated recent pages with `updated_at` instead of `created_at`

* fix: thown error instead of returning empty array
This commit is contained in:
Henit Chobisa 2024-01-12 13:26:48 +05:30 committed by sriram veeraghanta
parent 38580c3940
commit 151c355177
2 changed files with 23 additions and 17 deletions

View File

@ -4,8 +4,8 @@ import { StoreContext } from "contexts/store-context";
// types
import { IPageStore } from "store/page.store";
export const usePage = (): any => {
export const usePage = (): IPageStore => {
const context = useContext(StoreContext);
if (context === undefined) throw new Error("usePage must be used within StoreProvider");
return context as any;
return context.page;
};

View File

@ -133,21 +133,21 @@ export class PageStore implements IPageStore {
get recentProjectPages() {
if (!this.projectPageIds) return null;
const data: IRecentPages = { today: [], yesterday: [], this_week: [], older: [] };
data.today = this.projectPageIds.filter((p) => isToday(new Date(this.pages?.[p]?.created_at))) || [];
data.yesterday = this.projectPageIds.filter((p) => isYesterday(new Date(this.pages?.[p]?.created_at))) || [];
data.today = this.projectPageIds.filter((p) => isToday(new Date(this.pages?.[p]?.updated_at))) || [];
data.yesterday = this.projectPageIds.filter((p) => isYesterday(new Date(this.pages?.[p]?.updated_at))) || [];
data.this_week =
this.projectPageIds.filter((p) => {
const pageCreatedAt = this.pages?.[p]?.created_at;
const pageUpdatedAt = this.pages?.[p]?.updated_at;
return (
isThisWeek(new Date(pageCreatedAt)) &&
!isToday(new Date(pageCreatedAt)) &&
!isYesterday(new Date(pageCreatedAt))
isThisWeek(new Date(pageUpdatedAt)) &&
!isToday(new Date(pageUpdatedAt)) &&
!isYesterday(new Date(pageUpdatedAt))
);
}) || [];
data.older =
this.projectPageIds.filter((p) => {
const pageCreatedAt = this.pages?.[p]?.created_at;
return !isThisWeek(new Date(pageCreatedAt)) && !isYesterday(new Date(pageCreatedAt));
const pageUpdatedAt = this.pages?.[p]?.updated_at;
return !isThisWeek(new Date(pageUpdatedAt)) && !isYesterday(new Date(pageUpdatedAt));
}) || [];
return data;
}
@ -184,8 +184,10 @@ export class PageStore implements IPageStore {
* @param projectId
* @returns Promise<IPage[]>
*/
fetchProjectPages = async (workspaceSlug: string, projectId: string) =>
await this.pageService.getProjectPages(workspaceSlug, projectId).then((response) => {
fetchProjectPages = async (workspaceSlug: string, projectId: string) => {
try {
return await this.pageService.getProjectPages(workspaceSlug, projectId).then((response) => {
console.log("Response from backend 1", response);
runInAction(() => {
response.forEach((page) => {
set(this.pages, [page.id], page);
@ -193,6 +195,10 @@ export class PageStore implements IPageStore {
});
return response;
});
} catch (error) {
throw error;
}
};
/**
* fetches all archived pages for a project.