stablize act mutation reference

This commit is contained in:
keyan 2023-12-26 15:55:48 -06:00
parent 8c5c29ee45
commit 67a9fe23cf
3 changed files with 51 additions and 50 deletions

View File

@ -752,13 +752,14 @@ export default {
await ssValidate(amountSchema, { amount: sats })
await assertGofacYourself({ models, headers })
// disallow self tips except anons
if (me) {
const [item] = await models.$queryRawUnsafe(`
const [item] = await models.$queryRawUnsafe(`
${SELECT}
FROM "Item"
WHERE id = $1 AND "userId" = $2`, Number(id), me.id)
if (item) {
WHERE id = $1`, Number(id))
// disallow self tips except anons
if (me) {
if (Number(item.userId) === Number(me.id)) {
throw new GraphQLError('cannot zap your self', { extensions: { code: 'BAD_INPUT' } })
}
@ -769,7 +770,7 @@ export default {
}
}
const { item_act: vote } = await serializeInvoicable(
await serializeInvoicable(
models.$queryRaw`
SELECT
item_act(${Number(id)}::INTEGER,
@ -780,8 +781,9 @@ export default {
notifyZapped({ models, id })
return {
vote,
sats
id,
sats,
path: item.path
}
},
dontLikeThis: async (parent, { id, sats = DONT_LIKE_THIS_COST, hash, hmac }, { me, lnd, models }) => {

View File

@ -18,8 +18,9 @@ export default gql`
}
type ItemActResult {
vote: Int!
id: ID!
sats: Int!
path: String!
}
extend type Mutation {

View File

@ -56,52 +56,47 @@ const TipPopover = ({ target, show, handleClose }) => (
</Overlay>
)
function useAct ({ item, setVoteShow = () => {}, setTipShow = () => {} }) {
function useAct () {
const me = useMe()
const update = useCallback((cache, { data: { act: { id, sats, path } } }) => {
cache.modify({
id: `Item:${id}`,
fields: {
sats (existingSats = 0) {
return existingSats + sats
},
meSats: me
? (existingSats = 0) => {
return existingSats + sats
}
: undefined
}
})
// update all ancestors
path.split('.').forEach(aId => {
if (Number(aId) === Number(id)) return
cache.modify({
id: `Item:${aId}`,
fields: {
commentSats (existingCommentSats = 0) {
return existingCommentSats + sats
}
}
})
})
}, [!!me])
return useMutation(
gql`
mutation act($id: ID!, $sats: Int!, $hash: String, $hmac: String) {
act(id: $id, sats: $sats, hash: $hash, hmac: $hmac) {
id
sats
path
}
}`, {
update (cache, { data: { act: { sats } } }) {
cache.modify({
id: `Item:${item.id}`,
fields: {
sats (existingSats = 0) {
return existingSats + sats
},
meSats: me
? (existingSats = 0) => {
if (sats <= me.privates?.sats) {
if (existingSats === 0) {
setVoteShow(true)
} else {
setTipShow(true)
}
}
return existingSats + sats
}
: undefined
}
})
// update all ancestors
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 }
)
}
@ -177,11 +172,14 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
const zap = useDebounceCallback(async (sats) => {
if (!sats) return
const variables = { id: item.id, sats }
act({
variables,
optimisticResponse: {
act: {
sats
id: item.id,
sats,
path: item.path
}
}
}).catch((error) => {
@ -193,7 +191,7 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
toaster.danger(error?.message || error?.toString?.())
})
setPendingSats(0)
}, 500, [act, toaster, item?.id, showInvoiceModal, setPendingSats])
}, 500, [act, toaster, item?.id, item?.path, showInvoiceModal, setPendingSats])
const disabled = useMemo(() => item?.mine || item?.meForward || item?.deletedAt,
[item?.mine, item?.meForward, item?.deletedAt])