Skip to content

Commit

Permalink
fix: display correct status
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Nov 15, 2024
1 parent 1086abe commit 407e961
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 0 additions & 2 deletions packages/frontend/src/features/InfoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ export const InfoForm = () => {
<Divider mt="20px" mb="52px" />

<InputGroupWithTitle title="Le projet">
<UploadImage reportId={form.getValues().id} />

<Input
className={css({ mb: { base: "24px", lg: undefined } })}
label="Description"
Expand Down
3 changes: 3 additions & 0 deletions packages/frontend/src/features/NotesForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { css } from "#styled-system/css";
import { FurtherInfoChips } from "#components/chips/FurtherInfoChips";
import Button from "@codegouvfr/react-dsfr/Button";
import { useIsFormDisabled } from "./DisabledContext";
import { UploadImage } from "./upload/UploadImage";

export const NotesForm = () => {
const form = useFormContext<Report>();
Expand All @@ -28,6 +29,8 @@ export const NotesForm = () => {
/>
</InputGroupWithTitle>

<UploadImage reportId={form.getValues().id} />

<Divider mt="36px" mb="52px" />

<Stack gap={{ base: "0", lg: "16px" }} direction={{ base: "column", lg: "row" }}>
Expand Down
12 changes: 10 additions & 2 deletions packages/frontend/src/features/upload/UploadImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ export const UploadImage = ({ reportId }: { reportId: string }) => {

const notifyPictureLines = useMutation(async ({ pictureId, lines }: { pictureId: string; lines: Array<Line> }) => {
try {
emitterChannel.postMessage({ type: "status", id: pictureId, status: "uploading" });

const result = await api.post(`/api/upload/picture/${pictureId}/lines` as any, { body: { lines } });
await del(pictureId, getToPingStore());

emitterChannel.postMessage({ type: "status", id: pictureId, status: "success" });

return result;
} catch (e) {
await set(pictureId, JSON.stringify(lines), getToPingStore());
Expand Down Expand Up @@ -73,11 +77,14 @@ export const UploadImage = ({ reportId }: { reportId: string }) => {
formData.append("file", new Blob([buffer]), "file");
formData.append("reportId", reportId);
formData.append("pictureId", picId);
emitterChannel.postMessage({ type: "status", id: picId, status: "uploading" });

await api.post("/api/upload/image", {
body: formData,
query: { reportId: reportId, id: picId },
} as any);

emitterChannel.postMessage({ type: "status", id: picId, status: "success" });
} catch {
await set(picId, reportId, getToUploadStore());
syncImages();
Expand All @@ -93,7 +100,7 @@ export const UploadImage = ({ reportId }: { reportId: string }) => {

useEffect(() => {
const listener = (event: MessageEvent) => {
console.log("message", event.data);
console.log(event.data);
if (event.data.type === "status") {
console.log("status", event.data.id, event.data.status);
setStatusMap((prev) => ({ ...prev, [event.data.id]: event.data.status }));
Expand Down Expand Up @@ -149,6 +156,7 @@ export const UploadImage = ({ reportId }: { reportId: string }) => {
};

const broadcastChannel = new BroadcastChannel("sw-messages");
const emitterChannel = new BroadcastChannel("sw-messages");

const ReportPictures = ({
statusMap,
Expand Down Expand Up @@ -315,7 +323,7 @@ const PictureThumbnail = ({
};
};

const finalStatus = picture.url ? "success" : status ?? idbStatusQuery.data ?? "uploading";
const finalStatus = status ?? idbStatusQuery.data ?? "uploading";

const bgUrl = bgUrlQuery.data;

Expand Down
2 changes: 0 additions & 2 deletions packages/frontend/src/routes/pdf.$reportId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ export const PDF = () => {
meetDate: reportQuery.results!.meetDate?.toISOString(),
});

console.log({ snapshotReport, report: reportQuery.results, diff });

if (Object.keys(diff).length) return null;

return snapshot.html!;
Expand Down
5 changes: 5 additions & 0 deletions packages/frontend/src/service-worker/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ const syncPictureLines = async () => {

for (let i = 0; i < pictureIds.length; i++) {
const picId = pictureIds[i];
await set(picId, "uploading", getUploadStatusStore());
console.log("syncing picture lines for", picId);

const linesRaw = await get(picId, getToPingStore());
const lines = JSON.parse(linesRaw);
broadcastChannel.postMessage({ type: "status", id: picId, status: "uploading" });

await api.post(
`/api/upload/picture/${picId}/lines` as any,
Expand All @@ -65,6 +67,9 @@ const syncPictureLines = async () => {
);

await del(picId, getToPingStore());

broadcastChannel.postMessage({ type: "status", id: picId, status: "success" });
await set(picId, "success", getUploadStatusStore());
}
} catch (e) {
console.error("sync error", e);
Expand Down

0 comments on commit 407e961

Please sign in to comment.