From 9eab1beadc8e35fe81b52ddd05bc8df328c33ac7 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 7 Aug 2022 14:11:23 +0100 Subject: [PATCH] feat: mint post --- src/components/icons/CollectIcon.tsx | 5 +- src/components/site/PostFooter.tsx | 74 ++++++++++++++++++++++++++-- src/models/page.model.ts | 33 +++++++++++++ src/queries/page.ts | 48 ++++++++++++++++++ 4 files changed, 154 insertions(+), 6 deletions(-) diff --git a/src/components/icons/CollectIcon.tsx b/src/components/icons/CollectIcon.tsx index 7d9aa639..95b23125 100644 --- a/src/components/icons/CollectIcon.tsx +++ b/src/components/icons/CollectIcon.tsx @@ -12,7 +12,10 @@ export const CollectIcon: React.FC> = ( version="1.1" xmlns="http://www.w3.org/2000/svg" > - + ) } diff --git a/src/components/site/PostFooter.tsx b/src/components/site/PostFooter.tsx index fa7d4128..12ec8d95 100644 --- a/src/components/site/PostFooter.tsx +++ b/src/components/site/PostFooter.tsx @@ -4,8 +4,11 @@ import { CollectIcon } from "~/components/icons/CollectIcon" import { useLikePage, useUnlikePage, + useMintPage, useGetLikes, useCheckLike, + useGetMints, + useCheckMint, } from "~/queries/page" import { useAccount } from "wagmi" import { useConnectModal } from "@rainbow-me/rainbowkit" @@ -17,9 +20,12 @@ export const PostFooter: React.FC<{ }> = ({ page }) => { const likePage = useLikePage() const unlikePage = useUnlikePage() + const mintPage = useMintPage() + const { address } = useAccount() const { openConnectModal } = useConnectModal() const [likeProgress, setLikeProgress] = useState(false) + const [mintProgress, setMintProgress] = useState(false) const like = async () => { if (!address) { @@ -27,12 +33,29 @@ export const PostFooter: React.FC<{ openConnectModal?.() } else if (page?.id) { if (isLike.data?.count) { - unlikePage.mutate({ + // unlikePage.mutate({ + // address, + // pageId: page?.id, + // }) + // TODO + } else { + likePage.mutate({ address, pageId: page?.id, }) + } + } + } + + const mint = async () => { + if (!address) { + setMintProgress(true) + openConnectModal?.() + } else if (page?.id) { + if (isMint.data?.count) { + // TODO } else { - likePage.mutate({ + mintPage.mutate({ address, pageId: page?.id, }) @@ -48,6 +71,33 @@ export const PostFooter: React.FC<{ pageId: page?.id, }) + const mints = useGetMints({ + pageId: page?.id, + }) + const isMint = useCheckMint({ + address, + pageId: page?.id, + }) + + useEffect(() => { + if (mintProgress && address && isMint.isSuccess && page?.id) { + if (!isMint.data.count) { + mintPage.mutate({ + address, + pageId: page?.id, + }) + } + setMintProgress(false) + } + }, [ + mintProgress, + address, + isMint.isSuccess, + isMint.data?.count, + mintPage, + page?.id, + ]) + useEffect(() => { if (likeProgress && address && isLike.isSuccess && page?.id) { if (!isLike.data.count) { @@ -68,7 +118,7 @@ export const PostFooter: React.FC<{ ]) return ( -
+
-
) diff --git a/src/models/page.model.ts b/src/models/page.model.ts index c33ce758..a3944209 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -363,3 +363,36 @@ export async function checkLike({ }) } } + +export async function mintPage({ + address, + pageId, +}: { + address: string + pageId: string +}) { + return contract?.mintNote(pageId.split("-")[0], pageId.split("-")[1], address) +} + +export async function getMints({ pageId }: { pageId: string }) { + return indexer.getMintedNotesOfNote( + pageId.split("-")[0], + pageId.split("-")[1], + { + limit: 0, + }, + ) +} + +export async function checkMint({ + address, + pageId, +}: { + address: string + pageId: string +}) { + return indexer.getMintedNotesOfAddress(address, { + noteCharacterId: pageId.split("-")[0], + noteId: pageId.split("-")[1], + }) +} diff --git a/src/queries/page.ts b/src/queries/page.ts index abf6e62f..723f46ff 100644 --- a/src/queries/page.ts +++ b/src/queries/page.ts @@ -45,6 +45,35 @@ export const useCheckLike = (input: { address?: string; pageId?: string }) => { }) } +export const useGetMints = (input: { pageId?: string }) => { + return useQuery(["getMints", input.pageId], async () => { + if (!input.pageId) { + return { + count: 0, + list: [], + } + } + return pageModel.getMints({ + pageId: input.pageId, + }) + }) +} + +export const useCheckMint = (input: { address?: string; pageId?: string }) => { + return useQuery(["checkMint", input.pageId, input.address], async () => { + if (!input.pageId || !input.address) { + return { + count: 0, + list: [], + } + } + return pageModel.checkMint({ + address: input.address, + pageId: input.pageId, + }) + }) +} + export function useCreateOrUpdatePage() { const queryClient = useQueryClient() const mutation = useMutation( @@ -113,3 +142,22 @@ export function useUnlikePage() { }, ) } + +export function useMintPage() { + const queryClient = useQueryClient() + return useMutation( + async (input: Parameters[0]) => { + return pageModel.mintPage(input) + }, + { + onSuccess: (data, variables) => { + queryClient.invalidateQueries([ + "checkMint", + variables.pageId, + variables.address, + ]) + queryClient.invalidateQueries(["getMints", variables.pageId]) + }, + }, + ) +}