From 5d49ecc53695623062f9da9c8177d472b0de82a4 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 20 Jan 2022 17:04:12 -0600 Subject: [PATCH] refine tipping experience, removing notion of upvote from UX --- api/resolvers/item.js | 94 +++++++------------ api/typeDefs/item.js | 18 +--- components/comment.js | 2 +- components/item-act.js | 6 +- components/item.js | 2 +- components/seo.js | 2 +- components/upvote.js | 85 +++++------------ components/upvote.module.css | 2 +- fragments/comments.js | 4 +- fragments/items.js | 4 +- .../migration.sql | 4 +- 11 files changed, 70 insertions(+), 153 deletions(-) diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 96d36a0f..955777f9 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -362,7 +362,7 @@ export default { return await updateItem(parent, { id, data: { text } }, { me, models }) }, - act: async (parent, { id, act, sats, tipDefault }, { me, models }) => { + act: async (parent, { id, sats }, { me, models }) => { // need to make sure we are logged in if (!me) { throw new AuthenticationError('you must be logged in') @@ -372,26 +372,20 @@ export default { throw new UserInputError('sats must be positive', { argumentName: 'sats' }) } - // if we are tipping disallow self tips - if (act === 'TIP') { - const [item] = await models.$queryRaw(` - ${SELECT} - FROM "Item" - WHERE id = $1 AND "userId" = $2`, Number(id), me.id) - if (item) { - throw new UserInputError('cannot tip your self') - } - // if tipDefault, set on user - if (tipDefault) { - await models.user.update({ where: { id: me.id }, data: { tipDefault: sats } }) - } + // disallow self tips + const [item] = await models.$queryRaw(` + ${SELECT} + FROM "Item" + WHERE id = $1 AND "userId" = $2`, Number(id), me.id) + if (item) { + throw new UserInputError('cannot tip your self') } - await serialize(models, models.$queryRaw`SELECT item_act(${Number(id)}, ${me.id}, ${act}, ${Number(sats)})`) + const [{ item_act: vote }] = await serialize(models, models.$queryRaw`SELECT item_act(${Number(id)}, ${me.id}, 'TIP', ${Number(sats)})`) return { - sats, - act + vote, + sats } } }, @@ -448,6 +442,27 @@ export default { }, where: { itemId: item.id, + userId: { + not: item.userId + }, + act: { + not: 'BOOST' + } + } + }) + + return sats || 0 + }, + upvotes: async (item, args, { models }) => { + const { sum: { sats } } = await models.itemAct.aggregate({ + sum: { + sats: true + }, + where: { + itemId: item.id, + userId: { + not: item.userId + }, act: 'VOTE' } }) @@ -467,35 +482,6 @@ export default { return sats || 0 }, - tips: async (item, args, { models }) => { - const { sum: { sats } } = await models.itemAct.aggregate({ - sum: { - sats: true - }, - where: { - itemId: item.id, - act: 'TIP' - } - }) - - return sats || 0 - }, - meVote: async (item, args, { me, models }) => { - if (!me) return 0 - - const { sum: { sats } } = await models.itemAct.aggregate({ - sum: { - sats: true - }, - where: { - itemId: item.id, - userId: me.id, - act: 'VOTE' - } - }) - - return sats || 0 - }, meSats: async (item, args, { me, models }) => { if (!me) return 0 @@ -522,22 +508,6 @@ export default { mine: async (item, args, { me, models }) => { return me?.id === item.userId }, - meTip: async (item, args, { me, models }) => { - if (!me) return 0 - - const { sum: { sats } } = await models.itemAct.aggregate({ - sum: { - sats: true - }, - where: { - itemId: item.id, - userId: me.id, - act: 'TIP' - } - }) - - return sats || 0 - }, root: async (item, args, { models }) => { if (!item.parentId) { return null diff --git a/api/typeDefs/item.js b/api/typeDefs/item.js index e176f517..471799b5 100644 --- a/api/typeDefs/item.js +++ b/api/typeDefs/item.js @@ -10,15 +10,9 @@ export default gql` dupes(url: String!): [Item!] } - enum ItemAct { - VOTE - BOOST - TIP - } - type ItemActResult { + vote: Int! sats: Int! - act: ItemAct! } extend type Mutation { @@ -28,7 +22,7 @@ export default gql` updateDiscussion(id: ID!, title: String!, text: String): Item! createComment(text: String!, parentId: ID!): Item! updateComment(id: ID!, text: String!): Item! - act(id: ID!, act: ItemAct!, sats: Int, tipDefault: Boolean): ItemActResult! + act(id: ID!, sats: Int): ItemActResult! } type Items { @@ -53,13 +47,11 @@ export default gql` root: Item user: User! depth: Int! - sats: Int! - boost: Int! - tips: Int! mine: Boolean! - meVote: Int! + boost: Int! + sats: Int! + upvotes: Int! meSats: Int! - meTip: Int! ncomments: Int! comments: [Item!]! path: String diff --git a/components/comment.js b/components/comment.js index 8d4ac013..17af2616 100644 --- a/components/comment.js +++ b/components/comment.js @@ -79,7 +79,7 @@ export default function Comment ({
- 0 ? ` (${item.meSats} from me)` : ''}`}>{item.sats + item.tips} sats + {item.sats} sats \ {item.boost > 0 && <> diff --git a/components/item-act.js b/components/item-act.js index 4e7f71c8..f0f82489 100644 --- a/components/item-act.js +++ b/components/item-act.js @@ -57,13 +57,11 @@ export function ItemActModal () { default: false }} schema={ActSchema} - onSubmit={async ({ amount, tipDefault, submit }) => { + onSubmit={async ({ amount }) => { await item.act({ variables: { id: item.itemId, - act: submit, - sats: Number(amount), - tipDefault + sats: Number(amount) } }) await item.strike() diff --git a/components/item.js b/components/item.js index 9b034518..1af04e32 100644 --- a/components/item.js +++ b/components/item.js @@ -50,7 +50,7 @@ export default function Item ({ item, rank, children }) {
{!item.position && <> - 0 ? ` (${item.meSats} from me)` : ''}`}>{item.sats + item.tips} sats + {item.sats} sats \ } {item.boost > 0 && diff --git a/components/seo.js b/components/seo.js index dacb6ce8..0d4864e8 100644 --- a/components/seo.js +++ b/components/seo.js @@ -20,7 +20,7 @@ export default function Seo ({ item, user }) { desc = desc.replace(/\s+/g, ' ') } } else { - desc = `@${item.user.name} stacked ${(item.sats > 0 ? item.sats - 1 : 0) + item.tips} sats ${item.url ? `posting ${item.url}` : 'with this discussion'}` + desc = `@${item.user.name} stacked ${item.sats} sats ${item.url ? `posting ${item.url}` : 'with this discussion'}` } if (item.ncomments) { desc += ` [${item.ncomments} comments` diff --git a/components/upvote.js b/components/upvote.js index ea433421..b32fd4a0 100644 --- a/components/upvote.js +++ b/components/upvote.js @@ -104,60 +104,29 @@ export default function UpVote ({ item, className }) { const [act] = useMutation( gql` - mutation act($id: ID!, $act: ItemAct! $sats: Int!, $tipDefault: Boolean) { - act(id: $id, act: $act, sats: $sats, tipDefault: $tipDefault) { - act, + mutation act($id: ID!, $sats: Int!) { + act(id: $id, sats: $sats) { + vote, sats } }`, { - update (cache, { data: { act: { act, sats } } }) { - // read in the cached object so we don't use meSats prop - // which can be stale - if (act === 'VOTE') { - setVoteShow(true) - } - if (act === 'TIP') { - setTipShow(true) - } - + update (cache, { data: { act: { vote, sats } } }) { cache.modify({ id: `Item:${item.id}`, fields: { - meVote (existingMeVote = 0) { - if (act === 'VOTE') { - return existingMeVote + sats - } - return existingMeVote - }, - meTip (existingMeTip = 0) { - if (act === 'TIP') { - return existingMeTip + sats - } - return existingMeTip - }, sats (existingSats = 0) { - if (act === 'VOTE') { - return existingSats + sats - } - return existingSats + return existingSats + sats }, meSats (existingSats = 0) { - if (act === 'VOTE' || act === 'TIP') { - return existingSats + sats + if (existingSats === 0) { + setVoteShow(true) + } else { + setTipShow(true) } - return existingSats + return existingSats + sats }, - boost (existingBoost = 0) { - if (act === 'BOOST') { - return existingBoost + sats - } - return existingBoost - }, - tips (existingTips = 0) { - if (act === 'TIP') { - return existingTips + sats - } - return existingTips + upvotes (existingUpvotes = 0) { + return existingUpvotes + vote } } }) @@ -166,15 +135,12 @@ export default function UpVote ({ item, className }) { ) const overlayText = () => { - if (item?.meVote) { - if (me?.tipDefault) { - return `${me.tipDefault} sat${me.tipDefault > 1 ? 's' : ''}` - } - return '1 sat' + if (me?.tipDefault) { + return `${me.tipDefault} sat${me.tipDefault > 1 ? 's' : ''}` } + return '1 sat' } - const noSelfTips = item?.meVote && item?.mine const color = getColor(item?.meSats) return ( @@ -186,7 +152,7 @@ export default function UpVote ({ item, className }) { if (!item || voteLock) return // we can't tip ourselves - if (noSelfTips) { + if (item?.mine) { return } @@ -200,26 +166,19 @@ export default function UpVote ({ item, className }) { if (!item || voteLock) return // we can't tip ourselves - if (noSelfTips) { + if (item?.mine) { return } - if (item?.meVote) { + if (item?.meSats) { setVoteShow(false) - try { - strike() - await act({ variables: { id: item.id, act: 'TIP', sats: me.tipDefault || 1 } }) - } catch (e) { - console.log(e) - } - return } strike() try { setVoteLock(true) - await act({ variables: { id: item.id, act: 'VOTE', sats: 1 } }) + await act({ variables: { id: item.id, sats: me.tipDefault || 1 } }) } catch (error) { if (error.toString().includes('insufficient funds')) { setError(true) @@ -233,9 +192,9 @@ export default function UpVote ({ item, className }) { : signIn } > - +