Skip to content

Commit

Permalink
refactor: custom hook for loading draft logic (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricio0312rev authored Dec 5, 2023
1 parent e343050 commit 4f56f61
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 81 deletions.
2 changes: 1 addition & 1 deletion resources/js/Hooks/useAuthorizedAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from "axios";
import { useAuth } from "@/Contexts/AuthContext";
import { useMetaMaskContext } from "@/Contexts/MetaMaskContext";

type SignedActionAction =
export type SignedActionAction =
| (({ authenticated, signed }: { authenticated: boolean; signed: boolean }) => Promise<void>)
| (({ authenticated, signed }: { authenticated: boolean; signed: boolean }) => void);

Expand Down
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>
</>
);
};
100 changes: 20 additions & 80 deletions resources/js/Pages/Galleries/MyGalleries/Create.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { type PageProps, type VisitOptions } from "@inertiajs/core";
import { Head, router, usePage } from "@inertiajs/react";
import axios from "axios";
import { type FormEvent, type MouseEvent, useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import CreateGalleryForm from "./Components/CreateGalleryForm";
import { ConfirmDeletionDialog } from "@/Components/ConfirmDeletionDialog";
import { DraftsLimitDialog } from "@/Components/Galleries/DraftsLimitDialog";
import { MyGalleryDialogs } from "./Components/MyGalleryDialogs";
import { useDraftLoader } from "./hooks/useDraftLoader";
import { GalleryActionToolbar } from "@/Components/Galleries/GalleryPage/GalleryActionToolbar";
import { GalleryFormSlider, GalleryFormSliderTabs } from "@/Components/Galleries/GalleryPage/GalleryFormSlider";
import { LayoutWrapper } from "@/Components/Layout/LayoutWrapper";
Expand All @@ -17,9 +15,7 @@ import { useToasts } from "@/Hooks/useToasts";
import { useGalleryForm } from "@/Pages/Galleries/hooks/useGalleryForm";
import { type GalleryDraftUnsaved, useWalletDraftGalleries } from "@/Pages/Galleries/hooks/useWalletDraftGalleries";
import { useWalletDraftGallery } from "@/Pages/Galleries/hooks/useWalletDraftGallery";
import { arrayBufferToFile } from "@/Utils/array-buffer-to-file";
import { assertUser, assertWallet } from "@/Utils/assertions";
import { fileToImageDataURI } from "@/Utils/file-to-image-data-uri";
import { getQueryParameters } from "@/Utils/get-query-parameters";
import { isTruthy } from "@/Utils/is-truthy";
import { replaceUrlQuery } from "@/Utils/replace-url-query";
Expand Down Expand Up @@ -48,12 +44,9 @@ const Create = ({
assertUser(auth.user);
assertWallet(auth.wallet);

const { t } = useTranslation();
const { showToast } = useToasts();
const { props } = usePage();

const { signedAction } = useAuthorizedAction();

const { initialized } = useMetaMaskContext();

const [isGalleryFormSliderOpen, setIsGalleryFormSliderOpen] = useState(false);
Expand Down Expand Up @@ -151,51 +144,20 @@ const Create = ({
[gallery],
);

const { loadDraftCover, loadDraftNts } = useDraftLoader({
setGalleryCoverImageUrl,
showToast,
setNfts,
setInitialNfts,
});

useEffect(() => {
if (draft.id == null) {
return;
}

const loadDraftCover = async (): 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 (): 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);
};

void loadDraftCover();

void loadDraftNts();
void loadDraftCover({ draft });
void loadDraftNts({ draft });
}, [draft]);

const publishHandler = (event: FormEvent<Element>): void => {
Expand Down Expand Up @@ -322,37 +284,15 @@ const Create = ({
}}
/>

{isTruthy(gallery) && (
<ConfirmDeletionDialog
title={t("pages.galleries.delete_modal.title")}
isOpen={showDeleteModal}
onClose={() => {
setShowDeleteModal(false);
}}
onConfirm={() => {
handleGalleryDelete(gallery.slug);
}}
isDisabled={busy}
>
{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>
<MyGalleryDialogs
gallery={gallery}
showDeleteModal={showDeleteModal}
setShowDeleteModal={setShowDeleteModal}
handleGalleryDelete={handleGalleryDelete}
isBusy={busy}
showDraftsLimitModal={showDraftsLimitModal}
setShowDraftsLimitModal={setShowDraftsLimitModal}
/>
</LayoutWrapper>
);
};
Expand Down
65 changes: 65 additions & 0 deletions resources/js/Pages/Galleries/MyGalleries/hooks/useDraftLoader.ts
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,
};
};

0 comments on commit 4f56f61

Please sign in to comment.