2023-01-26 16:11:55 +00:00
|
|
|
import React from 'react'
|
|
|
|
import { Button } from 'react-bootstrap'
|
|
|
|
import styles from './pay-bounty.module.css'
|
|
|
|
import ActionTooltip from './action-tooltip'
|
|
|
|
import ModalButton from './modal-button'
|
|
|
|
import { useMutation, gql } from '@apollo/client'
|
|
|
|
import { useMe } from './me'
|
|
|
|
import { abbrNum } from '../lib/format'
|
|
|
|
import { useShowModal } from './modal'
|
|
|
|
import FundError from './fund-error'
|
2023-05-06 21:51:17 +00:00
|
|
|
import { useRoot } from './root'
|
2023-01-26 16:11:55 +00:00
|
|
|
|
|
|
|
export default function PayBounty ({ children, item }) {
|
|
|
|
const me = useMe()
|
|
|
|
const showModal = useShowModal()
|
2023-05-06 21:51:17 +00:00
|
|
|
const root = useRoot()
|
2023-01-26 16:11:55 +00:00
|
|
|
|
|
|
|
const [act] = useMutation(
|
|
|
|
gql`
|
|
|
|
mutation act($id: ID!, $sats: Int!) {
|
|
|
|
act(id: $id, sats: $sats) {
|
|
|
|
sats
|
|
|
|
}
|
|
|
|
}`, {
|
|
|
|
update (cache, { data: { act: { sats } } }) {
|
|
|
|
cache.modify({
|
|
|
|
id: `Item:${item.id}`,
|
|
|
|
fields: {
|
|
|
|
sats (existingSats = 0) {
|
|
|
|
return existingSats + sats
|
|
|
|
},
|
|
|
|
meSats (existingSats = 0) {
|
|
|
|
return existingSats + sats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// update all ancestor comment sats
|
|
|
|
item.path.split('.').forEach(id => {
|
|
|
|
if (Number(id) === Number(item.id)) return
|
|
|
|
cache.modify({
|
|
|
|
id: `Item:${id}`,
|
|
|
|
fields: {
|
|
|
|
commentSats (existingCommentSats = 0) {
|
|
|
|
return existingCommentSats + sats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// update root bounty status
|
|
|
|
cache.modify({
|
2023-05-06 21:51:17 +00:00
|
|
|
id: `Item:${root.id}`,
|
2023-01-26 16:11:55 +00:00
|
|
|
fields: {
|
|
|
|
bountyPaidTo (existingPaidTo = []) {
|
2023-01-26 23:28:10 +00:00
|
|
|
return [...(existingPaidTo || []), Number(item.id)]
|
2023-01-26 16:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePayBounty = async () => {
|
|
|
|
try {
|
|
|
|
await act({
|
2023-05-06 21:51:17 +00:00
|
|
|
variables: { id: item.id, sats: root.bounty },
|
2023-01-26 16:11:55 +00:00
|
|
|
optimisticResponse: {
|
|
|
|
act: {
|
|
|
|
id: `Item:${item.id}`,
|
2023-05-06 21:51:17 +00:00
|
|
|
sats: root.bounty
|
2023-01-26 16:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
if (error.toString().includes('insufficient funds')) {
|
|
|
|
showModal(onClose => {
|
|
|
|
return <FundError onClose={onClose} />
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
throw new Error({ message: error.toString() })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:51:17 +00:00
|
|
|
if (!me || item.mine || root.user.name !== me.name) {
|
2023-01-26 16:11:55 +00:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ActionTooltip
|
|
|
|
notForm
|
2023-05-06 21:51:17 +00:00
|
|
|
overlayText={`${root.bounty} sats`}
|
2023-01-26 16:11:55 +00:00
|
|
|
>
|
|
|
|
<ModalButton
|
|
|
|
clicker={
|
|
|
|
<div className={styles.pay}>
|
|
|
|
pay bounty
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div className='text-center font-weight-bold text-muted'>
|
|
|
|
Pay this bounty to {item.user.name}?
|
|
|
|
</div>
|
|
|
|
<div className='text-center'>
|
|
|
|
<Button className='mt-4' variant='primary' onClick={() => handlePayBounty()}>
|
2023-05-06 21:51:17 +00:00
|
|
|
pay <small>{abbrNum(root.bounty)} sats</small>
|
2023-01-26 16:11:55 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</ModalButton>
|
|
|
|
</ActionTooltip>
|
|
|
|
)
|
|
|
|
}
|