2024-07-01 12:02:29 -05:00
|
|
|
import React from 'react'
|
2023-07-24 13:35:05 -05:00
|
|
|
import Button from 'react-bootstrap/Button'
|
2023-01-26 10:11:55 -06:00
|
|
|
import styles from './pay-bounty.module.css'
|
|
|
|
import ActionTooltip from './action-tooltip'
|
|
|
|
import { useMe } from './me'
|
2024-03-20 01:37:31 +01:00
|
|
|
import { numWithUnits } from '@/lib/format'
|
2023-01-26 10:11:55 -06:00
|
|
|
import { useShowModal } from './modal'
|
2023-05-06 16:51:17 -05:00
|
|
|
import { useRoot } from './root'
|
2024-07-01 12:02:29 -05:00
|
|
|
import { ActCanceledError, useAct } from './item-act'
|
2024-05-28 12:18:54 -05:00
|
|
|
import { useLightning } from './lightning'
|
|
|
|
import { useToast } from './toast'
|
2023-01-26 10:11:55 -06:00
|
|
|
|
2024-07-01 12:02:29 -05:00
|
|
|
export const payBountyCacheMods = {
|
|
|
|
onPaid: (cache, { data }) => {
|
|
|
|
const response = Object.values(data)[0]
|
|
|
|
if (!response?.result) return
|
|
|
|
const { id, path } = response.result
|
2023-12-26 16:51:47 -06:00
|
|
|
const root = path.split('.')[0]
|
|
|
|
cache.modify({
|
|
|
|
id: `Item:${root}`,
|
|
|
|
fields: {
|
|
|
|
bountyPaidTo (existingPaidTo = []) {
|
|
|
|
return [...(existingPaidTo || []), Number(id)]
|
2023-01-26 10:11:55 -06:00
|
|
|
}
|
2023-12-26 16:51:47 -06:00
|
|
|
}
|
|
|
|
})
|
2024-07-01 12:02:29 -05:00
|
|
|
},
|
|
|
|
onPayError: (e, cache, { data }) => {
|
|
|
|
const response = Object.values(data)[0]
|
|
|
|
if (!response?.result) return
|
|
|
|
const { id, path } = response.result
|
|
|
|
const root = path.split('.')[0]
|
|
|
|
cache.modify({
|
|
|
|
id: `Item:${root}`,
|
|
|
|
fields: {
|
|
|
|
bountyPaidTo (existingPaidTo = []) {
|
|
|
|
return (existingPaidTo || []).filter(i => i !== Number(id))
|
|
|
|
}
|
2024-05-28 12:18:54 -05:00
|
|
|
}
|
2024-07-01 12:02:29 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-05-28 12:18:54 -05:00
|
|
|
|
2024-07-01 12:02:29 -05:00
|
|
|
export default function PayBounty ({ children, item }) {
|
2024-09-12 20:05:11 +02:00
|
|
|
const { me } = useMe()
|
2024-07-01 12:02:29 -05:00
|
|
|
const showModal = useShowModal()
|
|
|
|
const root = useRoot()
|
|
|
|
const strike = useLightning()
|
|
|
|
const toaster = useToast()
|
|
|
|
const variables = { id: item.id, sats: root.bounty, act: 'TIP' }
|
|
|
|
const act = useAct({
|
|
|
|
variables,
|
2024-07-01 14:58:53 -05:00
|
|
|
optimisticResponse: { act: { __typename: 'ItemActPaidAction', result: { ...variables, path: item.path } } },
|
2024-07-01 12:02:29 -05:00
|
|
|
...payBountyCacheMods
|
|
|
|
})
|
2024-06-04 03:02:34 -05:00
|
|
|
|
2024-07-01 12:02:29 -05:00
|
|
|
const handlePayBounty = async onCompleted => {
|
|
|
|
try {
|
|
|
|
strike()
|
2024-07-09 13:10:41 -05:00
|
|
|
const { error } = await act({ onCompleted })
|
|
|
|
if (error) throw error
|
2023-01-26 10:11:55 -06:00
|
|
|
} catch (error) {
|
2024-07-09 13:10:41 -05:00
|
|
|
if (error instanceof ActCanceledError) {
|
2023-01-26 10:11:55 -06:00
|
|
|
return
|
|
|
|
}
|
2024-05-28 12:18:54 -05:00
|
|
|
|
|
|
|
const reason = error?.message || error?.toString?.()
|
2024-07-09 13:10:41 -05:00
|
|
|
toaster.danger(reason)
|
2023-01-26 10:11:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 16:51:17 -05:00
|
|
|
if (!me || item.mine || root.user.name !== me.name) {
|
2023-01-26 10:11:55 -06:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ActionTooltip
|
|
|
|
notForm
|
2023-08-08 17:04:06 -04:00
|
|
|
overlayText={numWithUnits(root.bounty)}
|
2023-01-26 10:11:55 -06:00
|
|
|
>
|
2023-07-23 10:08:43 -05:00
|
|
|
<div
|
|
|
|
className={styles.pay} onClick={() => {
|
|
|
|
showModal(onClose => (
|
|
|
|
<>
|
2023-07-24 13:35:05 -05:00
|
|
|
<div className='text-center fw-bold text-muted'>
|
2023-07-23 10:08:43 -05:00
|
|
|
Pay this bounty to {item.user.name}?
|
|
|
|
</div>
|
|
|
|
<div className='text-center'>
|
|
|
|
<Button className='mt-4' variant='primary' onClick={() => handlePayBounty(onClose)}>
|
2023-08-08 17:04:06 -04:00
|
|
|
pay <small>{numWithUnits(root.bounty)}</small>
|
2023-07-23 10:08:43 -05:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
))
|
|
|
|
}}
|
2023-01-26 10:11:55 -06:00
|
|
|
>
|
2023-07-23 10:08:43 -05:00
|
|
|
pay bounty
|
|
|
|
</div>
|
2023-01-26 10:11:55 -06:00
|
|
|
</ActionTooltip>
|
|
|
|
)
|
|
|
|
}
|