From c0f55be1b58cdb733a1c8c8e6814c4c177cd0dc9 Mon Sep 17 00:00:00 2001 From: andrzej Date: Fri, 20 Sep 2024 15:19:31 +0200 Subject: [PATCH] fix: update server actions --- prisma/dev.db | Bin 69632 -> 69632 bytes src/app/lib/create.ts | 2 +- src/app/lib/update.ts | 46 +++++++++++------- src/app/submission/create.tsx | 2 +- src/app/ui/forms/schemas.ts | 2 +- src/app/ui/tables/inputs/genrePickerInput.tsx | 31 ++++++------ src/app/ui/tables/inputs/numberInput.tsx | 41 +++++++--------- src/app/ui/tables/inputs/textInput.tsx | 30 ++++++------ 8 files changed, 79 insertions(+), 75 deletions(-) diff --git a/prisma/dev.db b/prisma/dev.db index c1f3f25d4e2dd31fcc0bb07069b4f07b8e7a0eef..8207543e5a866f41705483f6c27a23dc0aa7ee18 100644 GIT binary patch delta 1084 zcmYLFU1%It6ux)hC{`^rlw}*z1jHBHv3tpmw3(RM zwO~LKB8Wvy;|13jO$D2ZEu>|reW+3}sQshX7EDD$s)87c7PUSo_;Y5qAzV23eCNCO zeBU|aN0jj+%HegiGTpqLR!;3L_0f#D(3hcg;#OZ%qhZc#aKT)F=b+d82R?>wn1!Fs z`LM3@m|1^Mok}*QXu%&2%6!xr^bT-aEIgf;2oW3h6mngfN_Mu;UHqUkQ1(YX9ykSe z$bG>Z=FV>Zycc+7UMvQsXT3_X6ja*x`3H7{NB6jnG1~}V!22+1&W7pnH_h%AOCiz# zIbRrcdP{y#c6xnmR`#8N5qBSV9v^bO;kMef$;PJzwO)Mv`ir>|F{LJXIVift!1al= zlMu2_+9n+uEOFPU8-h%hwd$S=*y0vkhU;Pa<_Rdmb&^KQ2HrYaO2q3h zSZxhjg2Y(UYnUl9WHS0Ssh+w^8t^A}ULf!TEW&Nv{{o(H^@N$d0z~*>XZ5www$D@92cmi4>s^z+i+9g={upa5_M21sCiH3w?$7zo z_w^p;y@&as51?xxy$ZU{Y$`pHq2YCB0>8jE=O4U;OYkT@!PcNI zcfv{FT(+#e9#)sW_RE>|nKn7``8oxElJhpVw=C2(wzMAB`@1t6zUuk1n_jf%x8=X- z-su<0#qHgdLa|&~w6$l)r;M>!LpJOZ9NC=DSf>(r0FPtQITa*&e{=>uGxWJGXUb}M zvdaiVHqTP%Y_a;i zJl#KMC5dH~1@%Bjb&WjRM7#=Krxx*J?SsYs0gx8n@YL3VNC8yacfa+;+BE{n|)dW|w!onqOE`J?rZSJRz zoeJjh9&W=C`~%1E4gQ8N@jM>GeYhK^a0vHeCtkr#*b^>Nb5VAVeB6SUS%TCWi3Q0u z7#u=9J7UF)>)ZrzW7DN;k){Un%E%|)9j4Nxj9tN%Zvl6=YWopgc1=dr35` zs&j)T55GI02_gfy(0J&>;3{1ini&>47pfV&8&wmtaF}fEQOLk!vh)LKyu#;LmFe*$ z$6*{M4{a#-OfH^B?c%vcLOs`@Q5VmSFdm{Ci_nrcCtIA~Y37TJ!g-1}@M@5FbP@~G zhIMP&p>9ToEd&3eTr?4g-3B6`<5hv6|G^)YF^GHF{qldyaeqow%4PviizO`J8Tl>uyVg)8K zd2Tk07z&uRF$26>+JT%>*;$)(V7^_oU6TQ;)>UnoUn`mLU2R;z06eU9nb0>QEf_%6 aX5i@?D}H8erIcx|bz0DRVYvlYwf_KuDA+6j diff --git a/src/app/lib/create.ts b/src/app/lib/create.ts index 80af2c2..6b90dc9 100644 --- a/src/app/lib/create.ts +++ b/src/app/lib/create.ts @@ -76,7 +76,7 @@ export async function createPub(data: Pub & { genres: number[] }): PromiseNew submission Create an entry for a new story i.e. a thing you intend to submit for publication. - + diff --git a/src/app/ui/forms/schemas.ts b/src/app/ui/forms/schemas.ts index 08580c8..8867706 100644 --- a/src/app/ui/forms/schemas.ts +++ b/src/app/ui/forms/schemas.ts @@ -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() }) diff --git a/src/app/ui/tables/inputs/genrePickerInput.tsx b/src/app/ui/tables/inputs/genrePickerInput.tsx index 3264547..8cf61eb 100644 --- a/src/app/ui/tables/inputs/genrePickerInput.tsx +++ b/src/app/ui/tables/inputs/genrePickerInput.tsx @@ -26,22 +26,21 @@ export default function GenrePickerInputCell(props: CellContext) { async function onSubmit({ genres }: { genres: number[] }, event: Event) { event.preventDefault() - const genresArray = genres.map((e) => { return { id: e } }) - console.log(`genres: ${genres}, genresArray: ${JSON.stringify(genresArray)}`) - toast({ - title: "You submitted the following values:", - description: ( -
-          {JSON.stringify(genres)}
-        
- ), - }) - const res = await updateGenres({ - id, - table, - genres: genresArray, - pathname - }) + try { + const genresArray = genres.map((e) => { return { id: e } }) + 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) } diff --git a/src/app/ui/tables/inputs/numberInput.tsx b/src/app/ui/tables/inputs/numberInput.tsx index e840c69..0a13cee 100644 --- a/src/app/ui/tables/inputs/numberInput.tsx +++ b/src/app/ui/tables/inputs/numberInput.tsx @@ -27,39 +27,34 @@ export default function NumberInputCell(props: CellContext) { }) - function onSubmit(value: z.infer) { - toast({ - title: "You submitted the following values:", - description: ( -
-          {JSON.stringify(value, null, 2)}
-        
- ), - }) - updateField({ - id, - table, - number: value[column], - column, - pathname - }) + async function onSubmit(value: z.infer) { + try { + const res = await updateField({ + id, + table, + 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: ( -
-          {JSON.stringify(errors, null, 2)}
-        
- ), + description: errors.message, }) console.log(JSON.stringify(errors)) } return (
setIsActive(prev => !prev)} + onDoubleClick={() => setIsActive(true)} className="w-full h-fit flex items-center justify-center" tabIndex={0} onKeyDown={e => { diff --git a/src/app/ui/tables/inputs/textInput.tsx b/src/app/ui/tables/inputs/textInput.tsx index c731a92..c06b1d3 100644 --- a/src/app/ui/tables/inputs/textInput.tsx +++ b/src/app/ui/tables/inputs/textInput.tsx @@ -28,21 +28,21 @@ export function TextInputCell(props: CellContext) { }) async function onSubmit(value: z.infer) { - toast({ - title: "You submitted the following values:", - description: ( -
-          {JSON.stringify(value, null, 2)}
-        
- ), - }) - const res = await updateField({ - id, - table, - datum: value[column], - column, - pathname - }) + try { + const res = await updateField({ + id, + table, + datum: value[column], + 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) }