Skip to content

Commit 8c5897b

Browse files
committed
Allow switching to a more expensive territory
1 parent c571ba0 commit 8c5897b

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

api/paidAction/itemUpdate.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ export const paymentMethods = [
1212
PAID_ACTION_PAYMENT_METHODS.PESSIMISTIC
1313
]
1414

15-
export async function getCost ({ id, boost = 0, uploadIds, bio }, { me, models }) {
15+
export async function getCost ({ subName, id, boost = 0, uploadIds, bio }, { me, models }) {
1616
// the only reason updating items costs anything is when it has new uploads
17-
// or more boost
17+
// or more boost or is switch to a more expensive sub
1818
const old = await models.item.findUnique({ where: { id: parseInt(id) } })
1919
const { totalFeesMsats } = await uploadFees(uploadIds, { models, me })
20-
const cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
20+
let cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
21+
if (old.subName !== subName) {
22+
const oldSub = await models.sub.findUnique({ where: { name: old.subName } })
23+
const newSub = await models.sub.findUnique({ where: { name: subName } })
24+
const oldCost = oldSub?.baseCost ?? 0
25+
const newCost = newSub?.baseCost ?? 0
26+
cost += satsToMsats(Math.max(0, newCost - oldCost)) // user will pay just the difference
27+
}
2128

2229
if (cost > 0 && old.invoiceActionState && old.invoiceActionState !== 'PAID') {
2330
throw new Error('creation invoice not paid')

api/resolvers/item.js

-8
Original file line numberDiff line numberDiff line change
@@ -1474,14 +1474,6 @@ export const updateItem = async (parent, { sub: subName, forward, hash, hmac, ..
14741474
throw new GqlInputError('item does not belong to you')
14751475
}
14761476

1477-
const differentSub = subName && old.subName !== subName
1478-
if (differentSub) {
1479-
const sub = await models.sub.findUnique({ where: { name: subName } })
1480-
if (sub.baseCost > old.sub.baseCost) {
1481-
throw new GqlInputError('cannot change to a more expensive sub')
1482-
}
1483-
}
1484-
14851477
// in case they lied about their existing boost
14861478
await validateSchema(advSchema, { boost: item.boost }, { models, me, existingBoost: old.boost })
14871479

pages/items/[id]/edit.js

+41-12
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,49 @@ import JobForm from '@/components/job-form'
77
import { PollForm } from '@/components/poll-form'
88
import { BountyForm } from '@/components/bounty-form'
99
import { useState } from 'react'
10-
import { useQuery } from '@apollo/client'
10+
import { useQuery, gql } from '@apollo/client'
1111
import { useRouter } from 'next/router'
1212
import PageLoading from '@/components/page-loading'
13-
import { FeeButtonProvider } from '@/components/fee-button'
13+
import { FeeButtonProvider, postCommentBaseLineItems } from '@/components/fee-button'
1414
import SubSelect from '@/components/sub-select'
1515
import useCanEdit from '@/components/use-can-edit'
16+
import { useMe } from '@/components/me'
1617

1718
export const getServerSideProps = getGetServerSideProps({
1819
query: ITEM,
1920
notFound: data => !data.item
2021
})
2122

23+
// TODO: cleanup
24+
const SUB_QUERY = gql`
25+
query Sub($name: String!) {
26+
sub(name: $name) {
27+
name
28+
baseCost
29+
}
30+
}
31+
`
32+
2233
export default function PostEdit ({ ssrData }) {
2334
const router = useRouter()
2435
const { data } = useQuery(ITEM, { variables: { id: router.query.id } })
2536
if (!data && !ssrData) return <PageLoading />
2637

2738
const { item } = data || ssrData
39+
const { me } = useMe()
2840
const [sub, setSub] = useState(item.subName)
2941

42+
// TODO: cleanup
43+
const { data: oldSubData } = useQuery(SUB_QUERY, {
44+
variables: { name: item.subName },
45+
skip: !item.subName
46+
})
47+
48+
const { data: newSubData } = useQuery(SUB_QUERY, {
49+
variables: { name: sub },
50+
skip: !sub
51+
})
52+
3053
const [,, editThreshold] = useCanEdit(item)
3154

3255
let FormType = DiscussionForm
@@ -45,20 +68,26 @@ export default function PostEdit ({ ssrData }) {
4568
itemType = 'BOUNTY'
4669
}
4770

48-
const existingBoostLineItem = item.boost
49-
? {
50-
existingBoost: {
51-
label: 'old boost',
52-
term: `- ${item.boost}`,
53-
op: '-',
54-
modifier: cost => cost - item.boost
71+
function editLineItems (oldSub, newSub) {
72+
const existingBoostLineItem = item.boost
73+
? {
74+
existingBoost: {
75+
label: 'old boost',
76+
term: `- ${item.boost}`,
77+
op: '-',
78+
modifier: cost => cost - item.boost
79+
}
5580
}
56-
}
57-
: undefined
81+
: undefined
82+
return {
83+
...(item.subName !== newSub?.name ? postCommentBaseLineItems({ baseCost: Math.max(0, newSub?.baseCost - oldSub?.baseCost), me: !!me }) : undefined),
84+
...existingBoostLineItem
85+
}
86+
}
5887

5988
return (
6089
<CenterLayout sub={sub}>
61-
<FeeButtonProvider baseLineItems={existingBoostLineItem}>
90+
<FeeButtonProvider baseLineItems={editLineItems(oldSubData?.sub, newSubData?.sub)}>
6291
<FormType item={item} editThreshold={editThreshold}>
6392
{!item.isJob &&
6493
<SubSelect

0 commit comments

Comments
 (0)