Allow pay per invoice for bounty and job posts
This commit is contained in:
parent
f2f09b22c4
commit
d186e869e1
@ -8,6 +8,8 @@ import InputGroup from 'react-bootstrap/InputGroup'
|
|||||||
import { bountySchema } from '../lib/validate'
|
import { bountySchema } from '../lib/validate'
|
||||||
import { SubSelectInitial } from './sub-select-form'
|
import { SubSelectInitial } from './sub-select-form'
|
||||||
import CancelButton from './cancel-button'
|
import CancelButton from './cancel-button'
|
||||||
|
import { useCallback } from 'react'
|
||||||
|
import { useAnonymous } from '../lib/anonymous'
|
||||||
|
|
||||||
export function BountyForm ({
|
export function BountyForm ({
|
||||||
item,
|
item,
|
||||||
@ -49,6 +51,33 @@ export function BountyForm ({
|
|||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const submitUpsertBounty = useCallback(
|
||||||
|
// we ignore the invoice since only stackers can post bounties
|
||||||
|
// the invoice is only for funding the wallet
|
||||||
|
async (_, boost, bounty, values, __) => {
|
||||||
|
const { error } = await upsertBounty({
|
||||||
|
variables: {
|
||||||
|
sub: item?.subName || sub?.name,
|
||||||
|
id: item?.id,
|
||||||
|
boost: boost ? Number(boost) : undefined,
|
||||||
|
bounty: bounty ? Number(bounty) : undefined,
|
||||||
|
...values
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (error) {
|
||||||
|
throw new Error({ message: error.toString() })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item) {
|
||||||
|
await router.push(`/items/${item.id}`)
|
||||||
|
} else {
|
||||||
|
const prefix = sub?.name ? `/~${sub.name}` : ''
|
||||||
|
await router.push(prefix + '/recent')
|
||||||
|
}
|
||||||
|
}, [upsertBounty, router])
|
||||||
|
|
||||||
|
const anonUpsertBounty = useAnonymous(submitUpsertBounty, { requireSession: true })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form
|
<Form
|
||||||
initial={{
|
initial={{
|
||||||
@ -61,26 +90,8 @@ export function BountyForm ({
|
|||||||
schema={schema}
|
schema={schema}
|
||||||
onSubmit={
|
onSubmit={
|
||||||
handleSubmit ||
|
handleSubmit ||
|
||||||
(async ({ boost, bounty, ...values }) => {
|
(async ({ boost, bounty, cost, ...values }) => {
|
||||||
const { error } = await upsertBounty({
|
await anonUpsertBounty(cost, boost, bounty, values)
|
||||||
variables: {
|
|
||||||
sub: item?.subName || sub?.name,
|
|
||||||
id: item?.id,
|
|
||||||
boost: boost ? Number(boost) : undefined,
|
|
||||||
bounty: bounty ? Number(bounty) : undefined,
|
|
||||||
...values
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if (error) {
|
|
||||||
throw new Error({ message: error.toString() })
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item) {
|
|
||||||
await router.push(`/items/${item.id}`)
|
|
||||||
} else {
|
|
||||||
const prefix = sub?.name ? `/~${sub.name}` : ''
|
|
||||||
await router.push(prefix + '/recent')
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
storageKeyPrefix={item ? undefined : 'bounty'}
|
storageKeyPrefix={item ? undefined : 'bounty'}
|
||||||
|
@ -5,7 +5,7 @@ import InputGroup from 'react-bootstrap/InputGroup'
|
|||||||
import Image from 'react-bootstrap/Image'
|
import Image from 'react-bootstrap/Image'
|
||||||
import BootstrapForm from 'react-bootstrap/Form'
|
import BootstrapForm from 'react-bootstrap/Form'
|
||||||
import Alert from 'react-bootstrap/Alert'
|
import Alert from 'react-bootstrap/Alert'
|
||||||
import { useEffect, useState } from 'react'
|
import { useCallback, useEffect, useState } from 'react'
|
||||||
import Info from './info'
|
import Info from './info'
|
||||||
import AccordianItem from './accordian-item'
|
import AccordianItem from './accordian-item'
|
||||||
import styles from '../styles/post.module.css'
|
import styles from '../styles/post.module.css'
|
||||||
@ -17,6 +17,7 @@ import Avatar from './avatar'
|
|||||||
import ActionTooltip from './action-tooltip'
|
import ActionTooltip from './action-tooltip'
|
||||||
import { jobSchema } from '../lib/validate'
|
import { jobSchema } from '../lib/validate'
|
||||||
import CancelButton from './cancel-button'
|
import CancelButton from './cancel-button'
|
||||||
|
import { useAnonymous } from '../lib/anonymous'
|
||||||
|
|
||||||
function satsMin2Mo (minute) {
|
function satsMin2Mo (minute) {
|
||||||
return minute * 30 * 24 * 60
|
return minute * 30 * 24 * 60
|
||||||
@ -50,6 +51,40 @@ export default function JobForm ({ item, sub }) {
|
|||||||
}`
|
}`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const submitUpsertJob = useCallback(
|
||||||
|
// we ignore the invoice since only stackers can post bounties
|
||||||
|
// the invoice is only for funding the wallet
|
||||||
|
async (_, maxBid, stop, start, values, __) => {
|
||||||
|
let status
|
||||||
|
if (start) {
|
||||||
|
status = 'ACTIVE'
|
||||||
|
} else if (stop) {
|
||||||
|
status = 'STOPPED'
|
||||||
|
}
|
||||||
|
|
||||||
|
const { error } = await upsertJob({
|
||||||
|
variables: {
|
||||||
|
id: item?.id,
|
||||||
|
sub: item?.subName || sub?.name,
|
||||||
|
maxBid: Number(maxBid),
|
||||||
|
status,
|
||||||
|
logo: Number(logoId),
|
||||||
|
...values
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (error) {
|
||||||
|
throw new Error({ message: error.toString() })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item) {
|
||||||
|
await router.push(`/items/${item.id}`)
|
||||||
|
} else {
|
||||||
|
await router.push(`/~${sub.name}/recent`)
|
||||||
|
}
|
||||||
|
}, [upsertJob, router])
|
||||||
|
|
||||||
|
const anonUpsertJob = useAnonymous(submitUpsertJob, { requireSession: true })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form
|
<Form
|
||||||
@ -68,32 +103,7 @@ export default function JobForm ({ item, sub }) {
|
|||||||
schema={jobSchema}
|
schema={jobSchema}
|
||||||
storageKeyPrefix={storageKeyPrefix}
|
storageKeyPrefix={storageKeyPrefix}
|
||||||
onSubmit={(async ({ maxBid, stop, start, ...values }) => {
|
onSubmit={(async ({ maxBid, stop, start, ...values }) => {
|
||||||
let status
|
await anonUpsertJob(1000, maxBid, stop, start, values)
|
||||||
if (start) {
|
|
||||||
status = 'ACTIVE'
|
|
||||||
} else if (stop) {
|
|
||||||
status = 'STOPPED'
|
|
||||||
}
|
|
||||||
|
|
||||||
const { error } = await upsertJob({
|
|
||||||
variables: {
|
|
||||||
id: item?.id,
|
|
||||||
sub: item?.subName || sub?.name,
|
|
||||||
maxBid: Number(maxBid),
|
|
||||||
status,
|
|
||||||
logo: Number(logoId),
|
|
||||||
...values
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if (error) {
|
|
||||||
throw new Error({ message: error.toString() })
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item) {
|
|
||||||
await router.push(`/items/${item.id}`)
|
|
||||||
} else {
|
|
||||||
await router.push(`/~${sub.name}/recent`)
|
|
||||||
}
|
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<div className='form-group'>
|
<div className='form-group'>
|
||||||
|
@ -100,7 +100,8 @@ export const isInsufficientFundsError = (error) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
forceInvoice: false
|
forceInvoice: false,
|
||||||
|
requireSession: false
|
||||||
}
|
}
|
||||||
export const useAnonymous = (fn, options = defaultOptions) => {
|
export const useAnonymous = (fn, options = defaultOptions) => {
|
||||||
const me = useMe()
|
const me = useMe()
|
||||||
@ -164,6 +165,9 @@ export const useAnonymous = (fn, options = defaultOptions) => {
|
|||||||
}, [invoice?.id])
|
}, [invoice?.id])
|
||||||
|
|
||||||
const anonFn = useCallback(async (amount, ...args) => {
|
const anonFn = useCallback(async (amount, ...args) => {
|
||||||
|
if (!me && options.requireSession) {
|
||||||
|
throw new Error('you must be logged in')
|
||||||
|
}
|
||||||
if (me && !options.forceInvoice) {
|
if (me && !options.forceInvoice) {
|
||||||
try {
|
try {
|
||||||
return await fn(amount, ...args)
|
return await fn(amount, ...args)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user