From 65a7ef10d0177beb9a9e8b61a94b5d9cb6f8c0dc Mon Sep 17 00:00:00 2001 From: k00b Date: Wed, 2 Oct 2024 18:14:25 -0500 Subject: [PATCH] make bio work as paid action --- api/paidAction/itemCreate.js | 4 ++-- api/paidAction/itemUpdate.js | 2 +- api/resolvers/user.js | 10 ++++----- api/typeDefs/user.js | 2 +- components/item.js | 2 +- fragments/paidAction.js | 10 +++++++++ lib/validate.js | 2 +- pages/[name]/index.js | 43 +++++++++++++++--------------------- 8 files changed, 38 insertions(+), 37 deletions(-) diff --git a/api/paidAction/itemCreate.js b/api/paidAction/itemCreate.js index 5b992b75..edc536ec 100644 --- a/api/paidAction/itemCreate.js +++ b/api/paidAction/itemCreate.js @@ -23,8 +23,8 @@ export async function getCost ({ subName, parentId, uploadIds, boost = 0, bio }, // sub allows freebies (or is a bio or a comment), cost is less than baseCost, not anon, // cost must be greater than user's balance, and user has not disabled freebies - const freebie = bio || (parentId && cost <= baseCost && !!me && - cost > me?.msats && !me?.disableFreebies) + const freebie = (parentId || bio) && cost <= baseCost && !!me && + cost > me?.msats && !me?.disableFreebies return freebie ? BigInt(0) : BigInt(cost) } diff --git a/api/paidAction/itemUpdate.js b/api/paidAction/itemUpdate.js index d9b39fb0..172e46c4 100644 --- a/api/paidAction/itemUpdate.js +++ b/api/paidAction/itemUpdate.js @@ -19,7 +19,7 @@ export async function getCost ({ id, boost = 0, uploadIds, bio }, { me, models } throw new Error('creation invoice not paid') } - return bio ? BigInt(0) : cost + return cost } export async function perform (args, context) { diff --git a/api/resolvers/user.js b/api/resolvers/user.js index 0950933a..431e6257 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -691,22 +691,20 @@ export default { return Number(photoId) }, - upsertBio: async (parent, { bio }, { me, models, lnd }) => { + upsertBio: async (parent, { text }, { me, models, lnd }) => { if (!me) { throw new GqlAuthenticationError() } - await ssValidate(bioSchema, { bio }) + await ssValidate(bioSchema, { text }) const user = await models.user.findUnique({ where: { id: me.id } }) if (user.bioId) { - await updateItem(parent, { id: user.bioId, text: bio, title: `@${user.name}'s bio` }, { me, models, lnd }) + return await updateItem(parent, { id: user.bioId, bio: true, text, title: `@${user.name}'s bio` }, { me, models, lnd }) } else { - await createItem(parent, { bio: true, text: bio, title: `@${user.name}'s bio` }, { me, models, lnd }) + return await createItem(parent, { bio: true, text, title: `@${user.name}'s bio` }, { me, models, lnd }) } - - return await models.user.findUnique({ where: { id: me.id } }) }, generateApiKey: async (parent, { id }, { models, me }) => { if (!me) { diff --git a/api/typeDefs/user.js b/api/typeDefs/user.js index f584ca7c..d8193536 100644 --- a/api/typeDefs/user.js +++ b/api/typeDefs/user.js @@ -33,7 +33,7 @@ export default gql` setName(name: String!): String setSettings(settings: SettingsInput!): User setPhoto(photoId: ID!): Int! - upsertBio(bio: String!): User! + upsertBio(text: String!): ItemPaidAction! setWalkthrough(tipPopover: Boolean, upvotePopover: Boolean): Boolean unlinkAuth(authType: String!): AuthMethods! linkUnverifiedEmail(email: String!): Boolean diff --git a/components/item.js b/components/item.js index 331870ce..b7f9029b 100644 --- a/components/item.js +++ b/components/item.js @@ -107,7 +107,7 @@ export default function Item ({ {item.position && (pinnable || !item.subName) ? : item.mine || item.meForward - ? + ? : item.meDontLikeSats > item.meSats ? : Number(item.user?.id) === USER_ID.ad diff --git a/fragments/paidAction.js b/fragments/paidAction.js index ed8cbb91..4deae625 100644 --- a/fragments/paidAction.js +++ b/fragments/paidAction.js @@ -205,6 +205,16 @@ export const POLL_VOTE = gql` } }` +export const UPSERT_BIO = gql` + ${ITEM_PAID_ACTION_FIELDS} + ${PAID_ACTION} + mutation upsertBio($text: String!) { + upsertBio(text: $text) { + ...ItemPaidActionFields + ...PaidActionFields + } + }` + export const CREATE_COMMENT = gql` ${ITEM_PAID_ACTION_FIELDS} ${PAID_ACTION} diff --git a/lib/validate.js b/lib/validate.js index 584226b7..ea24f0b3 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -800,7 +800,7 @@ export const phoenixdSchema = object().shape({ }, ['primaryPassword', 'secondaryPassword']) export const bioSchema = object({ - bio: string().required('required').trim() + text: string().required('required').trim() }) export const inviteSchema = object({ diff --git a/pages/[name]/index.js b/pages/[name]/index.js index 6f1b405b..9e3c7355 100644 --- a/pages/[name]/index.js +++ b/pages/[name]/index.js @@ -1,5 +1,5 @@ import Layout from '@/components/layout' -import { gql, useMutation, useQuery } from '@apollo/client' +import { useQuery } from '@apollo/client' import UserHeader from '@/components/user-header' import Button from 'react-bootstrap/Button' import styles from '@/styles/user.module.css' @@ -8,13 +8,14 @@ import ItemFull from '@/components/item-full' import { Form, MarkdownInput } from '@/components/form' import { useMe } from '@/components/me' import { USER_FULL } from '@/fragments/users' -import { ITEM_FIELDS } from '@/fragments/items' import { getGetServerSideProps } from '@/api/ssrApollo' import { FeeButtonProvider } from '@/components/fee-button' import { bioSchema } from '@/lib/validate' import { useRouter } from 'next/router' import PageLoading from '@/components/page-loading' import { ItemButtonBar } from '@/components/post' +import useItemSubmit from '@/components/use-item-submit' +import { UPSERT_BIO } from '@/fragments/paidAction' export const getServerSideProps = getGetServerSideProps({ query: USER_FULL, @@ -22,49 +23,41 @@ export const getServerSideProps = getGetServerSideProps({ }) export function BioForm ({ handleDone, bio, me }) { - const [upsertBio] = useMutation( - gql` - ${ITEM_FIELDS} - mutation upsertBio($bio: String!) { - upsertBio(bio: $bio) { - id - bio { - ...ItemFields - text - } - } - }`, { - update (cache, { data: { upsertBio } }) { + const onSubmit = useItemSubmit(UPSERT_BIO, { + navigateOnSubmit: false, + paidMutationOptions: { + update (cache, { data: { upsertBio: { result, invoice } } }) { + if (!result) return + cache.modify({ - id: `User:${upsertBio.id}`, + id: `User:${me.id}`, fields: { bio () { - return upsertBio.bio + return result.text } } }) } + }, + onSuccessfulSubmit: (data, { resetForm }) => { + handleDone?.() } - ) + }) return (
{ - const { error } = await upsertBio({ variables: values }) - if (error) throw error - handleDone?.() - }} + onSubmit={onSubmit} storageKeyPrefix={`bio-${me.id}`} >