fix: update server actions
This commit is contained in:
parent
0bb8eac362
commit
c0f55be1b5
BIN
prisma/dev.db
BIN
prisma/dev.db
Binary file not shown.
|
@ -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
|
||||||
|
|
|
@ -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 }) {
|
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({
|
const res = await prisma[table].update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
|
@ -14,10 +15,15 @@ export async function updateField({ datum, table, column, id, pathname }: { datu
|
||||||
})
|
})
|
||||||
console.log(`updated record in ${table}: ${JSON.stringify(res)}`)
|
console.log(`updated record in ${table}: ${JSON.stringify(res)}`)
|
||||||
revalidatePath(pathname)
|
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 }) {
|
export async function updateGenres({ genres, table, id, pathname }: { genres: { id: number }[], table: string, id: number, pathname: string }) {
|
||||||
|
try {
|
||||||
const res = await prisma[table].update({
|
const res = await prisma[table].update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
|
@ -26,5 +32,9 @@ export async function updateGenres({ genres, table, id, pathname }: { genres: {
|
||||||
})
|
})
|
||||||
console.log(`updated record in ${table}: ${JSON.stringify(res)}`)
|
console.log(`updated record in ${table}: ${JSON.stringify(res)}`)
|
||||||
revalidatePath(pathname)
|
revalidatePath(pathname)
|
||||||
redirect(pathname)
|
return res
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
try {
|
||||||
const genresArray = genres.map((e) => { return { id: e } })
|
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({
|
const res = await updateGenres({
|
||||||
id,
|
id,
|
||||||
table,
|
table,
|
||||||
genres: genresArray,
|
genres: genresArray,
|
||||||
pathname
|
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)
|
setIsActive(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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: (
|
|
||||||
<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({
|
|
||||||
id,
|
id,
|
||||||
table,
|
table,
|
||||||
number: value[column],
|
datum: value[column],
|
||||||
column,
|
column,
|
||||||
pathname
|
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)
|
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 => {
|
||||||
|
|
|
@ -28,14 +28,7 @@ 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:",
|
|
||||||
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>
|
|
||||||
),
|
|
||||||
})
|
|
||||||
const res = await updateField({
|
const res = await updateField({
|
||||||
id,
|
id,
|
||||||
table,
|
table,
|
||||||
|
@ -43,6 +36,13 @@ export function TextInputCell(props: CellContext<any, any>) {
|
||||||
column,
|
column,
|
||||||
pathname
|
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)
|
setIsActive(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue