fix: update server actions

This commit is contained in:
andrzej 2024-09-20 15:19:31 +02:00
parent 62f0e75abd
commit 2e947b8e78
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 } }
})
revalidatePath("/publication")
return res
return genresRes
} catch (error) {
console.error(error)
return undefined

View File

@ -6,6 +6,7 @@ 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 }) {
try {
const res = await prisma[table].update({
where: { id },
data: {
@ -14,10 +15,15 @@ export async function updateField({ datum, table, column, id, pathname }: { datu
})
console.log(`updated record in ${table}: ${JSON.stringify(res)}`)
revalidatePath(pathname)
redirect(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 }) {
try {
const res = await prisma[table].update({
where: { id },
data: {
@ -26,5 +32,9 @@ export async function updateGenres({ genres, table, id, pathname }: { genres: {
})
console.log(`updated record in ${table}: ${JSON.stringify(res)}`)
revalidatePath(pathname)
redirect(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>
<DialogDescription>Create an entry for a new story i.e. a thing you intend to submit for publication.</DialogDescription>
</DialogHeader>
<SubmissionForm createSub={createSub} pubs={pubs} responses={responses} stories={stories} defaults={defaults} />
<SubmissionForm pubs={pubs} responses={responses} stories={stories} defaults={defaults} />
<DialogFooter>
<DialogClose asChild>
</DialogClose>

View File

@ -2,7 +2,7 @@ import { z } from "zod";
export const storySchema = z.object({
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()
})

View File

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

View File

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

View File

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