Skip to content

Commit d148b80

Browse files
committed
cleanup; better query; better readability
1 parent 8c5897b commit d148b80

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

api/paidAction/itemUpdate.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ export const paymentMethods = [
1414

1515
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 or is switch to a more expensive sub
17+
// or more boost or is switching to a more expensive sub
1818
const old = await models.item.findUnique({ where: { id: parseInt(id) } })
1919
const { totalFeesMsats } = await uploadFees(uploadIds, { models, me })
2020
let cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
21+
2122
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
23+
const oldSub = await models.sub.findUnique({ where: { name: old.subName }, select: { baseCost: true } })
24+
const newSub = await models.sub.findUnique({ where: { name: subName }, select: { baseCost: true } })
25+
// user will pay just the difference
26+
cost += satsToMsats(Math.max(0, (newSub?.baseCost ?? 0) - (oldSub?.baseCost ?? 0)))
2727
}
2828

2929
if (cost > 0 && old.invoiceActionState && old.invoiceActionState !== 'PAID') {

fragments/items.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const ITEM_FIELDS = gql`
3535
meMuteSub
3636
meSubscription
3737
nsfw
38+
baseCost
3839
replyCost
3940
}
4041
otsHash

pages/items/[id]/edit.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export const getServerSideProps = getGetServerSideProps({
2020
notFound: data => !data.item
2121
})
2222

23-
// TODO: cleanup
24-
const SUB_QUERY = gql`
23+
const SUB_BASECOST = gql`
2524
query Sub($name: String!) {
2625
sub(name: $name) {
2726
name
@@ -39,16 +38,8 @@ export default function PostEdit ({ ssrData }) {
3938
const { me } = useMe()
4039
const [sub, setSub] = useState(item.subName)
4140

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-
})
41+
// we need to fetch the new sub to calculate the cost difference
42+
const { data: newSubData } = useQuery(SUB_BASECOST, { variables: { name: sub } })
5243

5344
const [,, editThreshold] = useCanEdit(item)
5445

@@ -68,7 +59,7 @@ export default function PostEdit ({ ssrData }) {
6859
itemType = 'BOUNTY'
6960
}
7061

71-
function editLineItems (oldSub, newSub) {
62+
function editLineItems (newSub) {
7263
const existingBoostLineItem = item.boost
7364
? {
7465
existingBoost: {
@@ -79,15 +70,24 @@ export default function PostEdit ({ ssrData }) {
7970
}
8071
}
8172
: undefined
73+
74+
const isSwitchingSub = item.subName !== newSub?.name
75+
const subCostDifference = isSwitchingSub && {
76+
...postCommentBaseLineItems({
77+
baseCost: Math.max(0, (newSub?.baseCost ?? 0) - (item?.sub?.baseCost ?? 0)),
78+
me: !!me
79+
})
80+
}
81+
8282
return {
83-
...(item.subName !== newSub?.name ? postCommentBaseLineItems({ baseCost: Math.max(0, newSub?.baseCost - oldSub?.baseCost), me: !!me }) : undefined),
83+
...subCostDifference,
8484
...existingBoostLineItem
8585
}
8686
}
8787

8888
return (
8989
<CenterLayout sub={sub}>
90-
<FeeButtonProvider baseLineItems={editLineItems(oldSubData?.sub, newSubData?.sub)}>
90+
<FeeButtonProvider baseLineItems={editLineItems(newSubData?.sub)}>
9191
<FormType item={item} editThreshold={editThreshold}>
9292
{!item.isJob &&
9393
<SubSelect

0 commit comments

Comments
 (0)