Skip to content

Commit

Permalink
Update actiongrants and minimize fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasdeluna committed Feb 26, 2024
1 parent 8ab8093 commit 69a10db
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/routes/articles/components/ArticleEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ const ArticleEditor = () => {
id="article-title"
/>
<Field
placeholder="Skriv tråden din her"
placeholder="Skriv artikkelen din her ..."
name="content"
label="Beskrivelse"
label="Innhold"
component={EditorField.Field}
/>

Expand Down
16 changes: 5 additions & 11 deletions app/routes/forum/components/ForumDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { usePreparedEffect } from '@webkom/react-prepare';
import { useParams } from 'react-router-dom';
import { fetchForum, fetchForums } from 'app/actions/ForumActions';
import { fetchForum } from 'app/actions/ForumActions';
import { Content, ContentMain } from 'app/components/Content';
import NavigationTab, { NavigationLink } from 'app/components/NavigationTab';
import { selectForumsById } from 'app/reducers/forums';
Expand All @@ -12,13 +12,6 @@ const ForumDetail = () => {
const { forumId } = useParams<{ forumId: string }>();
const dispatch = useAppDispatch();

//This is needed due to action_grant not being sent on detail action for some reason. Should be fixed.
usePreparedEffect(
'fetchAllForums',
() => forumId && dispatch(fetchForums()),
[forumId]
);

usePreparedEffect(
'fetchDetailForum',
() => forumId && dispatch(fetchForum(forumId)),
Expand All @@ -28,10 +21,11 @@ const ForumDetail = () => {
const forum: DetailedForum = useAppSelector((state) =>
selectForumsById(state, { forumId })
);
const actionGrant = useAppSelector((state) => state.forums.actionGrant);
const detailActionGrant = forum?.actionGrant;

return (
forum && (
forum &&
detailActionGrant && (
<Content>
<ContentMain>
<NavigationTab
Expand All @@ -40,7 +34,7 @@ const ForumDetail = () => {
path: '/forum',
}}
>
{actionGrant.includes('edit') && (
{detailActionGrant.includes('edit') && (
<NavigationLink to={`/forum/${forumId}/edit`}>
Rediger
</NavigationLink>
Expand Down
2 changes: 1 addition & 1 deletion app/routes/forum/components/ForumEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const ForumEditor = () => {
<Button
flat
onClick={() =>
navigate(`/forum/${isNew ? '' : forumId}/threads`)
navigate(`/forum/${isNew ? '' : forumId + '/threads'}`)
}
>
Avbryt
Expand Down
2 changes: 1 addition & 1 deletion app/routes/forum/components/ForumList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ForumList = () => {

usePreparedEffect('fetchAllForums', () => dispatch(fetchForums()), []);

const forums = useAppSelector((state) => selectForums(state));
const forums: PublicForum[] = useAppSelector((state) => selectForums(state));
const actionGrant = useAppSelector((state) => state.forums.actionGrant);

return (
Expand Down
7 changes: 4 additions & 3 deletions app/routes/forum/components/ThreadDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ const ThreadDetail = () => {
selectCommentsByIds(state, thread ? thread.comments : [])
);

const actionGrant = useAppSelector((state) => state.threads.actionGrant);
const detailActionGrant = thread?.actionGrant;

return (
thread && (
thread &&
detailActionGrant && (
<Content>
<ContentMain>
<NavigationTab
Expand All @@ -45,7 +46,7 @@ const ThreadDetail = () => {
}}
>
{(thread.createdBy?.id === currentUser.id ||
actionGrant.includes('edit')) && (
detailActionGrant.includes('edit')) && (
<NavigationLink to={`/forum/${forumId}/threads/${threadId}/edit`}>
Rediger
</NavigationLink>
Expand Down
4 changes: 2 additions & 2 deletions app/routes/forum/components/ThreadEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const ThreadEditor = () => {
id="thread-title"
/>
<Field
placeholder="Skriv artikkelen din her ..."
placeholder="Skriv innholdet i tråden din her..."
name="content"
label="Innhold"
component={EditorField.Field}
Expand All @@ -117,7 +117,7 @@ const ThreadEditor = () => {
<SubmitButton>
{isNew ? 'Opprett' : 'Lagre endringer'}
</SubmitButton>
{!isNew && (
{!isNew && thread.actionGrant.includes('delete') && (
<ConfirmModal
title="Slett tråd"
message="Er du sikker på at du vil slette tråden?"
Expand Down
5 changes: 4 additions & 1 deletion app/store/models/Forum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Comment } from './Comment';
import type { PublicUser } from './User';
import type { ContentTarget } from '../utils/contentTarget';
import type { Dateish } from 'app/models';
import type { ActionGrant, Dateish } from 'app/models';
import type { ID } from 'app/store/models';

export interface CreateThread {
Expand All @@ -26,6 +26,7 @@ export interface DetailedThread extends PublicThread {
comments?: Comment[];
createdBy?: PublicUser;
contentTarget: ContentTarget;
actionGrant: ActionGrant;
}

export interface CreateForum {
Expand All @@ -42,10 +43,12 @@ export interface PublicForum {
title: string;
description: string;
createdAt: Dateish;
actionGrant: ActionGrant;
}

export interface DetailedForum extends PublicForum {
threads?: PublicThread[];
created_by?: PublicUser;
contentTarget: string;
actionGrant: ActionGrant;
}

0 comments on commit 69a10db

Please sign in to comment.