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,53 +16,100 @@ 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 { CreateCourseSchema } from '../schema/course.schema';
|
||||
import { createCourse } from '../server/actions/course-actions';
|
||||
import {
|
||||
CreateCourseSchema,
|
||||
UpdateCourseSchema,
|
||||
} from '../schema/course.schema';
|
||||
import { createCourse, updateCourse } from '../server/actions/course-actions';
|
||||
|
||||
interface Props {
|
||||
accountId: string;
|
||||
account: string;
|
||||
/** If provided, form operates in edit mode */
|
||||
courseId?: string;
|
||||
initialData?: {
|
||||
courseNumber: string;
|
||||
name: string;
|
||||
description: string;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
fee: number;
|
||||
reducedFee: number;
|
||||
capacity: number;
|
||||
minParticipants: number;
|
||||
status: string;
|
||||
registrationDeadline: string;
|
||||
notes: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function CreateCourseForm({ accountId, account }: Props) {
|
||||
export function CreateCourseForm({
|
||||
accountId,
|
||||
account,
|
||||
courseId,
|
||||
initialData,
|
||||
}: Props) {
|
||||
const router = useRouter();
|
||||
const isEdit = Boolean(courseId);
|
||||
|
||||
const form = useForm({
|
||||
resolver: zodResolver(CreateCourseSchema),
|
||||
resolver: zodResolver(isEdit ? UpdateCourseSchema : CreateCourseSchema),
|
||||
defaultValues: {
|
||||
accountId,
|
||||
courseNumber: '',
|
||||
name: '',
|
||||
description: '',
|
||||
startDate: '',
|
||||
endDate: '',
|
||||
fee: 0,
|
||||
reducedFee: 0,
|
||||
capacity: 20,
|
||||
minParticipants: 5,
|
||||
status: 'planned' as const,
|
||||
registrationDeadline: '',
|
||||
notes: '',
|
||||
...(isEdit ? { courseId } : { accountId }),
|
||||
courseNumber: initialData?.courseNumber ?? '',
|
||||
name: initialData?.name ?? '',
|
||||
description: initialData?.description ?? '',
|
||||
startDate: initialData?.startDate ?? '',
|
||||
endDate: initialData?.endDate ?? '',
|
||||
fee: initialData?.fee ?? 0,
|
||||
reducedFee: initialData?.reducedFee ?? 0,
|
||||
capacity: initialData?.capacity ?? 20,
|
||||
minParticipants: initialData?.minParticipants ?? 5,
|
||||
status: (initialData?.status ?? 'planned') as
|
||||
| 'planned'
|
||||
| 'open'
|
||||
| 'running'
|
||||
| 'completed'
|
||||
| 'cancelled',
|
||||
registrationDeadline: initialData?.registrationDeadline ?? '',
|
||||
notes: initialData?.notes ?? '',
|
||||
},
|
||||
});
|
||||
|
||||
const { execute, isPending } = useAction(createCourse, {
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Kurs erfolgreich erstellt');
|
||||
router.push(`/home/${account}/courses`);
|
||||
}
|
||||
const { execute: execCreate, isPending: isCreating } = useActionWithToast(
|
||||
createCourse,
|
||||
{
|
||||
successMessage: 'Kurs erfolgreich erstellt',
|
||||
errorMessage: 'Fehler beim Erstellen des Kurses',
|
||||
onSuccess: () => router.push(`/home/${account}/courses`),
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Erstellen des Kurses');
|
||||
);
|
||||
|
||||
const { execute: execUpdate, isPending: isUpdating } = useActionWithToast(
|
||||
updateCourse,
|
||||
{
|
||||
successMessage: 'Kurs aktualisiert',
|
||||
errorMessage: 'Fehler beim Aktualisieren',
|
||||
onSuccess: () => router.push(`/home/${account}/courses/${courseId}`),
|
||||
},
|
||||
});
|
||||
);
|
||||
|
||||
const isPending = isCreating || isUpdating;
|
||||
|
||||
const handleSubmit = (data: Record<string, unknown>) => {
|
||||
if (isEdit && courseId) {
|
||||
execUpdate({ ...data, courseId } 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>
|
||||
@@ -167,7 +213,7 @@ export function CreateCourseForm({ accountId, account }: Props) {
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Kapazität</CardTitle>
|
||||
<CardTitle>Kapazität & Gebühren</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<FormField
|
||||
@@ -211,7 +257,7 @@ export function CreateCourseForm({ accountId, account }: Props) {
|
||||
name="fee"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Gebühr (€)</FormLabel>
|
||||
<FormLabel>Gebühr (EUR)</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
type="number"
|
||||
@@ -230,7 +276,7 @@ export function CreateCourseForm({ accountId, account }: Props) {
|
||||
name="reducedFee"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Ermäßigte Gebühr (€)</FormLabel>
|
||||
<FormLabel>Ermäßigte Gebühr (EUR)</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
type="number"
|
||||
@@ -274,42 +320,35 @@ export function CreateCourseForm({ accountId, account }: Props) {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<div className="sm:col-span-1">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="notes"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Notizen</FormLabel>
|
||||
<FormControl>
|
||||
<textarea
|
||||
{...field}
|
||||
className="border-input bg-background flex min-h-[80px] w-full rounded-md border px-3 py-2 text-sm"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="notes"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Notizen</FormLabel>
|
||||
<FormControl>
|
||||
<textarea
|
||||
{...field}
|
||||
className="border-input bg-background flex min-h-[80px] w-full rounded-md border px-3 py-2 text-sm"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => router.back()}
|
||||
data-test="course-cancel-btn"
|
||||
>
|
||||
<Button type="button" variant="outline" onClick={() => router.back()}>
|
||||
Abbrechen
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={isPending}
|
||||
data-test="course-submit-btn"
|
||||
>
|
||||
{isPending ? 'Wird erstellt...' : 'Kurs erstellen'}
|
||||
<Button type="submit" disabled={isPending}>
|
||||
{isPending
|
||||
? 'Wird gespeichert...'
|
||||
: isEdit
|
||||
? 'Kurs aktualisieren'
|
||||
: 'Kurs erstellen'}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -39,6 +39,7 @@ export type CreateCourseInput = z.infer<typeof CreateCourseSchema>;
|
||||
export const UpdateCourseSchema = CreateCourseSchema.partial().extend({
|
||||
courseId: z.string().uuid(),
|
||||
});
|
||||
export type UpdateCourseInput = z.infer<typeof UpdateCourseSchema>;
|
||||
|
||||
export const EnrollParticipantSchema = z.object({
|
||||
courseId: z.string().uuid(),
|
||||
|
||||
@@ -8,6 +8,7 @@ import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import {
|
||||
CreateCourseSchema,
|
||||
UpdateCourseSchema,
|
||||
EnrollParticipantSchema,
|
||||
CreateSessionSchema,
|
||||
CreateCategorySchema,
|
||||
@@ -29,6 +30,32 @@ export const createCourse = authActionClient
|
||||
return { success: true, data: result };
|
||||
});
|
||||
|
||||
export const updateCourse = authActionClient
|
||||
.inputSchema(UpdateCourseSchema)
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createCourseManagementApi(client);
|
||||
|
||||
logger.info({ name: 'course.update' }, 'Updating course...');
|
||||
const result = await api.updateCourse(input);
|
||||
logger.info({ name: 'course.update' }, 'Course updated');
|
||||
return { success: true, data: result };
|
||||
});
|
||||
|
||||
export const deleteCourse = authActionClient
|
||||
.inputSchema(z.object({ courseId: z.string().uuid() }))
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createCourseManagementApi(client);
|
||||
|
||||
logger.info({ name: 'course.delete' }, 'Archiving course...');
|
||||
await api.deleteCourse(input.courseId);
|
||||
logger.info({ name: 'course.delete' }, 'Course archived');
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
export const enrollParticipant = authActionClient
|
||||
.inputSchema(EnrollParticipantSchema)
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { Database } from '@kit/supabase/database';
|
||||
|
||||
import type {
|
||||
CreateCourseInput,
|
||||
UpdateCourseInput,
|
||||
EnrollParticipantInput,
|
||||
} from '../schema/course.schema';
|
||||
|
||||
@@ -78,6 +79,51 @@ export function createCourseManagementApi(client: SupabaseClient<Database>) {
|
||||
return data;
|
||||
},
|
||||
|
||||
async updateCourse(input: UpdateCourseInput) {
|
||||
const update: Record<string, unknown> = {};
|
||||
if (input.name !== undefined) update.name = input.name;
|
||||
if (input.courseNumber !== undefined)
|
||||
update.course_number = input.courseNumber || null;
|
||||
if (input.description !== undefined)
|
||||
update.description = input.description || null;
|
||||
if (input.categoryId !== undefined)
|
||||
update.category_id = input.categoryId || null;
|
||||
if (input.instructorId !== undefined)
|
||||
update.instructor_id = input.instructorId || null;
|
||||
if (input.locationId !== undefined)
|
||||
update.location_id = input.locationId || null;
|
||||
if (input.startDate !== undefined)
|
||||
update.start_date = input.startDate || null;
|
||||
if (input.endDate !== undefined) update.end_date = input.endDate || null;
|
||||
if (input.fee !== undefined) update.fee = input.fee;
|
||||
if (input.reducedFee !== undefined)
|
||||
update.reduced_fee = input.reducedFee ?? null;
|
||||
if (input.capacity !== undefined) update.capacity = input.capacity;
|
||||
if (input.minParticipants !== undefined)
|
||||
update.min_participants = input.minParticipants;
|
||||
if (input.status !== undefined) update.status = input.status;
|
||||
if (input.registrationDeadline !== undefined)
|
||||
update.registration_deadline = input.registrationDeadline || null;
|
||||
if (input.notes !== undefined) update.notes = input.notes || null;
|
||||
|
||||
const { data, error } = await client
|
||||
.from('courses')
|
||||
.update(update)
|
||||
.eq('id', input.courseId)
|
||||
.select()
|
||||
.single();
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
async deleteCourse(courseId: string) {
|
||||
const { error } = await client
|
||||
.from('courses')
|
||||
.update({ status: 'cancelled' })
|
||||
.eq('id', courseId);
|
||||
if (error) throw error;
|
||||
},
|
||||
|
||||
// --- Enrollment ---
|
||||
async enrollParticipant(input: EnrollParticipantInput) {
|
||||
// Check capacity
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -20,7 +20,10 @@ import { Input } from '@kit/ui/input';
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
|
||||
import { CreateFishSpeciesSchema } from '../schema/fischerei.schema';
|
||||
import { createSpecies } from '../server/actions/fischerei-actions';
|
||||
import {
|
||||
createSpecies,
|
||||
updateSpecies,
|
||||
} from '../server/actions/fischerei-actions';
|
||||
|
||||
interface CreateSpeciesFormProps {
|
||||
accountId: string;
|
||||
@@ -65,22 +68,50 @@ export function CreateSpeciesForm({
|
||||
},
|
||||
});
|
||||
|
||||
const { execute, isPending } = useAction(createSpecies, {
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success(isEdit ? 'Fischart aktualisiert' : 'Fischart erstellt');
|
||||
router.push(`/home/${account}/fischerei/species`);
|
||||
}
|
||||
const { execute: executeCreate, isPending: isCreating } = useAction(
|
||||
createSpecies,
|
||||
{
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Fischart erstellt');
|
||||
router.push(`/home/${account}/fischerei/species`);
|
||||
}
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
},
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
);
|
||||
|
||||
const { execute: executeUpdate, isPending: isUpdating } = useAction(
|
||||
updateSpecies,
|
||||
{
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Fischart aktualisiert');
|
||||
router.push(`/home/${account}/fischerei/species`);
|
||||
}
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
},
|
||||
},
|
||||
});
|
||||
);
|
||||
|
||||
const isPending = isCreating || isUpdating;
|
||||
|
||||
const handleSubmit = (data: Record<string, unknown>) => {
|
||||
if (isEdit && species?.id) {
|
||||
executeUpdate({ ...data, speciesId: String(species.id) } as any);
|
||||
} else {
|
||||
executeCreate(data as any);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit((data) => execute(data))}
|
||||
onSubmit={form.handleSubmit((data) => handleSubmit(data))}
|
||||
className="space-y-6"
|
||||
>
|
||||
{/* Card 1: Grunddaten */}
|
||||
|
||||
@@ -21,13 +21,17 @@ import { Input } from '@kit/ui/input';
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
|
||||
import { CreateStockingSchema } from '../schema/fischerei.schema';
|
||||
import { createStocking } from '../server/actions/fischerei-actions';
|
||||
import {
|
||||
createStocking,
|
||||
updateStocking,
|
||||
} from '../server/actions/fischerei-actions';
|
||||
|
||||
interface CreateStockingFormProps {
|
||||
accountId: string;
|
||||
account: string;
|
||||
waters: Array<{ id: string; name: string }>;
|
||||
species: Array<{ id: string; name: string }>;
|
||||
stocking?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function CreateStockingForm({
|
||||
@@ -35,41 +39,86 @@ export function CreateStockingForm({
|
||||
account,
|
||||
waters,
|
||||
species,
|
||||
stocking,
|
||||
}: CreateStockingFormProps) {
|
||||
const router = useRouter();
|
||||
const isEdit = !!stocking;
|
||||
|
||||
const form = useForm({
|
||||
resolver: zodResolver(CreateStockingSchema),
|
||||
defaultValues: {
|
||||
accountId,
|
||||
waterId: '',
|
||||
speciesId: '',
|
||||
stockingDate: todayISO(),
|
||||
quantity: 0,
|
||||
weightKg: undefined as number | undefined,
|
||||
ageClass: 'sonstige' as const,
|
||||
costEuros: undefined as number | undefined,
|
||||
supplierId: undefined as string | undefined,
|
||||
remarks: '',
|
||||
waterId: (stocking?.water_id as string) ?? '',
|
||||
speciesId: (stocking?.species_id as string) ?? '',
|
||||
stockingDate: (stocking?.stocking_date as string) ?? todayISO(),
|
||||
quantity: stocking?.quantity != null ? Number(stocking.quantity) : 0,
|
||||
weightKg:
|
||||
stocking?.weight_kg != null
|
||||
? Number(stocking.weight_kg)
|
||||
: (undefined as number | undefined),
|
||||
ageClass: ((stocking?.age_class as string) ?? 'sonstige') as
|
||||
| 'brut'
|
||||
| 'soemmerlinge'
|
||||
| 'einsoemmerig'
|
||||
| 'zweisoemmerig'
|
||||
| 'dreisoemmerig'
|
||||
| 'vorgestreckt'
|
||||
| 'setzlinge'
|
||||
| 'laichfische'
|
||||
| 'sonstige',
|
||||
costEuros:
|
||||
stocking?.cost_euros != null
|
||||
? Number(stocking.cost_euros)
|
||||
: (undefined as number | undefined),
|
||||
supplierId: (stocking?.supplier_id as string | undefined) ?? undefined,
|
||||
remarks: (stocking?.remarks as string) ?? '',
|
||||
},
|
||||
});
|
||||
|
||||
const { execute, isPending } = useAction(createStocking, {
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Besatz eingetragen');
|
||||
router.push(`/home/${account}/fischerei/stocking`);
|
||||
}
|
||||
const { execute: executeCreate, isPending: isCreating } = useAction(
|
||||
createStocking,
|
||||
{
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Besatz eingetragen');
|
||||
router.push(`/home/${account}/fischerei/stocking`);
|
||||
}
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
},
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
);
|
||||
|
||||
const { execute: executeUpdate, isPending: isUpdating } = useAction(
|
||||
updateStocking,
|
||||
{
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Besatz aktualisiert');
|
||||
router.push(`/home/${account}/fischerei/stocking`);
|
||||
}
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
},
|
||||
},
|
||||
});
|
||||
);
|
||||
|
||||
const isPending = isCreating || isUpdating;
|
||||
|
||||
const handleSubmit = (data: Record<string, unknown>) => {
|
||||
if (isEdit && stocking?.id) {
|
||||
executeUpdate({ ...data, stockingId: String(stocking.id) } as any);
|
||||
} else {
|
||||
executeCreate(data as any);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit((data) => execute(data))}
|
||||
onSubmit={form.handleSubmit((data) => handleSubmit(data))}
|
||||
className="space-y-6"
|
||||
>
|
||||
<Card>
|
||||
@@ -260,7 +309,11 @@ export function CreateStockingForm({
|
||||
disabled={isPending}
|
||||
data-test="stocking-submit-btn"
|
||||
>
|
||||
{isPending ? 'Wird gespeichert...' : 'Besatz eintragen'}
|
||||
{isPending
|
||||
? 'Wird gespeichert...'
|
||||
: isEdit
|
||||
? 'Besatz aktualisieren'
|
||||
: 'Besatz eintragen'}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -20,7 +20,7 @@ import { Input } from '@kit/ui/input';
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
|
||||
import { CreateWaterSchema } from '../schema/fischerei.schema';
|
||||
import { createWater } from '../server/actions/fischerei-actions';
|
||||
import { createWater, updateWater } from '../server/actions/fischerei-actions';
|
||||
|
||||
interface CreateWaterFormProps {
|
||||
accountId: string;
|
||||
@@ -65,22 +65,50 @@ export function CreateWaterForm({
|
||||
},
|
||||
});
|
||||
|
||||
const { execute, isPending } = useAction(createWater, {
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success(isEdit ? 'Gewässer aktualisiert' : 'Gewässer erstellt');
|
||||
router.push(`/home/${account}/fischerei/waters`);
|
||||
}
|
||||
const { execute: executeCreate, isPending: isCreating } = useAction(
|
||||
createWater,
|
||||
{
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Gewässer erstellt');
|
||||
router.push(`/home/${account}/fischerei/waters`);
|
||||
}
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
},
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
);
|
||||
|
||||
const { execute: executeUpdate, isPending: isUpdating } = useAction(
|
||||
updateWater,
|
||||
{
|
||||
onSuccess: ({ data }) => {
|
||||
if (data?.success) {
|
||||
toast.success('Gewässer aktualisiert');
|
||||
router.push(`/home/${account}/fischerei/waters`);
|
||||
}
|
||||
},
|
||||
onError: ({ error }) => {
|
||||
toast.error(error.serverError ?? 'Fehler beim Speichern');
|
||||
},
|
||||
},
|
||||
});
|
||||
);
|
||||
|
||||
const isPending = isCreating || isUpdating;
|
||||
|
||||
const handleSubmit = (data: Record<string, unknown>) => {
|
||||
if (isEdit && water?.id) {
|
||||
executeUpdate({ ...data, waterId: String(water.id) } as any);
|
||||
} else {
|
||||
executeCreate(data as any);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit((data) => execute(data))}
|
||||
onSubmit={form.handleSubmit((data) => handleSubmit(data))}
|
||||
className="space-y-6"
|
||||
>
|
||||
{/* Card 1: Grunddaten */}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
import { Trash2 } from 'lucide-react';
|
||||
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@kit/ui/alert-dialog';
|
||||
import { Button } from '@kit/ui/button';
|
||||
|
||||
interface DeleteConfirmButtonProps {
|
||||
title: string;
|
||||
description: string;
|
||||
isPending?: boolean;
|
||||
onConfirm: () => void;
|
||||
}
|
||||
|
||||
export function DeleteConfirmButton({
|
||||
title,
|
||||
description,
|
||||
isPending,
|
||||
onConfirm,
|
||||
}: DeleteConfirmButtonProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<AlertDialog open={open} onOpenChange={setOpen}>
|
||||
<AlertDialogTrigger
|
||||
render={
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
data-test="delete-btn"
|
||||
disabled={isPending}
|
||||
onClick={(e: React.MouseEvent) => e.stopPropagation()}
|
||||
>
|
||||
<Trash2 className="text-destructive h-4 w-4" />
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>{title}</AlertDialogTitle>
|
||||
<AlertDialogDescription>{description}</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Abbrechen</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
onClick={() => {
|
||||
onConfirm();
|
||||
setOpen(false);
|
||||
}}
|
||||
>
|
||||
{isPending ? 'Wird gelöscht...' : 'Löschen'}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
);
|
||||
}
|
||||
@@ -5,13 +5,17 @@ import { useCallback } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
|
||||
import { Plus } from 'lucide-react';
|
||||
import { Pencil, Plus } from 'lucide-react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
|
||||
import { Badge } from '@kit/ui/badge';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
|
||||
import { Input } from '@kit/ui/input';
|
||||
import { useActionWithToast } from '@kit/ui/use-action-with-toast';
|
||||
|
||||
import { deleteSpecies } from '../server/actions/fischerei-actions';
|
||||
import { DeleteConfirmButton } from './delete-confirm-button';
|
||||
|
||||
interface SpeciesDataTableProps {
|
||||
data: Array<Record<string, unknown>>;
|
||||
@@ -19,6 +23,7 @@ interface SpeciesDataTableProps {
|
||||
page: number;
|
||||
pageSize: number;
|
||||
account: string;
|
||||
accountId: string;
|
||||
}
|
||||
|
||||
export function SpeciesDataTable({
|
||||
@@ -27,10 +32,19 @@ export function SpeciesDataTable({
|
||||
page,
|
||||
pageSize,
|
||||
account,
|
||||
accountId,
|
||||
}: SpeciesDataTableProps) {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const currentSearch = searchParams.get('q') ?? '';
|
||||
|
||||
const { execute: executeDelete, isPending: isDeleting } = useActionWithToast(
|
||||
deleteSpecies,
|
||||
{
|
||||
successMessage: 'Fischart gelöscht',
|
||||
onSuccess: () => router.refresh(),
|
||||
},
|
||||
);
|
||||
const totalPages = Math.max(1, Math.ceil(total / pageSize));
|
||||
|
||||
const form = useForm({
|
||||
@@ -132,6 +146,7 @@ export function SpeciesDataTable({
|
||||
<th className="p-3 text-right font-medium">
|
||||
Max. Fang/Tag
|
||||
</th>
|
||||
<th className="p-3 text-right font-medium">Aktionen</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -162,6 +177,33 @@ export function SpeciesDataTable({
|
||||
? String(species.max_catch_per_day)
|
||||
: '—'}
|
||||
</td>
|
||||
<td className="p-3 text-right">
|
||||
<div className="flex items-center justify-end gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
data-test="species-edit-btn"
|
||||
onClick={() =>
|
||||
router.push(
|
||||
`/home/${account}/fischerei/species/${String(species.id)}/edit`,
|
||||
)
|
||||
}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<DeleteConfirmButton
|
||||
title="Fischart löschen"
|
||||
description="Möchten Sie diese Fischart wirklich löschen? Diese Aktion kann nicht rückgängig gemacht werden."
|
||||
isPending={isDeleting}
|
||||
onConfirm={() =>
|
||||
executeDelete({
|
||||
speciesId: String(species.id),
|
||||
accountId,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
|
||||
@@ -5,7 +5,7 @@ import { useCallback } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
|
||||
import { Plus } from 'lucide-react';
|
||||
import { Pencil, Plus } from 'lucide-react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
|
||||
import { formatDate } from '@kit/shared/dates';
|
||||
@@ -14,8 +14,11 @@ import { Badge } from '@kit/ui/badge';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
|
||||
import { Input } from '@kit/ui/input';
|
||||
import { useActionWithToast } from '@kit/ui/use-action-with-toast';
|
||||
|
||||
import { AGE_CLASS_LABELS } from '../lib/fischerei-constants';
|
||||
import { deleteStocking } from '../server/actions/fischerei-actions';
|
||||
import { DeleteConfirmButton } from './delete-confirm-button';
|
||||
|
||||
interface StockingDataTableProps {
|
||||
data: Array<Record<string, unknown>>;
|
||||
@@ -23,6 +26,7 @@ interface StockingDataTableProps {
|
||||
page: number;
|
||||
pageSize: number;
|
||||
account: string;
|
||||
accountId: string;
|
||||
}
|
||||
|
||||
export function StockingDataTable({
|
||||
@@ -31,11 +35,20 @@ export function StockingDataTable({
|
||||
page,
|
||||
pageSize,
|
||||
account,
|
||||
accountId,
|
||||
}: StockingDataTableProps) {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const totalPages = Math.max(1, Math.ceil(total / pageSize));
|
||||
|
||||
const { execute: executeDelete, isPending: isDeleting } = useActionWithToast(
|
||||
deleteStocking,
|
||||
{
|
||||
successMessage: 'Besatz gelöscht',
|
||||
onSuccess: () => router.refresh(),
|
||||
},
|
||||
);
|
||||
|
||||
const updateParams = useCallback(
|
||||
(updates: Record<string, string>) => {
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
@@ -102,6 +115,7 @@ export function StockingDataTable({
|
||||
<th className="p-3 text-right font-medium">Gewicht (kg)</th>
|
||||
<th className="p-3 text-left font-medium">Altersklasse</th>
|
||||
<th className="p-3 text-right font-medium">Kosten (€)</th>
|
||||
<th className="p-3 text-right font-medium">Aktionen</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -145,6 +159,33 @@ export function StockingDataTable({
|
||||
? formatCurrencyAmount(row.cost_euros as number)
|
||||
: '—'}
|
||||
</td>
|
||||
<td className="p-3 text-right">
|
||||
<div className="flex items-center justify-end gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
data-test="stocking-edit-btn"
|
||||
onClick={() =>
|
||||
router.push(
|
||||
`/home/${account}/fischerei/stocking/${String(row.id)}/edit`,
|
||||
)
|
||||
}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<DeleteConfirmButton
|
||||
title="Besatz löschen"
|
||||
description="Möchten Sie diesen Besatzeintrag wirklich löschen? Diese Aktion kann nicht rückgängig gemacht werden."
|
||||
isPending={isDeleting}
|
||||
onConfirm={() =>
|
||||
executeDelete({
|
||||
stockingId: String(row.id),
|
||||
accountId,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
|
||||
@@ -5,7 +5,7 @@ import { useCallback } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
|
||||
import { Plus } from 'lucide-react';
|
||||
import { Pencil, Plus } from 'lucide-react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
|
||||
import { formatNumber } from '@kit/shared/formatters';
|
||||
@@ -13,8 +13,11 @@ import { Badge } from '@kit/ui/badge';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
|
||||
import { Input } from '@kit/ui/input';
|
||||
import { useActionWithToast } from '@kit/ui/use-action-with-toast';
|
||||
|
||||
import { WATER_TYPE_LABELS } from '../lib/fischerei-constants';
|
||||
import { deleteWater } from '../server/actions/fischerei-actions';
|
||||
import { DeleteConfirmButton } from './delete-confirm-button';
|
||||
|
||||
interface WatersDataTableProps {
|
||||
data: Array<Record<string, unknown>>;
|
||||
@@ -22,6 +25,7 @@ interface WatersDataTableProps {
|
||||
page: number;
|
||||
pageSize: number;
|
||||
account: string;
|
||||
accountId: string;
|
||||
}
|
||||
|
||||
const WATER_TYPE_OPTIONS = [
|
||||
@@ -43,10 +47,19 @@ export function WatersDataTable({
|
||||
page,
|
||||
pageSize,
|
||||
account,
|
||||
accountId,
|
||||
}: WatersDataTableProps) {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const { execute: executeDelete, isPending: isDeleting } = useActionWithToast(
|
||||
deleteWater,
|
||||
{
|
||||
successMessage: 'Gewässer gelöscht',
|
||||
onSuccess: () => router.refresh(),
|
||||
},
|
||||
);
|
||||
|
||||
const currentSearch = searchParams.get('q') ?? '';
|
||||
const currentType = searchParams.get('type') ?? '';
|
||||
const totalPages = Math.max(1, Math.ceil(total / pageSize));
|
||||
@@ -170,6 +183,7 @@ export function WatersDataTable({
|
||||
<th className="p-3 text-left font-medium">Typ</th>
|
||||
<th className="p-3 text-right font-medium">Fläche (ha)</th>
|
||||
<th className="p-3 text-left font-medium">Ort</th>
|
||||
<th className="p-3 text-right font-medium">Aktionen</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -208,6 +222,34 @@ export function WatersDataTable({
|
||||
<td className="text-muted-foreground p-3">
|
||||
{String(water.location ?? '—')}
|
||||
</td>
|
||||
<td className="p-3 text-right">
|
||||
<div className="flex items-center justify-end gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
data-test="water-edit-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
router.push(
|
||||
`/home/${account}/fischerei/waters/${String(water.id)}/edit`,
|
||||
);
|
||||
}}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<DeleteConfirmButton
|
||||
title="Gewässer löschen"
|
||||
description="Möchten Sie dieses Gewässer wirklich löschen? Diese Aktion kann nicht rückgängig gemacht werden."
|
||||
isPending={isDeleting}
|
||||
onConfirm={() =>
|
||||
executeDelete({
|
||||
waterId: String(water.id),
|
||||
accountId,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
|
||||
@@ -473,6 +473,16 @@ export function createFischereiApi(client: SupabaseClient<Database>) {
|
||||
return { data: data ?? [], total: count ?? 0, page, pageSize };
|
||||
},
|
||||
|
||||
async getStocking(stockingId: string) {
|
||||
const { data, error } = await client
|
||||
.from('fish_stocking')
|
||||
.select('*')
|
||||
.eq('id', stockingId)
|
||||
.single();
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
async createStocking(input: CreateStockingInput, userId: string) {
|
||||
const { data, error } = await client
|
||||
.from('fish_stocking')
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
"./hooks/*": "./src/hooks/*.ts",
|
||||
"./components": "./src/components/index.ts",
|
||||
"./actions/*": "./src/server/actions/*.ts",
|
||||
"./types": "./src/types.ts",
|
||||
"./services/*": "./src/server/services/*.ts"
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -33,19 +33,7 @@ export const createRecord = authActionClient
|
||||
}
|
||||
|
||||
// Validate data against field definitions
|
||||
const fields = (
|
||||
moduleWithFields as unknown as {
|
||||
fields: Array<{
|
||||
name: string;
|
||||
field_type: string;
|
||||
is_required: boolean;
|
||||
min_value?: number | null;
|
||||
max_value?: number | null;
|
||||
max_length?: number | null;
|
||||
regex_pattern?: string | null;
|
||||
}>;
|
||||
}
|
||||
).fields;
|
||||
const { fields } = moduleWithFields;
|
||||
const validation = validateRecordData(
|
||||
input.data as Record<string, unknown>,
|
||||
fields as Parameters<typeof validateRecordData>[1],
|
||||
@@ -98,19 +86,7 @@ export const updateRecord = authActionClient
|
||||
throw new Error('Module not found');
|
||||
}
|
||||
|
||||
const fields = (
|
||||
moduleWithFields as unknown as {
|
||||
fields: Array<{
|
||||
name: string;
|
||||
field_type: string;
|
||||
is_required: boolean;
|
||||
min_value?: number | null;
|
||||
max_value?: number | null;
|
||||
max_length?: number | null;
|
||||
regex_pattern?: string | null;
|
||||
}>;
|
||||
}
|
||||
).fields;
|
||||
const { fields } = moduleWithFields;
|
||||
const validation = validateRecordData(
|
||||
input.data as Record<string, unknown>,
|
||||
fields as Parameters<typeof validateRecordData>[1],
|
||||
|
||||
@@ -6,6 +6,7 @@ import type {
|
||||
CreateModuleInput,
|
||||
UpdateModuleInput,
|
||||
} from '../../schema/module.schema';
|
||||
import type { ModuleWithFields } from '../../types';
|
||||
|
||||
/**
|
||||
* Service for managing module definitions (CRUD).
|
||||
@@ -38,15 +39,20 @@ export function createModuleDefinitionService(
|
||||
return data;
|
||||
},
|
||||
|
||||
async getModuleWithFields(moduleId: string) {
|
||||
async getModuleWithFields(
|
||||
moduleId: string,
|
||||
): Promise<ModuleWithFields | null> {
|
||||
const { data, error } = await client
|
||||
.from('modules')
|
||||
.select('*, fields:module_fields(*)')
|
||||
.eq('id', moduleId)
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
if (error) {
|
||||
if (error.code === 'PGRST116') return null;
|
||||
throw error;
|
||||
}
|
||||
return data as unknown as ModuleWithFields;
|
||||
},
|
||||
|
||||
async createModule(input: CreateModuleInput) {
|
||||
|
||||
12
packages/features/module-builder/src/types.ts
Normal file
12
packages/features/module-builder/src/types.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { Tables } from '@kit/supabase/database';
|
||||
|
||||
/** A module row from the database */
|
||||
export type Module = Tables<'modules'>;
|
||||
|
||||
/** A module field row from the database */
|
||||
export type ModuleField = Tables<'module_fields'>;
|
||||
|
||||
/** Module with its field definitions joined */
|
||||
export interface ModuleWithFields extends Module {
|
||||
fields: ModuleField[];
|
||||
}
|
||||
Reference in New Issue
Block a user