feat: add update and delete functionality for courses, events, and species; enhance attendance tracking and category creation
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useAction } from 'next-safe-action/hooks';
|
||||
import { useForm } from 'react-hook-form';
|
||||
|
||||
import { Button } from '@kit/ui/button';
|
||||
@@ -17,58 +16,104 @@ import {
|
||||
FormMessage,
|
||||
} from '@kit/ui/form';
|
||||
import { Input } from '@kit/ui/input';
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
import { useActionWithToast } from '@kit/ui/use-action-with-toast';
|
||||
|
||||
import { CreateEventSchema } from '../schema/event.schema';
|
||||
import { createEvent } from '../server/actions/event-actions';
|
||||
import { CreateEventSchema, UpdateEventSchema } from '../schema/event.schema';
|
||||
import { createEvent, updateEvent } from '../server/actions/event-actions';
|
||||
|
||||
interface Props {
|
||||
accountId: string;
|
||||
account: string;
|
||||
/** If provided, form operates in edit mode */
|
||||
eventId?: string;
|
||||
initialData?: {
|
||||
name: string;
|
||||
description: string;
|
||||
eventDate: string;
|
||||
eventTime: string;
|
||||
endDate: string;
|
||||
location: string;
|
||||
capacity: number | undefined;
|
||||
minAge: number | undefined;
|
||||
maxAge: number | undefined;
|
||||
fee: number;
|
||||
status: string;
|
||||
registrationDeadline: string;
|
||||
contactName: string;
|
||||
contactEmail: string;
|
||||
contactPhone: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function CreateEventForm({ accountId, account }: Props) {
|
||||
export function CreateEventForm({
|
||||
accountId,
|
||||
account,
|
||||
eventId,
|
||||
initialData,
|
||||
}: Props) {
|
||||
const router = useRouter();
|
||||
const isEdit = Boolean(eventId);
|
||||
|
||||
const form = useForm({
|
||||
resolver: zodResolver(CreateEventSchema),
|
||||
resolver: zodResolver(isEdit ? UpdateEventSchema : CreateEventSchema),
|
||||
defaultValues: {
|
||||
accountId,
|
||||
name: '',
|
||||
description: '',
|
||||
eventDate: '',
|
||||
eventTime: '',
|
||||
endDate: '',
|
||||
location: '',
|
||||
capacity: undefined as number | undefined,
|
||||
minAge: undefined as number | undefined,
|
||||
maxAge: undefined as number | undefined,
|
||||
fee: 0,
|
||||
status: 'planned' as const,
|
||||
registrationDeadline: '',
|
||||
contactName: '',
|
||||
contactEmail: '',
|
||||
contactPhone: '',
|
||||
...(isEdit ? { eventId } : { accountId }),
|
||||
name: initialData?.name ?? '',
|
||||
description: initialData?.description ?? '',
|
||||
eventDate: initialData?.eventDate ?? '',
|
||||
eventTime: initialData?.eventTime ?? '',
|
||||
endDate: initialData?.endDate ?? '',
|
||||
location: initialData?.location ?? '',
|
||||
capacity: initialData?.capacity ?? (undefined as number | undefined),
|
||||
minAge: initialData?.minAge ?? (undefined as number | undefined),
|
||||
maxAge: initialData?.maxAge ?? (undefined as number | undefined),
|
||||
fee: initialData?.fee ?? 0,
|
||||
status: (initialData?.status ?? 'planned') as
|
||||
| 'planned'
|
||||
| 'open'
|
||||
| 'full'
|
||||
| 'running'
|
||||
| 'completed'
|
||||
| 'cancelled',
|
||||
registrationDeadline: initialData?.registrationDeadline ?? '',
|
||||
contactName: initialData?.contactName ?? '',
|
||||
contactEmail: initialData?.contactEmail ?? '',
|
||||
contactPhone: initialData?.contactPhone ?? '',
|
||||
},
|
||||
});
|
||||
|
||||
const { execute, isPending } = useAction(createEvent, {
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Veranstaltung erfolgreich erstellt');
|
||||
router.push(`/home/${account}/events`);
|
||||
}
|
||||
const { execute: execCreate, isPending: isCreating } = useActionWithToast(
|
||||
createEvent,
|
||||
{
|
||||
successMessage: 'Veranstaltung erfolgreich erstellt',
|
||||
errorMessage: 'Fehler beim Erstellen der Veranstaltung',
|
||||
onSuccess: () => router.push(`/home/${account}/events`),
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(
|
||||
error.serverError ?? 'Fehler beim Erstellen der Veranstaltung',
|
||||
);
|
||||
);
|
||||
|
||||
const { execute: execUpdate, isPending: isUpdating } = useActionWithToast(
|
||||
updateEvent,
|
||||
{
|
||||
successMessage: 'Veranstaltung aktualisiert',
|
||||
errorMessage: 'Fehler beim Aktualisieren',
|
||||
onSuccess: () => router.push(`/home/${account}/events/${eventId}`),
|
||||
},
|
||||
});
|
||||
);
|
||||
|
||||
const isPending = isCreating || isUpdating;
|
||||
|
||||
const handleSubmit = (data: Record<string, unknown>) => {
|
||||
if (isEdit && eventId) {
|
||||
execUpdate({ ...data, eventId } as any);
|
||||
} else {
|
||||
execCreate({ ...data, accountId } as any);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit((data) => execute(data))}
|
||||
onSubmit={form.handleSubmit(handleSubmit as any)}
|
||||
className="space-y-6"
|
||||
>
|
||||
<Card>
|
||||
@@ -241,7 +286,7 @@ export function CreateEventForm({ accountId, account }: Props) {
|
||||
name="fee"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Gebühr (€)</FormLabel>
|
||||
<FormLabel>Gebühr (EUR)</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
type="number"
|
||||
@@ -365,7 +410,11 @@ export function CreateEventForm({ accountId, account }: Props) {
|
||||
disabled={isPending}
|
||||
data-test="event-submit-btn"
|
||||
>
|
||||
{isPending ? 'Wird erstellt...' : 'Veranstaltung erstellen'}
|
||||
{isPending
|
||||
? 'Wird gespeichert...'
|
||||
: isEdit
|
||||
? 'Veranstaltung aktualisieren'
|
||||
: 'Veranstaltung erstellen'}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -29,6 +29,11 @@ export const CreateEventSchema = z.object({
|
||||
});
|
||||
export type CreateEventInput = z.infer<typeof CreateEventSchema>;
|
||||
|
||||
export const UpdateEventSchema = CreateEventSchema.partial().extend({
|
||||
eventId: z.string().uuid(),
|
||||
});
|
||||
export type UpdateEventInput = z.infer<typeof UpdateEventSchema>;
|
||||
|
||||
export const EventRegistrationSchema = z.object({
|
||||
eventId: z.string().uuid(),
|
||||
firstName: z.string().min(1),
|
||||
|
||||
@@ -8,6 +8,7 @@ import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import {
|
||||
CreateEventSchema,
|
||||
UpdateEventSchema,
|
||||
EventRegistrationSchema,
|
||||
CreateHolidayPassSchema,
|
||||
} from '../../schema/event.schema';
|
||||
@@ -26,6 +27,32 @@ export const createEvent = authActionClient
|
||||
return { success: true, data: result };
|
||||
});
|
||||
|
||||
export const updateEvent = authActionClient
|
||||
.inputSchema(UpdateEventSchema)
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createEventManagementApi(client);
|
||||
|
||||
logger.info({ name: 'event.update' }, 'Updating event...');
|
||||
const result = await api.updateEvent(input);
|
||||
logger.info({ name: 'event.update' }, 'Event updated');
|
||||
return { success: true, data: result };
|
||||
});
|
||||
|
||||
export const deleteEvent = authActionClient
|
||||
.inputSchema(z.object({ eventId: z.string().uuid() }))
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createEventManagementApi(client);
|
||||
|
||||
logger.info({ name: 'event.delete' }, 'Cancelling event...');
|
||||
await api.deleteEvent(input.eventId);
|
||||
logger.info({ name: 'event.delete' }, 'Event cancelled');
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
export const registerForEvent = authActionClient
|
||||
.inputSchema(EventRegistrationSchema)
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
|
||||
@@ -2,7 +2,10 @@ import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import type { Database } from '@kit/supabase/database';
|
||||
|
||||
import type { CreateEventInput } from '../schema/event.schema';
|
||||
import type {
|
||||
CreateEventInput,
|
||||
UpdateEventInput,
|
||||
} from '../schema/event.schema';
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
@@ -69,7 +72,7 @@ export function createEventManagementApi(client: SupabaseClient<Database>) {
|
||||
account_id: input.accountId,
|
||||
name: input.name,
|
||||
description: input.description || null,
|
||||
event_date: input.eventDate || null,
|
||||
event_date: input.eventDate,
|
||||
event_time: input.eventTime || null,
|
||||
end_date: input.endDate || null,
|
||||
location: input.location || null,
|
||||
@@ -89,6 +92,50 @@ export function createEventManagementApi(client: SupabaseClient<Database>) {
|
||||
return data;
|
||||
},
|
||||
|
||||
async updateEvent(input: UpdateEventInput) {
|
||||
const update: Record<string, unknown> = {};
|
||||
if (input.name !== undefined) update.name = input.name;
|
||||
if (input.description !== undefined)
|
||||
update.description = input.description || null;
|
||||
if (input.eventDate !== undefined)
|
||||
update.event_date = input.eventDate || null;
|
||||
if (input.eventTime !== undefined)
|
||||
update.event_time = input.eventTime || null;
|
||||
if (input.endDate !== undefined) update.end_date = input.endDate || null;
|
||||
if (input.location !== undefined)
|
||||
update.location = input.location || null;
|
||||
if (input.capacity !== undefined) update.capacity = input.capacity;
|
||||
if (input.minAge !== undefined) update.min_age = input.minAge ?? null;
|
||||
if (input.maxAge !== undefined) update.max_age = input.maxAge ?? null;
|
||||
if (input.fee !== undefined) update.fee = input.fee;
|
||||
if (input.status !== undefined) update.status = input.status;
|
||||
if (input.registrationDeadline !== undefined)
|
||||
update.registration_deadline = input.registrationDeadline || null;
|
||||
if (input.contactName !== undefined)
|
||||
update.contact_name = input.contactName || null;
|
||||
if (input.contactEmail !== undefined)
|
||||
update.contact_email = input.contactEmail || null;
|
||||
if (input.contactPhone !== undefined)
|
||||
update.contact_phone = input.contactPhone || null;
|
||||
|
||||
const { data, error } = await client
|
||||
.from('events')
|
||||
.update(update)
|
||||
.eq('id', input.eventId)
|
||||
.select()
|
||||
.single();
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
async deleteEvent(eventId: string) {
|
||||
const { error } = await client
|
||||
.from('events')
|
||||
.update({ status: 'cancelled' })
|
||||
.eq('id', eventId);
|
||||
if (error) throw error;
|
||||
},
|
||||
|
||||
async registerForEvent(input: {
|
||||
eventId: string;
|
||||
firstName: string;
|
||||
|
||||
Reference in New Issue
Block a user