2022-08-30 15:38:59 +00:00
|
|
|
import { HeartIcon } from '@heroicons/react/outline'
|
|
|
|
import { Button } from 'web/components/button'
|
|
|
|
import React from 'react'
|
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import { useUserLikes } from 'web/hooks/use-likes'
|
|
|
|
import toast from 'react-hot-toast'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
2022-08-30 23:13:25 +00:00
|
|
|
import { likeContract } from 'web/lib/firebase/likes'
|
2022-08-30 15:38:59 +00:00
|
|
|
import { LIKE_TIP_AMOUNT } from 'common/like'
|
|
|
|
import clsx from 'clsx'
|
2022-08-30 23:13:25 +00:00
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
2022-08-30 15:38:59 +00:00
|
|
|
|
|
|
|
export function LikeMarketButton(props: {
|
|
|
|
contract: Contract
|
|
|
|
user: User | null | undefined
|
|
|
|
}) {
|
|
|
|
const { contract, user } = props
|
|
|
|
|
|
|
|
const likes = useUserLikes(user?.id)
|
2022-08-30 23:13:25 +00:00
|
|
|
const userLikedContractIds = likes
|
2022-08-30 15:38:59 +00:00
|
|
|
?.filter((l) => l.type === 'contract')
|
|
|
|
.map((l) => l.id)
|
|
|
|
|
|
|
|
const onLike = async () => {
|
2022-08-30 23:13:25 +00:00
|
|
|
if (!user) return firebaseLogin()
|
2022-08-30 15:38:59 +00:00
|
|
|
await likeContract(user, contract)
|
|
|
|
toast(`You tipped ${contract.creatorName} ${formatMoney(LIKE_TIP_AMOUNT)}!`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
size={'lg'}
|
|
|
|
className={'mb-1'}
|
|
|
|
color={'gray-white'}
|
|
|
|
onClick={onLike}
|
|
|
|
>
|
2022-08-30 23:13:25 +00:00
|
|
|
<Col className={'sm:flex-row sm:gap-x-2'}>
|
2022-08-30 15:38:59 +00:00
|
|
|
<HeartIcon
|
|
|
|
className={clsx(
|
|
|
|
'h-6 w-6',
|
2022-08-30 23:13:25 +00:00
|
|
|
user &&
|
|
|
|
(userLikedContractIds?.includes(contract.id) ||
|
|
|
|
(!likes && contract.likedByUserIds?.includes(user.id)))
|
2022-08-30 15:38:59 +00:00
|
|
|
? 'fill-red-500 text-red-500'
|
|
|
|
: ''
|
|
|
|
)}
|
|
|
|
/>
|
2022-08-30 23:13:25 +00:00
|
|
|
Tip
|
|
|
|
</Col>
|
2022-08-30 15:38:59 +00:00
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
}
|