feat(event-form): fix view of created_at and updated_at on edit and implement redirect for save and cancel buttons

This commit is contained in:
micha 2025-06-18 22:47:10 +02:00
parent 05f56a2186
commit 4476ee6eb9
2 changed files with 19 additions and 3 deletions

View file

@ -17,6 +17,7 @@ export default function Home() {
</h1>
<RedirectButton redirectUrl='/logout' buttonText='Logout' />
<RedirectButton redirectUrl='/settings' buttonText='Settings' />
<RedirectButton redirectUrl='/home/new-event' buttonText='New Event' />
</div>
</div>
);

View file

@ -12,6 +12,7 @@ import {
usePatchApiEventEventID,
} from '@/generated/api/event/event';
import ParticipantListEntry from '@/components/custom-ui/participantListEntry';
import { useRouter } from 'next/navigation';
interface EventFormProps {
type: 'create' | 'edit';
@ -32,6 +33,7 @@ const EventForm: React.FC<EventFormProps> = (props) => {
query: { enabled: props.type === 'edit' },
});
const patchEvent = usePatchApiEventEventID();
const router = useRouter();
// Extract event fields for form defaults
const event = eventData?.data?.event;
@ -130,14 +132,23 @@ const EventForm: React.FC<EventFormProps> = (props) => {
createEvent({ data });
}
router.back();
}
// Calculate values for organiser, created, and updated
const organiserValue = isLoading
? 'Loading...'
: data?.data.user?.name || 'Unknown User';
const createdAtValue = new Date().toISOString();
const updatedAtValue = new Date().toISOString();
// Use DB values for created_at/updated_at in edit mode
const createdAtValue =
props.type === 'edit' && event?.created_at
? event.created_at
: new Date().toISOString();
const updatedAtValue =
props.type === 'edit' && event?.updated_at
? event.updated_at
: new Date().toISOString();
// Format date for display
const createdAtDisplay = new Date(createdAtValue).toLocaleDateString();
@ -252,7 +263,11 @@ const EventForm: React.FC<EventFormProps> = (props) => {
<div className='flex flex-row gap-2 justify-end mt-4 mb-6'>
<div className='w-[20%] grid max-sm:w-[40%]'>
<Button type='button' variant='secondary'>
<Button
type='button'
variant='secondary'
onClick={() => router.back()}
>
cancel
</Button>
{/* TODO: add onClick handler to cancel cancel */}