Skip to content

Commit

Permalink
#459 - feat: add frontend permission check for updating reviewer
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvandescheur committed Oct 28, 2024
1 parent ac369fe commit 0db343e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import {
import { FormEvent, useState } from "react";
import { useNavigation, useRevalidator } from "react-router-dom";

import { useReviewers, useSubmitAction } from "../../hooks";
import { useReviewers, useWhoAmI } from "../../hooks";
import {
DestructionList,
reassignDestructionList,
} from "../../lib/api/destructionLists";
import { canReassignDestructionList } from "../../lib/auth/permissions";
import { formatUser } from "../../lib/format/user";

export type DestructionListReviewerProps = {
Expand All @@ -35,6 +36,7 @@ export function DestructionListReviewer({
const revalidator = useRevalidator();
const alert = useAlert();
const reviewers = useReviewers();
const user = useWhoAmI();

/**
* Gets called when the change is confirmed.
Expand Down Expand Up @@ -85,19 +87,26 @@ export function DestructionListReviewer({
value: (
<>
{formatUser(reviewer.user)}
&nbsp;
<Button
aria-label="Beoordelaar bewerken"
disabled={state === "loading" || state === "submitting"}
size="xs"
variant="secondary"
onClick={(e) => {
e.preventDefault();
setModalState(true);
}}
>
<Solid.PencilIcon />
</Button>
{user &&
canReassignDestructionList(user, destructionList) && (
<>
&nbsp;
<Button
aria-label="Beoordelaar bewerken"
disabled={
state === "loading" || state === "submitting"
}
size="xs"
variant="secondary"
onClick={(e) => {
e.preventDefault();
setModalState(true);
}}
>
<Solid.PencilIcon />
</Button>
</>
)}
</>
),
},
Expand Down
1 change: 1 addition & 0 deletions frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./useLatestReviewResponse";
export * from "./usePoll";
export * from "./useReviewers";
export * from "./useSubmitAction";
export * from "./useWhoAmI";
29 changes: 29 additions & 0 deletions frontend/src/hooks/useWhoAmI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useAlert } from "@maykin-ui/admin-ui";
import { useEffect, useState } from "react";

import { User, whoAmI } from "../lib/api/auth";

/**
* Hook resolving the current user
*/
export function useWhoAmI(): User | null {
const alert = useAlert();

const [valueState, setValueState] = useState<ReturnType<
typeof useWhoAmI
> | null>(null);
useEffect(() => {
whoAmI()
.then((value) => setValueState(value))
.catch((e) => {
console.error(e);
alert(
"Foutmelding",
"Er is een fout opgetreden bij het ophalen van de huidige gebruiker!",
"Ok",
);
});
}, []);

return valueState;
}
11 changes: 11 additions & 0 deletions frontend/src/lib/auth/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,14 @@ export function canTriggerDestruction(
destructionList.status === "ready_to_delete"
);
}

export function canReassignDestructionList(
user: User,
destructionList: DestructionList,
) {
return (
canStartDestructionList(user) &&
(destructionList.status === "new" ||
destructionList.status === "ready_to_review")
);
}

0 comments on commit 0db343e

Please sign in to comment.