From 727a2223a576ebb945d2e45964f500e4358a97d0 Mon Sep 17 00:00:00 2001 From: Martin Ledoux <32564108+ledouxm@users.noreply.github.com> Date: Tue, 24 Sep 2024 09:52:05 +0200 Subject: [PATCH] Mep (#37) * fix: use createdBy along with redactedBy to allow user to edit a report * refactor: add useCanEdit hook --- .../frontend/src/features/ReportActions.tsx | 12 ++++++++---- .../frontend/src/hooks/useCanEditReport.tsx | 19 +++++++++++++++++++ .../frontend/src/routes/edit.$reportId.tsx | 13 ++----------- 3 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 packages/frontend/src/hooks/useCanEditReport.tsx diff --git a/packages/frontend/src/features/ReportActions.tsx b/packages/frontend/src/features/ReportActions.tsx index 09d5b87e..0b03f886 100644 --- a/packages/frontend/src/features/ReportActions.tsx +++ b/packages/frontend/src/features/ReportActions.tsx @@ -11,11 +11,12 @@ import { omit } from "pastable"; import { ReportWithUser } from "./ReportList"; import { useNavigate } from "@tanstack/react-router"; import { api } from "../api"; +import { useCanEditReport } from "../hooks/useCanEditReport"; export const ReportActions = forwardRef(({ report }, ref) => { const user = useUser()!; - const isOwner = report.createdBy === user.id; + const canEdit = useCanEditReport(report); const navigate = useNavigate(); @@ -32,8 +33,11 @@ export const ReportActions = forwardRef - {isOwner ? ( + {canEdit ? ( <> ) : null} - {isOwner ? ( + {canEdit ? ( <> { + const user = useUser()!; + const isOwner = report.redactedById === user.id; + const isCreator = report.createdBy === user.id; + + const userDelegations = useLiveQuery( + db.delegation.liveFirst({ where: { createdBy: report.createdBy, delegatedTo: user.id } }), + ); + + const hasDelegation = !!userDelegations.results; + const canEdit = isOwner || isCreator || hasDelegation; + + return canEdit; +}; diff --git a/packages/frontend/src/routes/edit.$reportId.tsx b/packages/frontend/src/routes/edit.$reportId.tsx index 27b119d7..b0801429 100644 --- a/packages/frontend/src/routes/edit.$reportId.tsx +++ b/packages/frontend/src/routes/edit.$reportId.tsx @@ -20,7 +20,7 @@ import { db } from "../db"; import { InfoForm } from "../features/InfoForm"; import { NotesForm } from "../features/NotesForm"; import { DisabledContext } from "../features/DisabledContext"; -import { useUser } from "../contexts/AuthContext"; +import { useCanEditReport } from "../hooks/useCanEditReport"; const EditReport = () => { const { reportId } = Route.useParams(); @@ -64,7 +64,6 @@ function useFormWithFocus(props: return [form, () => focusedRef.current] as const; } - const WithReport = ({ report }: { report: Report }) => { const { tab } = Route.useSearch(); const [form, getFocused] = useFormWithFocus({ @@ -72,15 +71,7 @@ const WithReport = ({ report }: { report: Report }) => { resetOptions: {}, }); - const user = useUser()!; - const isOwner = report.redactedById === user.id; - - const userDelegations = useLiveQuery( - db.delegation.liveFirst({ where: { createdBy: report.createdBy, delegatedTo: user.id } }), - ); - - const hasDelegation = !!userDelegations.results; - const canEdit = isOwner || hasDelegation; + const canEdit = useCanEditReport(report); const navigate = useNavigate();