-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: custom hook for loading draft logic (#517)
- Loading branch information
1 parent
e343050
commit 4f56f61
Showing
4 changed files
with
148 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
resources/js/Pages/Galleries/MyGalleries/Components/MyGalleryDialogs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { router } from "@inertiajs/react"; | ||
import React from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { ConfirmDeletionDialog } from "@/Components/ConfirmDeletionDialog"; | ||
import { DraftsLimitDialog } from "@/Components/Galleries/DraftsLimitDialog"; | ||
import { isTruthy } from "@/Utils/is-truthy"; | ||
|
||
export const MyGalleryDialogs = ({ | ||
gallery, | ||
showDeleteModal, | ||
setShowDeleteModal, | ||
handleGalleryDelete, | ||
isBusy, | ||
showDraftsLimitModal, | ||
setShowDraftsLimitModal, | ||
}: { | ||
gallery?: App.Data.Gallery.GalleryData; | ||
showDeleteModal: boolean; | ||
setShowDeleteModal: (show: boolean) => void; | ||
handleGalleryDelete: (slug: string) => void; | ||
isBusy: boolean; | ||
showDraftsLimitModal: boolean; | ||
setShowDraftsLimitModal: (show: boolean) => void; | ||
}): JSX.Element => { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<> | ||
{isTruthy(gallery) && ( | ||
<ConfirmDeletionDialog | ||
title={t("pages.galleries.delete_modal.title")} | ||
isOpen={showDeleteModal} | ||
onClose={() => { | ||
setShowDeleteModal(false); | ||
}} | ||
onConfirm={() => { | ||
handleGalleryDelete(gallery.slug); | ||
}} | ||
isDisabled={isBusy} | ||
> | ||
{t("pages.galleries.delete_modal.confirmation_text")} | ||
</ConfirmDeletionDialog> | ||
)} | ||
|
||
<DraftsLimitDialog | ||
title={t("pages.galleries.create.drafts_limit_modal_title")} | ||
isOpen={showDraftsLimitModal} | ||
onClose={() => { | ||
setShowDraftsLimitModal(false); | ||
}} | ||
onCancel={() => { | ||
router.visit(route("my-galleries", { draft: 1 })); | ||
}} | ||
onConfirm={() => { | ||
setShowDraftsLimitModal(false); | ||
}} | ||
> | ||
{t("pages.galleries.create.drafts_limit_modal_message")} | ||
</DraftsLimitDialog> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
resources/js/Pages/Galleries/MyGalleries/hooks/useDraftLoader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import axios from "axios"; | ||
import { useTranslation } from "react-i18next"; | ||
import { type ToastMessage } from "@/Components/Toast"; | ||
import { type GalleryDraftUnsaved } from "@/Pages/Galleries/hooks/useWalletDraftGalleries"; | ||
import { arrayBufferToFile } from "@/Utils/array-buffer-to-file"; | ||
import { fileToImageDataURI } from "@/Utils/file-to-image-data-uri"; | ||
|
||
export const useDraftLoader = ({ | ||
setGalleryCoverImageUrl, | ||
showToast, | ||
setNfts, | ||
setInitialNfts, | ||
}: { | ||
setGalleryCoverImageUrl: (url: string) => void; | ||
showToast: (toastMessage?: ToastMessage) => void; | ||
setNfts: (nfts: App.Data.Gallery.GalleryNftData[]) => void; | ||
setInitialNfts: (nfts: App.Data.Gallery.GalleryNftData[]) => void; | ||
}): { | ||
loadDraftCover: ({ draft }: { draft: GalleryDraftUnsaved }) => Promise<void>; | ||
loadDraftNts: ({ draft }: { draft: GalleryDraftUnsaved }) => Promise<void>; | ||
} => { | ||
const { t } = useTranslation(); | ||
|
||
const loadDraftCover = async ({ draft }: { draft: GalleryDraftUnsaved }): Promise<void> => { | ||
const file = arrayBufferToFile(draft.cover, draft.coverFileName, draft.coverType); | ||
|
||
if (file === null) { | ||
setGalleryCoverImageUrl(""); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
const imageDataURI = await fileToImageDataURI(file); | ||
setGalleryCoverImageUrl(imageDataURI); | ||
} catch { | ||
setGalleryCoverImageUrl(""); | ||
} | ||
}; | ||
|
||
const loadDraftNts = async ({ draft }: { draft: GalleryDraftUnsaved }): Promise<void> => { | ||
const { data: nfts } = await axios.get<App.Data.Gallery.GalleryNftData[]>( | ||
route("user.nfts", { | ||
ids: draft.nfts.map((nft) => nft.nftId).join(","), | ||
}), | ||
); | ||
|
||
if (nfts.length < draft.nfts.length) { | ||
showToast({ | ||
message: t("pages.galleries.my_galleries.nfts_no_longer_owned"), | ||
type: "warning", | ||
}); | ||
|
||
setNfts(nfts); | ||
return; | ||
} | ||
|
||
setInitialNfts(nfts); | ||
}; | ||
|
||
return { | ||
loadDraftCover, | ||
loadDraftNts, | ||
}; | ||
}; |