Skip to content

Commit

Permalink
fix: correctly clear images on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Oct 14, 2024
1 parent e1b77f2 commit 4969eb9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/frontend/src/features/InfoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useUser } from "../contexts/AuthContext";
import { db } from "../db";
import { useIsFormDisabled } from "./DisabledContext";
import { ServiceInstructeurSelect } from "./ServiceInstructeurSelect";
import { getPicturesStore, getToUploadStore, getUploadStatusStore, syncImages } from "./idb";
import { deleteImageFromIdb, getPicturesStore, getToUploadStore, getUploadStatusStore, syncImages } from "./idb";

export const InfoForm = () => {
const form = useFormContext<Report>();
Expand Down Expand Up @@ -286,7 +286,11 @@ const ReportPictures = ({ statusMap }: { statusMap: Record<string, string> }) =>
};

const PictureThumbnail = ({ picture, index, status }: { picture: Pictures; index: number; status?: string }) => {
const deletePictureMutation = useMutation(() => db.pictures.delete({ where: { id: picture.id } }));
const deletePictureMutation = useMutation(async () => {
await deleteImageFromIdb(picture.id);

return db.pictures.delete({ where: { id: picture.id } });
});

const bgUrlQuery = useQuery({
queryKey: ["picture", picture.id, picture.url],
Expand Down
8 changes: 7 additions & 1 deletion packages/frontend/src/features/idb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStore } from "idb-keyval";
import { createStore, del } from "idb-keyval";

export const getPicturesStore = () => createStore("toSync", "images");
export const getToUploadStore = () => createStore("toUpload", "images");
Expand All @@ -9,3 +9,9 @@ export const syncImages = async () => {
const registration = await navigator.serviceWorker.ready;
await registration.sync.register("images");
};

export const deleteImageFromIdb = async (id: string) => {
await del(id, getPicturesStore());
await del(id, getToUploadStore());
await del(id, getUploadStatusStore());
};

0 comments on commit 4969eb9

Please sign in to comment.