From 43a8c303983e785abdda7e28fb67075c1a707f68 Mon Sep 17 00:00:00 2001 From: Patryk Omiotek Date: Thu, 21 Mar 2024 20:30:47 +0100 Subject: [PATCH] feat: display error on the frontend side --- .../app/(public)/job-offers/create/actions.ts | 19 +++++++++++++++---- .../app/(public)/job-offers/create/page.tsx | 8 ++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/apps/jobboard/src/app/(public)/job-offers/create/actions.ts b/apps/jobboard/src/app/(public)/job-offers/create/actions.ts index b08d703..dd30fba 100644 --- a/apps/jobboard/src/app/(public)/job-offers/create/actions.ts +++ b/apps/jobboard/src/app/(public)/job-offers/create/actions.ts @@ -3,6 +3,7 @@ import db from '@jobboard/prisma-client'; import { redirect } from 'next/navigation'; import { CreateOfferDto, offerSchema } from './types'; +import { ZodError } from 'zod'; // export const createJobOfferAction = async (data: FormData) => { export const createJobOfferAction = async (data: CreateOfferDto) => { @@ -24,11 +25,21 @@ export const createJobOfferAction = async (data: CreateOfferDto) => { // city, // }); - const offer = offerSchema.parse(data); + try { + const offer = offerSchema.parse(data); - await db.jobOffer.create({ data: offer }); + await db.jobOffer.create({ data: offer }); - // redirect('/job-offers'); + return { status: 'ok', message: 'created' }; // 201 created + } catch (error) { + if (error instanceof ZodError) { + // logger + return { status: 'error', message: 'sth' }; // 400 + } + // } else if (error instanceof Prisma) { // TODO: check error type + // + // } + } - return { status: 'ok' }; + return { status: 'error', message: 'sth' }; // 400 }; diff --git a/apps/jobboard/src/app/(public)/job-offers/create/page.tsx b/apps/jobboard/src/app/(public)/job-offers/create/page.tsx index cdb4406..2066e1e 100644 --- a/apps/jobboard/src/app/(public)/job-offers/create/page.tsx +++ b/apps/jobboard/src/app/(public)/job-offers/create/page.tsx @@ -1,12 +1,12 @@ 'use client'; -import { Button, Header, Input } from '@jobboard/common-ui'; +import { Alert, Button, Header, Input } from '@jobboard/common-ui'; import { createJobOfferAction } from './actions'; import { CreateOfferDto, offerSchema } from './types'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; -import { use, useTransition } from 'react'; +import { use, useState, useTransition } from 'react'; import { useRouter } from 'next/navigation'; export default function CreateOfferPage() { @@ -19,6 +19,7 @@ export default function CreateOfferPage() { }); const router = useRouter(); const [isPending, setTransition] = useTransition(); + const [isError, setIsError] = useState(false); const handleOfferFormSubmit = async (data: CreateOfferDto) => { // console.log({ data }); @@ -27,12 +28,15 @@ export default function CreateOfferPage() { if (result.status === 'ok') { setTransition(() => router.push('/job-offers')); setTransition(() => router.refresh()); + } else if (result.status === 'error') { + setIsError(true); } }; return (
Create Offer
+ {isError && }