Skip to content

Commit

Permalink
Feat(webapp): Redirect to viewing page if user cannot edit a page
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitmouton committed Apr 16, 2024
1 parent bed50ba commit fd09014
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/webapp/src/pages/a/[slug]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { getApolloClient } from 'api/client';
import { useQuery } from '@apollo/client';
import { Main, Sidebar } from 'layout';
import { EditArticlePage } from 'article/EditArticlePage';
import { Article, User } from 'util/model';
import { useCurrentUser } from 'util/user';
import { useRouter } from 'next/router';

import GetArticleQuery from 'api/query/GetArticleQuery.graphql';

Expand All @@ -16,6 +19,8 @@ const EditArticleRoute = ({
if (!article) {
throw new Error('Article is not valid.');
}
const router = useRouter();
const currentUser = useCurrentUser();
const didWriteClient = React.useRef(false);
if (!didWriteClient.current) {
getApolloClient().writeQuery({
Expand All @@ -29,11 +34,23 @@ const EditArticleRoute = ({
const { data } = useQuery(GetArticleQuery, {
variables: { id: article.id },
});
const canEditArticle =
User.canEditArticle(currentUser, article) || User.isAdmin(currentUser);

React.useEffect(() => {
if (!canEditArticle) {
router.push(Article.getPath(article));
}
}, [canEditArticle, router, article]);

if (error) {
return <ErrorMessage error={error} />;
}

if (!canEditArticle) {
return null;
}

return (
<>
<Main>
Expand Down

0 comments on commit fd09014

Please sign in to comment.