[WEB-1019] chore: error state for unauthorized pages (#4219)
* chore: private page access * chore: add error state for private pages --------- Co-authored-by: NarayanBavisetti <narayan3119@gmail.com>
This commit is contained in:
parent
1080094b01
commit
1880eb7704
7 changed files with 226 additions and 184 deletions
|
|
@ -21,6 +21,7 @@ export interface IPageStore extends TPage {
|
|||
canCurrentUserEditPage: boolean; // it will give the user permission to read the page or write the page
|
||||
canCurrentUserDuplicatePage: boolean;
|
||||
canCurrentUserLockPage: boolean;
|
||||
canCurrentUserChangeAccess: boolean;
|
||||
canCurrentUserArchivePage: boolean;
|
||||
canCurrentUserDeletePage: boolean;
|
||||
isContentEditable: boolean;
|
||||
|
|
@ -72,7 +73,10 @@ export class PageStore implements IPageStore {
|
|||
// service
|
||||
pageService: PageService;
|
||||
|
||||
constructor(private store: RootStore, page: TPage) {
|
||||
constructor(
|
||||
private store: RootStore,
|
||||
page: TPage
|
||||
) {
|
||||
this.id = page?.id || undefined;
|
||||
this.name = page?.name || undefined;
|
||||
this.description_html = page?.description_html || undefined;
|
||||
|
|
@ -122,6 +126,7 @@ export class PageStore implements IPageStore {
|
|||
canCurrentUserEditPage: computed,
|
||||
canCurrentUserDuplicatePage: computed,
|
||||
canCurrentUserLockPage: computed,
|
||||
canCurrentUserChangeAccess: computed,
|
||||
canCurrentUserArchivePage: computed,
|
||||
canCurrentUserDeletePage: computed,
|
||||
isContentEditable: computed,
|
||||
|
|
@ -245,12 +250,19 @@ export class PageStore implements IPageStore {
|
|||
return this.isCurrentUserOwner || (!!currentUserProjectRole && currentUserProjectRole >= EUserProjectRoles.MEMBER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns true if the current logged in user can change the access of the page
|
||||
*/
|
||||
get canCurrentUserChangeAccess() {
|
||||
return this.isCurrentUserOwner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns true if the current logged in user can archive the page
|
||||
*/
|
||||
get canCurrentUserArchivePage() {
|
||||
const currentUserProjectRole = this.store.user.membership.currentProjectRole;
|
||||
return this.isCurrentUserOwner || (!!currentUserProjectRole && currentUserProjectRole >= EUserProjectRoles.MEMBER);
|
||||
return this.isCurrentUserOwner || currentUserProjectRole === EUserProjectRoles.ADMIN;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -315,13 +327,14 @@ export class PageStore implements IPageStore {
|
|||
set(this, key, currentPageResponse?.[currentPageKey] || undefined);
|
||||
});
|
||||
});
|
||||
} catch {
|
||||
} catch (error) {
|
||||
runInAction(() => {
|
||||
Object.keys(pageData).forEach((key) => {
|
||||
const currentPageKey = key as keyof TPage;
|
||||
set(this, key, currentPage?.[currentPageKey] || undefined);
|
||||
});
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -349,10 +362,11 @@ export class PageStore implements IPageStore {
|
|||
...updatedProps,
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
} catch (error) {
|
||||
runInAction(() => {
|
||||
this.view_props = currentViewProps;
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -366,13 +380,16 @@ export class PageStore implements IPageStore {
|
|||
const pageAccess = this.access;
|
||||
runInAction(() => (this.access = EPageAccess.PUBLIC));
|
||||
|
||||
await this.pageService
|
||||
.update(workspaceSlug, projectId, this.id, {
|
||||
try {
|
||||
await this.pageService.update(workspaceSlug, projectId, this.id, {
|
||||
access: EPageAccess.PUBLIC,
|
||||
})
|
||||
.catch(() => {
|
||||
runInAction(() => (this.access = pageAccess));
|
||||
});
|
||||
} catch (error) {
|
||||
runInAction(() => {
|
||||
this.access = pageAccess;
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -385,13 +402,16 @@ export class PageStore implements IPageStore {
|
|||
const pageAccess = this.access;
|
||||
runInAction(() => (this.access = EPageAccess.PRIVATE));
|
||||
|
||||
await this.pageService
|
||||
.update(workspaceSlug, projectId, this.id, {
|
||||
try {
|
||||
await this.pageService.update(workspaceSlug, projectId, this.id, {
|
||||
access: EPageAccess.PRIVATE,
|
||||
})
|
||||
.catch(() => {
|
||||
runInAction(() => (this.access = pageAccess));
|
||||
});
|
||||
} catch (error) {
|
||||
runInAction(() => {
|
||||
this.access = pageAccess;
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -404,36 +424,11 @@ export class PageStore implements IPageStore {
|
|||
const pageIsLocked = this.is_locked;
|
||||
runInAction(() => (this.is_locked = true));
|
||||
|
||||
await this.pageService.lock(workspaceSlug, projectId, this.id).catch(() => {
|
||||
runInAction(() => (this.is_locked = pageIsLocked));
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @description archive the page
|
||||
*/
|
||||
archive = async () => {
|
||||
const { workspaceSlug, projectId } = this.store.app.router;
|
||||
if (!workspaceSlug || !projectId || !this.id) return undefined;
|
||||
|
||||
await this.pageService.archive(workspaceSlug, projectId, this.id).then((res) => {
|
||||
await this.pageService.lock(workspaceSlug, projectId, this.id).catch((error) => {
|
||||
runInAction(() => {
|
||||
this.archived_at = res.archived_at;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @description restore the page
|
||||
*/
|
||||
restore = async () => {
|
||||
const { workspaceSlug, projectId } = this.store.app.router;
|
||||
if (!workspaceSlug || !projectId || !this.id) return undefined;
|
||||
|
||||
await this.pageService.restore(workspaceSlug, projectId, this.id).then(() => {
|
||||
runInAction(() => {
|
||||
this.archived_at = null;
|
||||
this.is_locked = pageIsLocked;
|
||||
});
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -447,11 +442,48 @@ export class PageStore implements IPageStore {
|
|||
const pageIsLocked = this.is_locked;
|
||||
runInAction(() => (this.is_locked = false));
|
||||
|
||||
await this.pageService.unlock(workspaceSlug, projectId, this.id).catch(() => {
|
||||
runInAction(() => (this.is_locked = pageIsLocked));
|
||||
await this.pageService.unlock(workspaceSlug, projectId, this.id).catch((error) => {
|
||||
runInAction(() => {
|
||||
this.is_locked = pageIsLocked;
|
||||
});
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @description archive the page
|
||||
*/
|
||||
archive = async () => {
|
||||
const { workspaceSlug, projectId } = this.store.app.router;
|
||||
if (!workspaceSlug || !projectId || !this.id) return undefined;
|
||||
|
||||
try {
|
||||
const response = await this.pageService.archive(workspaceSlug, projectId, this.id);
|
||||
runInAction(() => {
|
||||
this.archived_at = response.archived_at;
|
||||
});
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @description restore the page
|
||||
*/
|
||||
restore = async () => {
|
||||
const { workspaceSlug, projectId } = this.store.app.router;
|
||||
if (!workspaceSlug || !projectId || !this.id) return undefined;
|
||||
|
||||
try {
|
||||
await this.pageService.restore(workspaceSlug, projectId, this.id);
|
||||
runInAction(() => {
|
||||
this.archived_at = null;
|
||||
});
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @description add the page to favorites
|
||||
*/
|
||||
|
|
@ -464,8 +496,11 @@ export class PageStore implements IPageStore {
|
|||
this.is_favorite = true;
|
||||
});
|
||||
|
||||
await this.pageService.addToFavorites(workspaceSlug, projectId, this.id).catch(() => {
|
||||
runInAction(() => (this.is_favorite = pageIsFavorite));
|
||||
await this.pageService.addToFavorites(workspaceSlug, projectId, this.id).catch((error) => {
|
||||
runInAction(() => {
|
||||
this.is_favorite = pageIsFavorite;
|
||||
});
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -481,8 +516,11 @@ export class PageStore implements IPageStore {
|
|||
this.is_favorite = false;
|
||||
});
|
||||
|
||||
await this.pageService.removeFromFavorites(workspaceSlug, projectId, this.id).catch(() => {
|
||||
runInAction(() => (this.is_favorite = pageIsFavorite));
|
||||
await this.pageService.removeFromFavorites(workspaceSlug, projectId, this.id).catch((error) => {
|
||||
runInAction(() => {
|
||||
this.is_favorite = pageIsFavorite;
|
||||
});
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ export class ProjectPageStore implements IProjectPageStore {
|
|||
});
|
||||
|
||||
return pages;
|
||||
} catch {
|
||||
} catch (error) {
|
||||
runInAction(() => {
|
||||
this.loader = undefined;
|
||||
this.error = {
|
||||
|
|
@ -156,6 +156,7 @@ export class ProjectPageStore implements IProjectPageStore {
|
|||
description: "Failed to fetch the pages, Please try again later.",
|
||||
};
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -181,7 +182,7 @@ export class ProjectPageStore implements IProjectPageStore {
|
|||
});
|
||||
|
||||
return page;
|
||||
} catch {
|
||||
} catch (error) {
|
||||
runInAction(() => {
|
||||
this.loader = undefined;
|
||||
this.error = {
|
||||
|
|
@ -189,6 +190,7 @@ export class ProjectPageStore implements IProjectPageStore {
|
|||
description: "Failed to fetch the page, Please try again later.",
|
||||
};
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -213,7 +215,7 @@ export class ProjectPageStore implements IProjectPageStore {
|
|||
});
|
||||
|
||||
return page;
|
||||
} catch {
|
||||
} catch (error) {
|
||||
runInAction(() => {
|
||||
this.loader = undefined;
|
||||
this.error = {
|
||||
|
|
@ -221,6 +223,7 @@ export class ProjectPageStore implements IProjectPageStore {
|
|||
description: "Failed to create a page, Please try again later.",
|
||||
};
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -235,7 +238,7 @@ export class ProjectPageStore implements IProjectPageStore {
|
|||
|
||||
await this.service.remove(workspaceSlug, projectId, pageId);
|
||||
runInAction(() => unset(this.data, [pageId]));
|
||||
} catch {
|
||||
} catch (error) {
|
||||
runInAction(() => {
|
||||
this.loader = undefined;
|
||||
this.error = {
|
||||
|
|
@ -243,6 +246,7 @@ export class ProjectPageStore implements IProjectPageStore {
|
|||
description: "Failed to delete a page, Please try again later.",
|
||||
};
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue