Skip to content

Commit

Permalink
Mep (#37)
Browse files Browse the repository at this point in the history
* fix: use createdBy along with redactedBy to allow user to edit a report

* refactor: add useCanEdit hook
  • Loading branch information
ledouxm authored Sep 24, 2024
1 parent 5a0ea5b commit 727a222
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
12 changes: 8 additions & 4 deletions packages/frontend/src/features/ReportActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement, { report: ReportWithUser }>(({ report }, ref) => {
const user = useUser()!;

const isOwner = report.createdBy === user.id;
const canEdit = useCanEditReport(report);

const navigate = useNavigate();

Expand All @@ -32,16 +33,19 @@ export const ReportActions = forwardRef<HTMLDivElement, { report: ReportWithUser
data: {
...payload,
id: `report-${v4()}`,
title: `${report.title} - copie`,
title: `${report.title ?? "Sans titre"} - copie`,
createdAt: new Date(),
redactedBy: user.name,
redactedById: user.id,
createdBy: user.id,
pdf: undefined,
},
});
});

return (
<Stack ref={ref} gap="0">
{isOwner ? (
{canEdit ? (
<>
<ReportAction
iconId="ri-pencil-line"
Expand All @@ -51,7 +55,7 @@ export const ReportActions = forwardRef<HTMLDivElement, { report: ReportWithUser
<Divider height="1px" color="#DDD" />
</>
) : null}
{isOwner ? (
{canEdit ? (
<>
<ReportAction
iconId="ri-delete-bin-2-line"
Expand Down
19 changes: 19 additions & 0 deletions packages/frontend/src/hooks/useCanEditReport.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Report } from "@cr-vif/electric-client/frontend";
import { useUser } from "../contexts/AuthContext";
import { useLiveQuery } from "electric-sql/react";
import { db } from "../db";

export const useCanEditReport = (report: Report) => {
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;
};
13 changes: 2 additions & 11 deletions packages/frontend/src/routes/edit.$reportId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -64,23 +64,14 @@ function useFormWithFocus<TFieldValues extends FieldValues = FieldValues>(props:

return [form, () => focusedRef.current] as const;
}

const WithReport = ({ report }: { report: Report }) => {
const { tab } = Route.useSearch();
const [form, getFocused] = useFormWithFocus<Report>({
defaultValues: 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();

Expand Down

0 comments on commit 727a222

Please sign in to comment.