diff --git a/client/src/lib/api-types/event.ts b/client/src/lib/api-types/event.ts index 7cc563e0..99ad1485 100644 --- a/client/src/lib/api-types/event.ts +++ b/client/src/lib/api-types/event.ts @@ -9,7 +9,12 @@ import * as z from 'zod'; import { event } from './schemas'; import type { SuccessResponse, ErrorResponse } from './index'; +// Success response for GetEvent API export interface GetEventSuccAPI + extends SuccessResponse>> {} + +// Success response for CreateEvent API +export interface CreateEventSuccAPI extends SuccessResponse> {} /** @@ -22,3 +27,12 @@ export type GetEventFailAPI = ErrorResponse< | 'Unable to connect to the server. Please check your network connection' | 'Data inconsistency detected. Please refresh the page and try again' >; + +// Failure response for CreateEvent API +export type CreateEventFailAPI = ErrorResponse< + | 'Invalid input data. Please check your form and try again' + | 'Event could not be created. Please try again later' + | 'There was a problem accessing the database. Please try again later' + | 'Too many requests. Please try again later' + | 'Unable to connect to the server. Please check your network connection' +>; diff --git a/client/src/pages/user/create-events.tsx b/client/src/pages/user/create-events.tsx index 96b444b6..54632815 100644 --- a/client/src/pages/user/create-events.tsx +++ b/client/src/pages/user/create-events.tsx @@ -1,144 +1,223 @@ -// src/pages/user/createevents.tsx - -import React from 'react'; -import { useForm } from 'react-hook-form'; -import { z, ZodSchema } from 'zod'; -import { zodResolver } from '@hookform/resolvers/zod'; - -// Define your form schema using zod -const eventSchema: ZodSchema = z.object({ - title: z.string().min(1, 'Title is required'), - date: z.string().min(1, 'Date is required'), - time: z.string().min(1, 'Time is required'), - location: z.string().min(1, 'Location is required'), - description: z.string().optional(), - file: z.instanceof(File).optional(), -}); +import React, { useState } from 'react'; +import { useForm, useFormState } from 'react-hook-form'; +import httpClient from '@utils/http'; +import { Button } from '@components/ui/button'; +import { Input } from '@components/ui/input'; +import { + Form, + FormField, + FormItem, + FormLabel, + FormControl, + FormDescription, + FormMessage, +} from '@components/ui/form'; +import { cn } from '@utils/tailwind'; +import { CreateEventSuccAPI } from '@lib/api-types/event'; +import { AxiosError } from 'axios'; // Define TypeScript type for form data -type EventFormData = z.infer; +type EventFormData = { + title: string; + date: string; // The date should be in YYYY-MM-DD format + time: string; + location: string; + description?: string; +}; -const EventCreationPage: React.FC = () => { - const { register, handleSubmit, setValue } = useForm({ - resolver: zodResolver(eventSchema), +const EventCreationPage: React.FC<{ className?: string }> = ({ + className, + ...props +}) => { + const [error, setError] = useState(null); + const [successMessage, setSuccessMessage] = useState(null); + + const eventForm = useForm({ mode: 'onBlur', }); - // Handle form submission - const onSubmit = (data: EventFormData) => { - console.log('Form Data:', data); - - // Handle file upload here if needed - }; + const { isSubmitting } = useFormState({ + control: eventForm.control, + }); - // Handle file input change - const handleFileChange = (event: React.ChangeEvent) => { - const file = event.target.files?.[0]; - if (file) { - setValue('file', file); + const handleSave = async (data: EventFormData) => { + console.log('Form data:', data); // Log form data for debugging + try { + console.log('Sending request...'); + const response = await httpClient.post( + { + uri: '/event', + payload: data, + withCredentials: 'access', + }, + ); + console.log('Response received:', response); + setSuccessMessage('Event created successfully'); + setError(null); + eventForm.reset(); + } catch (err) { + console.error('Request failed:', err); + if (err instanceof AxiosError) { + if (err.response) { + setError( + `Server error: ${err.response?.data?.errors?.[0]?.message || 'An unexpected error occurred'}`, + ); + } else if (err.request) { + setError( + 'Network error: Please check your connection and try again.', + ); + } else { + setError( + `Error creating event! ${err.message || 'An unexpected error occurred'}`, + ); + } + } else { + setError( + `Error creating event! ${String(err) || 'An unexpected error occurred'}`, + ); + } + setSuccessMessage(null); } }; return ( -
-

Create Event

-
-
- - -
+
+

Create Event

+ + + {error &&

{error}

} + {successMessage &&

{successMessage}

} -
- - ( + + Title + + + + {fieldState.error ? ( + {fieldState.error.message} + ) : ( + Enter the event title. + )} + + )} /> -
-
- - ( + + Date + + + + {fieldState.error ? ( + {fieldState.error.message} + ) : ( + Select the event date. + )} + + )} /> -
-
- - ( + + Time + + + + {fieldState.error ? ( + {fieldState.error.message} + ) : ( + Enter the event time. + )} + + )} /> -
-
- -