refactor link/dicussion to upserts and reuse more code

This commit is contained in:
keyan 2022-04-18 17:10:26 -05:00
parent 2b594109ea
commit 822fa9113a
6 changed files with 57 additions and 159 deletions

View File

@ -445,70 +445,26 @@ export default {
},
Mutation: {
createLink: async (parent, { title, url, boost }, { me, models }) => {
if (!title) {
throw new UserInputError('link must have title', { argumentName: 'title' })
}
upsertLink: async (parent, args, { me, models }) => {
const { id, ...data } = args
data.url = ensureProtocol(data.url)
if (!url) {
throw new UserInputError('link must have url', { argumentName: 'url' })
if (id) {
const { forward, boost, ...remaining } = data
return await updateItem(parent, { id, data: remaining }, { me, models })
} else {
return await createItem(parent, data, { me, models })
}
return await createItem(parent, { title, url: ensureProtocol(url), boost }, { me, models })
},
updateLink: async (parent, { id, title, url }, { me, models }) => {
if (!id) {
throw new UserInputError('link must have id', { argumentName: 'id' })
}
upsertDiscussion: async (parent, args, { me, models }) => {
const { id, ...data } = args
if (!title) {
throw new UserInputError('link must have title', { argumentName: 'title' })
if (id) {
const { forward, boost, ...remaining } = data
return await updateItem(parent, { id, data: remaining }, { me, models })
} else {
return await createItem(parent, data, { me, models })
}
if (!url) {
throw new UserInputError('link must have url', { argumentName: 'url' })
}
// update iff this item belongs to me
const item = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(item.userId) !== Number(me.id)) {
throw new AuthenticationError('item does not belong to you')
}
if (Date.now() > new Date(item.createdAt).getTime() + 10 * 60000) {
throw new UserInputError('item can no longer be editted')
}
return await updateItem(parent, { id, data: { title, url: ensureProtocol(url) } }, { me, models })
},
createDiscussion: async (parent, { title, text, boost }, { me, models }) => {
if (!title) {
throw new UserInputError('discussion must have title', { argumentName: 'title' })
}
return await createItem(parent, { title, text, boost }, { me, models })
},
updateDiscussion: async (parent, { id, title, text }, { me, models }) => {
if (!id) {
throw new UserInputError('discussion must have id', { argumentName: 'id' })
}
if (!title) {
throw new UserInputError('discussion must have title', { argumentName: 'title' })
}
// update iff this item belongs to me
const item = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(item.userId) !== Number(me.id)) {
throw new AuthenticationError('item does not belong to you')
}
// if it's not the FAQ and older than 10 minutes
if (item.id !== 349 && Date.now() > new Date(item.createdAt).getTime() + 10 * 60000) {
throw new UserInputError('item can no longer be editted')
}
return await updateItem(parent, { id, data: { title, text } }, { me, models })
},
upsertJob: async (parent, { id, sub, title, company, location, remote, text, url, maxBid, status }, { me, models }) => {
if (!me) {
@ -559,6 +515,11 @@ export default {
}
}
const old = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(old.userId) !== Number(me?.id)) {
throw new AuthenticationError('item does not belong to you')
}
return await models.item.update({
where: { id: Number(id) },
data
@ -572,35 +533,9 @@ export default {
})
},
createComment: async (parent, { text, parentId }, { me, models }) => {
if (!text) {
throw new UserInputError('comment must have text', { argumentName: 'text' })
}
if (!parentId) {
throw new UserInputError('comment must have parent', { argumentName: 'parentId' })
}
return await createItem(parent, { text, parentId }, { me, models })
},
updateComment: async (parent, { id, text }, { me, models }) => {
if (!text) {
throw new UserInputError('comment must have text', { argumentName: 'text' })
}
if (!id) {
throw new UserInputError('comment must have id', { argumentName: 'id' })
}
// update iff this comment belongs to me
const comment = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(comment.userId) !== Number(me.id)) {
throw new AuthenticationError('comment does not belong to you')
}
if (Date.now() > new Date(comment.createdAt).getTime() + 10 * 60000) {
throw new UserInputError('comment can no longer be editted')
}
return await updateItem(parent, { id, data: { text } }, { me, models })
},
act: async (parent, { id, sats }, { me, models }) => {
@ -828,6 +763,17 @@ export const createMentions = async (item, models) => {
}
const updateItem = async (parent, { id, data }, { me, models }) => {
// update iff this item belongs to me
const old = await models.item.findUnique({ where: { id: Number(id) } })
if (Number(old.userId) !== Number(me?.id)) {
throw new AuthenticationError('item does not belong to you')
}
// if it's not the FAQ and older than 10 minutes
if (old.id !== 349 && Date.now() > new Date(old.createdAt).getTime() + 10 * 60000) {
throw new UserInputError('item can no longer be editted')
}
const item = await models.item.update({
where: { id: Number(id) },
data
@ -838,7 +784,7 @@ const updateItem = async (parent, { id, data }, { me, models }) => {
return item
}
const createItem = async (parent, { title, url, text, boost, parentId }, { me, models }) => {
const createItem = async (parent, { title, url, text, boost, forward, parentId }, { me, models }) => {
if (!me) {
throw new AuthenticationError('you must be logged in')
}

View File

@ -19,10 +19,11 @@ export default gql`
}
extend type Mutation {
upsertStory(title: String!, text: String, url: String, boost: Int): Item!
upsertLink(id: ID, title: String!, url: String!, boost: Int, forward: String): Item!
upsertDiscussion(id: ID, title: String!, text: String, boost: Int, forward: String): Item!
upsertJob(id: ID, sub: ID!, title: String!, company: String!, location: String, remote: Boolean, text: String!, url: String!, maxBid: Int!, status: String): Item!
createComment(text: String!, parentId: ID!): Item!
updateComment(id: ID!, text: String!): Item!
upsertJob(id: ID, sub: ID!, title: String!, company: String!, location: String, remote: Boolean, text: String!, url: String!, maxBid: Int!, status: String): Item!
act(id: ID!, sats: Int): ItemActResult!
}

View File

@ -7,7 +7,7 @@ import { BOOST_MIN } from '../lib/constants'
export const AdvPostSchema = {
boost: Yup.number().typeError('must be a number')
.min(BOOST_MIN, `must be at least ${BOOST_MIN}`).integer('must be whole'),
forward: Yup.string().required('required').trim()
forward: Yup.string().trim()
}
export const AdvPostInitial = {

View File

@ -18,36 +18,14 @@ export function DiscussionForm ({
adv, handleSubmit
}) {
const router = useRouter()
const [createDiscussion] = useMutation(
const [upsertDiscussion] = useMutation(
gql`
mutation createDiscussion($title: String!, $text: String, $boost: Int, $forward: String) {
createDiscussion(title: $title, text: $text, boost: $boost, forward: $forward) {
mutation upsertDiscussion($id: ID, $title: String!, $text: String, $boost: Int, $forward: String) {
upsertDiscussion(id: $id, title: $title, text: $text, boost: $boost, forward: $forward) {
id
}
}`
)
const [updateDiscussion] = useMutation(
gql`
mutation updateDiscussion($id: ID!, $title: String!, $text: String!, $forward: String) {
updateDiscussion(id: $id, title: $title, text: $text, forward: $forward) {
id
}
}`, {
update (cache, { data: { updateDiscussion } }) {
cache.modify({
id: `Item:${item.id}`,
fields: {
title () {
return updateDiscussion.title
},
text () {
return updateDiscussion.text
}
}
})
}
}
)
return (
<Form
@ -58,12 +36,9 @@ export function DiscussionForm ({
}}
schema={DiscussionSchema}
onSubmit={handleSubmit || (async ({ boost, ...values }) => {
let error
if (item) {
({ error } = await updateDiscussion({ variables: { ...values, id: item.id } }))
} else {
({ error } = await createDiscussion({ variables: { boost: Number(boost), ...values } }))
}
const { error } = await upsertDiscussion({
variables: { id: item?.id, boost: Number(boost), ...values }
})
if (error) {
throw new Error({ message: error.toString() })
}

View File

@ -112,11 +112,15 @@ export default function JobForm ({ item, sub }) {
status = 'STOPPED'
}
const variables = { sub: sub.name, maxBid: Number(maxBid), status, ...values }
if (item) {
variables.id = item.id
}
const { error } = await upsertJob({ variables })
const { error } = await upsertJob({
variables: {
id: item?.id,
sub: sub.name,
maxBid: Number(maxBid),
status,
...values
}
})
if (error) {
throw new Error({ message: error.toString() })
}

View File

@ -36,40 +36,15 @@ export function LinkForm ({ item, editThreshold }) {
fetchPolicy: 'network-only'
})
const [createLink] = useMutation(
const [upsertLink] = useMutation(
gql`
mutation createLink($title: String!, $url: String!, $boost: Int, $forward: String) {
createLink(title: $title, url: $url, boost: $boost, forward: $forward) {
mutation upsertLink($id: ID, $title: String!, $url: String!, $boost: Int, $forward: String) {
upsertLink(id: $id, title: $title, url: $url, boost: $boost, forward: $forward) {
id
}
}`
)
const [updateLink] = useMutation(
gql`
mutation updateLink($id: ID!, $title: String!, $url: String!, $forward: String) {
updateLink(id: $id, title: $title, url: $url, forward: $forward) {
id
title
url
}
}`, {
update (cache, { data: { updateLink } }) {
cache.modify({
id: `Item:${item.id}`,
fields: {
title () {
return updateLink.title
},
url () {
return updateLink.url
}
}
})
}
}
)
return (
<Form
initial={{
@ -79,12 +54,9 @@ export function LinkForm ({ item, editThreshold }) {
}}
schema={LinkSchema}
onSubmit={async ({ boost, ...values }) => {
let error
if (item) {
({ error } = await updateLink({ variables: { ...values, id: item.id } }))
} else {
({ error } = await createLink({ variables: { boost: Number(boost), ...values } }))
}
const { error } = await upsertLink({
variables: { id: item?.id, boost: Number(boost), ...values }
})
if (error) {
throw new Error({ message: error.toString() })
}