fix: update server actions

This commit is contained in:
andrzej 2024-09-20 15:19:31 +02:00
parent 0bb8eac362
commit c0f55be1b5
8 changed files with 79 additions and 75 deletions

Binary file not shown.

View File

@ -76,7 +76,7 @@ export async function createPub(data: Pub & { genres: number[] }): Promise<Pub |
{ genres: { set: genresArray } } { genres: { set: genresArray } }
}) })
revalidatePath("/publication") revalidatePath("/publication")
return res return genresRes
} catch (error) { } catch (error) {
console.error(error) console.error(error)
return undefined return undefined

View File

@ -6,25 +6,35 @@ import { redirect } from "next/navigation"
export async function updateField({ datum, table, column, id, pathname }: { datum?: string | number | Genre[], table: string, column: string, id: number, pathname: string }) { export async function updateField({ datum, table, column, id, pathname }: { datum?: string | number | Genre[], table: string, column: string, id: number, pathname: string }) {
const res = await prisma[table].update({ try {
where: { id }, const res = await prisma[table].update({
data: { where: { id },
[column]: datum data: {
} [column]: datum
}) }
console.log(`updated record in ${table}: ${JSON.stringify(res)}`) })
revalidatePath(pathname) console.log(`updated record in ${table}: ${JSON.stringify(res)}`)
redirect(pathname) revalidatePath(pathname)
return res
} catch (error) {
console.error(error)
return undefined
}
} }
export async function updateGenres({ genres, table, id, pathname }: { genres: { id: number }[], table: string, id: number, pathname: string }) { export async function updateGenres({ genres, table, id, pathname }: { genres: { id: number }[], table: string, id: number, pathname: string }) {
const res = await prisma[table].update({ try {
where: { id }, const res = await prisma[table].update({
data: { where: { id },
genres: { set: genres } data: {
} genres: { set: genres }
}) }
console.log(`updated record in ${table}: ${JSON.stringify(res)}`) })
revalidatePath(pathname) console.log(`updated record in ${table}: ${JSON.stringify(res)}`)
redirect(pathname) revalidatePath(pathname)
return res
} catch (error) {
console.error(error)
return undefined
}
} }

View File

@ -29,7 +29,7 @@ export default function CreateSubmissionDialog({ stories, pubs, responses, defau
<DialogTitle>New submission</DialogTitle> <DialogTitle>New submission</DialogTitle>
<DialogDescription>Create an entry for a new story i.e. a thing you intend to submit for publication.</DialogDescription> <DialogDescription>Create an entry for a new story i.e. a thing you intend to submit for publication.</DialogDescription>
</DialogHeader> </DialogHeader>
<SubmissionForm createSub={createSub} pubs={pubs} responses={responses} stories={stories} defaults={defaults} /> <SubmissionForm pubs={pubs} responses={responses} stories={stories} defaults={defaults} />
<DialogFooter> <DialogFooter>
<DialogClose asChild> <DialogClose asChild>
</DialogClose> </DialogClose>

View File

@ -2,7 +2,7 @@ import { z } from "zod";
export const storySchema = z.object({ export const storySchema = z.object({
title: z.string().min(2).max(50), title: z.string().min(2).max(50),
word_count: z.number(), word_count: z.coerce.number(),
genres: z.object({ id: z.number(), name: z.string() }).array() genres: z.object({ id: z.number(), name: z.string() }).array()
}) })

View File

@ -26,22 +26,21 @@ export default function GenrePickerInputCell(props: CellContext<any, any>) {
async function onSubmit({ genres }: { genres: number[] }, event: Event) { async function onSubmit({ genres }: { genres: number[] }, event: Event) {
event.preventDefault() event.preventDefault()
const genresArray = genres.map((e) => { return { id: e } }) try {
console.log(`genres: ${genres}, genresArray: ${JSON.stringify(genresArray)}`) const genresArray = genres.map((e) => { return { id: e } })
toast({ const res = await updateGenres({
title: "You submitted the following values:", id,
description: ( table,
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4"> genres: genresArray,
<code className="text-white">{JSON.stringify(genres)}</code> pathname
</pre> })
), if (res === undefined) throw new Error("Something went wrong.")
}) toast({ title: "Field updated successfully." })
const res = await updateGenres({ window.location.reload()
id, } catch (error) {
table, console.error(error)
genres: genresArray, toast({ title: "Something went wrong." })
pathname }
})
setIsActive(false) setIsActive(false)
} }

View File

@ -27,39 +27,34 @@ export default function NumberInputCell(props: CellContext<any, any>) {
}) })
function onSubmit(value: z.infer<typeof formSchema>) { async function onSubmit(value: z.infer<typeof formSchema>) {
toast({ try {
title: "You submitted the following values:", const res = await updateField({
description: ( id,
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4"> table,
<code className="text-white">{JSON.stringify(value, null, 2)}</code> datum: value[column],
</pre> column,
), pathname
}) })
updateField({ if (res === undefined) throw new Error("something went wrong")
id, toast({ title: "Field updated successfully." })
table, } catch (error) {
number: value[column], console.error(error)
column, toast({ title: "Something went wrong." })
pathname }
})
setIsActive(false) setIsActive(false)
} }
function onErrors(errors) { function onErrors(errors: Error) {
toast({ toast({
title: "You have errors", title: "You have errors",
description: ( description: errors.message,
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(errors, null, 2)}</code>
</pre>
),
}) })
console.log(JSON.stringify(errors)) console.log(JSON.stringify(errors))
} }
return ( return (
<div <div
onDoubleClick={() => setIsActive(prev => !prev)} onDoubleClick={() => setIsActive(true)}
className="w-full h-fit flex items-center justify-center" className="w-full h-fit flex items-center justify-center"
tabIndex={0} tabIndex={0}
onKeyDown={e => { onKeyDown={e => {

View File

@ -28,21 +28,21 @@ export function TextInputCell(props: CellContext<any, any>) {
}) })
async function onSubmit(value: z.infer<typeof formSchema>) { async function onSubmit(value: z.infer<typeof formSchema>) {
toast({ try {
title: "You submitted the following values:", const res = await updateField({
description: ( id,
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4"> table,
<code className="text-white">{JSON.stringify(value, null, 2)}</code> datum: value[column],
</pre> column,
), pathname
}) })
const res = await updateField({ if (res === undefined) throw new Error("something went wrong")
id, toast({ title: "Field updated successfully." })
table, window.location.reload()
datum: value[column], } catch (error) {
column, console.error(error)
pathname toast({ title: "Something went wrong." })
}) }
setIsActive(false) setIsActive(false)
} }