Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | 14x | import { Dispatch, SetStateAction } from "react"; import { UseFormReset, FieldValues } from "react-hook-form"; import { useMutation } from "react-query"; import { addDateToFilename, getListingChanges } from "../utils"; import type { ListingData } from "../types"; type Options = { data: ListingData; dirtyFields: { [key: string]: boolean }; getDefaultData: (arg: ListingData) => { [key: string]: unknown }; refetch: () => void; reset: UseFormReset<FieldValues>; setShowSuccessNotification: Dispatch<SetStateAction<boolean>>; setUpdateMetadataOnRelease: Dispatch<SetStateAction<boolean>>; shouldShowUpdateMetadataWarning: (arg: FieldValues) => boolean; snapName: string | undefined; }; function useMutateListingData({ data, dirtyFields, refetch, reset, setShowSuccessNotification, setUpdateMetadataOnRelease, shouldShowUpdateMetadataWarning, snapName, }: Options) { return useMutation({ mutationFn: async (values: FieldValues) => { const formData = new FormData(); const changes = getListingChanges(dirtyFields, values, data); formData.set("csrf_token", window.CSRF_TOKEN); formData.set("snap_id", data.snap_id); if (values.icon && values.icon.length > 0) { formData.append("icon", values.icon[0]); } if (values.banner && values.banner.length > 0) { formData.append("banner-image", values.banner[0]); } if (values.screenshots) { values.screenshots.forEach((screenshot: FileList) => { if (screenshot[0]) { const oldName = screenshot[0].name; const newFile = addDateToFilename(screenshot[0], new Date()); formData.append("screenshots", newFile); const imageIndex = changes?.images?.findIndex( (image: { url: string; type: string; status: string; name?: string; }) => image.name === oldName, ); if (changes.images && imageIndex) { changes.images[imageIndex].name = newFile.name; changes.images[imageIndex].url = URL.createObjectURL(newFile); } } }); } formData.set("changes", JSON.stringify(changes)); const response = await fetch(`/api/${snapName}/listing`, { method: "POST", body: formData, }); if (shouldShowUpdateMetadataWarning(values)) { setUpdateMetadataOnRelease(false); } if (!response.ok) { throw new Error("There was a problem saving listing data"); } }, onSuccess: () => { const mainPanel = document.querySelector(".l-main") as HTMLElement; mainPanel.scrollTo({ top: 0, left: 0, behavior: "smooth" }); refetch(); setShowSuccessNotification(true); }, onSettled: (_error, _context, data) => { if (data) { reset(data); } else { reset(); } }, }); } export default useMutateListingData; |