2022-06-23 08:07:52 +00:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useState } from 'react'
|
|
|
|
import { SEO } from 'web/components/SEO'
|
|
|
|
import { Title } from 'web/components/title'
|
2022-07-10 22:03:15 +00:00
|
|
|
import { claimManalink } from 'web/lib/firebase/api'
|
2022-06-23 08:07:52 +00:00
|
|
|
import { useManalink } from 'web/lib/firebase/manalinks'
|
|
|
|
import { ManalinkCard } from 'web/components/manalink-card'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
|
|
|
|
|
|
|
export default function ClaimPage() {
|
|
|
|
const user = useUser()
|
|
|
|
const router = useRouter()
|
|
|
|
const { slug } = router.query as { slug: string }
|
|
|
|
const manalink = useManalink(slug)
|
|
|
|
const [claiming, setClaiming] = useState(false)
|
|
|
|
const [error, setError] = useState<string | undefined>(undefined)
|
|
|
|
|
|
|
|
if (!manalink) {
|
|
|
|
return <></>
|
|
|
|
}
|
|
|
|
|
|
|
|
const info = { ...manalink, uses: manalink.claims.length }
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<SEO
|
|
|
|
title="Send Mana"
|
|
|
|
description="Send mana to anyone via link!"
|
|
|
|
url="/send"
|
|
|
|
/>
|
|
|
|
<div className="mx-auto max-w-xl">
|
2022-07-01 15:40:43 +00:00
|
|
|
<Title text={`Claim M$${manalink.amount} mana`} />
|
2022-06-23 08:07:52 +00:00
|
|
|
<ManalinkCard
|
2022-07-16 01:42:37 +00:00
|
|
|
user={user}
|
2022-06-23 08:07:52 +00:00
|
|
|
info={info}
|
|
|
|
isClaiming={claiming}
|
|
|
|
onClaim={async () => {
|
|
|
|
setClaiming(true)
|
|
|
|
try {
|
|
|
|
if (user == null) {
|
|
|
|
await firebaseLogin()
|
2022-07-16 01:42:37 +00:00
|
|
|
setClaiming(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (user?.id == manalink.fromId) {
|
|
|
|
throw new Error("You can't claim your own manalink.")
|
2022-06-23 08:07:52 +00:00
|
|
|
}
|
2022-07-08 22:28:04 +00:00
|
|
|
await claimManalink({ slug: manalink.slug })
|
2022-07-04 13:49:41 +00:00
|
|
|
user && router.push(`/${user.username}?claimed-mana=yes`)
|
2022-06-23 08:07:52 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
const message =
|
|
|
|
e && e instanceof Object ? e.toString() : 'An error occurred.'
|
|
|
|
setError(message)
|
|
|
|
}
|
|
|
|
setClaiming(false)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{error && (
|
|
|
|
<section className="my-5 text-red-500">
|
|
|
|
<p>Failed to claim manalink.</p>
|
|
|
|
<p>{error}</p>
|
|
|
|
</section>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|