640 lines
20 KiB
TypeScript
640 lines
20 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import { useRouter } from 'next/navigation';
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from '@kit/ui/alert-dialog';
|
|
import { Button } from '@kit/ui/button';
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
} from '@kit/ui/form';
|
|
import { Input } from '@kit/ui/input';
|
|
import { Textarea } from '@kit/ui/textarea';
|
|
import { useActionWithToast } from '@kit/ui/use-action-with-toast';
|
|
|
|
import { CreateMemberSchema } from '../schema/member.schema';
|
|
import { createMember } from '../server/actions/member-actions';
|
|
|
|
interface Props {
|
|
accountId: string;
|
|
account: string; // slug for redirect
|
|
duesCategories: Array<{ id: string; name: string; amount: number }>;
|
|
}
|
|
|
|
interface DuplicateEntry {
|
|
field: string;
|
|
message: string;
|
|
id?: string;
|
|
}
|
|
|
|
export function CreateMemberForm({
|
|
accountId,
|
|
account,
|
|
duesCategories,
|
|
}: Props) {
|
|
const router = useRouter();
|
|
const [duplicates, setDuplicates] = useState<DuplicateEntry[]>([]);
|
|
const form = useForm({
|
|
resolver: zodResolver(CreateMemberSchema),
|
|
defaultValues: {
|
|
accountId,
|
|
firstName: '',
|
|
lastName: '',
|
|
email: '',
|
|
phone: '',
|
|
mobile: '',
|
|
street: '',
|
|
houseNumber: '',
|
|
postalCode: '',
|
|
city: '',
|
|
country: 'DE',
|
|
memberNumber: '',
|
|
status: 'active' as const,
|
|
entryDate: new Date().toISOString().split('T')[0]!,
|
|
iban: '',
|
|
bic: '',
|
|
accountHolder: '',
|
|
gdprConsent: false,
|
|
notes: '',
|
|
},
|
|
});
|
|
|
|
const { execute, isPending } = useActionWithToast(createMember, {
|
|
successMessage: 'Mitglied erfolgreich erstellt',
|
|
errorMessage: 'Fehler beim Erstellen',
|
|
onSuccess: () => {
|
|
router.push(`/home/${account}/members-cms`);
|
|
},
|
|
onError: (_error, data) => {
|
|
if (data?.validationErrors) {
|
|
setDuplicates(data.validationErrors as DuplicateEntry[]);
|
|
}
|
|
},
|
|
});
|
|
|
|
return (
|
|
<Form {...form}>
|
|
<form
|
|
onSubmit={form.handleSubmit((data) => execute(data))}
|
|
className="space-y-6"
|
|
>
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Persönliche Daten</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
|
<FormField
|
|
control={form.control}
|
|
name="firstName"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Vorname *</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="lastName"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Nachname *</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="dateOfBirth"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Geburtsdatum</FormLabel>
|
|
<FormControl>
|
|
<Input type="date" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="gender"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Geschlecht</FormLabel>
|
|
<FormControl>
|
|
<select
|
|
{...field}
|
|
className="border-input bg-background flex h-10 w-full rounded-md border px-3 py-2 text-sm"
|
|
>
|
|
<option value="">— Bitte wählen —</option>
|
|
<option value="male">Männlich</option>
|
|
<option value="female">Weiblich</option>
|
|
<option value="diverse">Divers</option>
|
|
</select>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Kontakt</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="grid grid-cols-1 gap-4 sm:grid-cols-3">
|
|
<FormField
|
|
control={form.control}
|
|
name="email"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>E-Mail</FormLabel>
|
|
<FormControl>
|
|
<Input type="email" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="phone"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Telefon</FormLabel>
|
|
<FormControl>
|
|
<Input type="tel" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="mobile"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Mobil</FormLabel>
|
|
<FormControl>
|
|
<Input type="tel" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Adresse</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
|
<FormField
|
|
control={form.control}
|
|
name="street"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Straße</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="houseNumber"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Hausnummer</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="postalCode"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>PLZ</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="city"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Ort</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Mitgliedschaft</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="grid grid-cols-1 gap-4 sm:grid-cols-3">
|
|
<FormField
|
|
control={form.control}
|
|
name="memberNumber"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Mitgliedsnr.</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="status"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Status</FormLabel>
|
|
<FormControl>
|
|
<select
|
|
{...field}
|
|
className="border-input bg-background flex h-10 w-full rounded-md border px-3 py-2 text-sm"
|
|
>
|
|
<option value="active">Aktiv</option>
|
|
<option value="inactive">Inaktiv</option>
|
|
<option value="pending">Ausstehend</option>
|
|
</select>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="entryDate"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Eintrittsdatum</FormLabel>
|
|
<FormControl>
|
|
<Input type="date" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{duesCategories.length > 0 && (
|
|
<FormField
|
|
control={form.control}
|
|
name="duesCategoryId"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Beitragskategorie</FormLabel>
|
|
<FormControl>
|
|
<select
|
|
{...field}
|
|
className="border-input bg-background flex h-10 w-full rounded-md border px-3 py-2 text-sm"
|
|
>
|
|
<option value="">— Keine —</option>
|
|
{duesCategories.map((c) => (
|
|
<option key={c.id} value={c.id}>
|
|
{c.name} ({c.amount} €)
|
|
</option>
|
|
))}
|
|
</select>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>SEPA-Bankdaten</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="grid grid-cols-1 gap-4 sm:grid-cols-3">
|
|
<FormField
|
|
control={form.control}
|
|
name="iban"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>IBAN</FormLabel>
|
|
<FormControl>
|
|
<Input
|
|
placeholder="DE89 3704 0044 0532 0130 00"
|
|
{...field}
|
|
onChange={(e) =>
|
|
field.onChange(
|
|
e.target.value
|
|
.toUpperCase()
|
|
.replace(/[^A-Z0-9]/g, ''),
|
|
)
|
|
}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="bic"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>BIC</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="accountHolder"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Kontoinhaber</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Guardian (Gap 4) */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Erziehungsberechtigte (Jugend)</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="grid grid-cols-1 gap-4 sm:grid-cols-3">
|
|
<FormField
|
|
control={form.control}
|
|
name="guardianName"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Name Erziehungsberechtigte/r</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="guardianPhone"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Telefon</FormLabel>
|
|
<FormControl>
|
|
<Input type="tel" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="guardianEmail"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>E-Mail</FormLabel>
|
|
<FormControl>
|
|
<Input type="email" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Lifecycle flags (Gap 4) */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Mitgliedschaftsmerkmale</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="grid grid-cols-2 gap-4 sm:grid-cols-4">
|
|
{(
|
|
[
|
|
['isHonorary', 'Ehrenmitglied'],
|
|
['isFoundingMember', 'Gründungsmitglied'],
|
|
['isYouth', 'Jugendmitglied'],
|
|
['isRetiree', 'Rentner/Senior'],
|
|
['isProbationary', 'Probejahr'],
|
|
] as const
|
|
).map(([name, label]) => (
|
|
<FormField
|
|
key={name}
|
|
control={form.control}
|
|
name={name}
|
|
render={({ field }) => (
|
|
<FormItem className="flex items-center gap-2">
|
|
<FormControl>
|
|
<input
|
|
type="checkbox"
|
|
checked={field.value as boolean}
|
|
onChange={field.onChange}
|
|
className="border-input h-4 w-4 rounded"
|
|
/>
|
|
</FormControl>
|
|
<FormLabel className="!mt-0">{label}</FormLabel>
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
))}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* GDPR granular (Gap 4) */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Datenschutz-Einwilligungen</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="grid grid-cols-2 gap-4 sm:grid-cols-4">
|
|
{(
|
|
[
|
|
['gdprConsent', 'Allgemeine Einwilligung'],
|
|
['gdprNewsletter', 'Newsletter'],
|
|
['gdprInternet', 'Internet/Homepage'],
|
|
['gdprPrint', 'Vereinszeitung'],
|
|
['gdprBirthdayInfo', 'Geburtstagsinfo'],
|
|
] as const
|
|
).map(([name, label]) => (
|
|
<FormField
|
|
key={name}
|
|
control={form.control}
|
|
name={name}
|
|
render={({ field }) => (
|
|
<FormItem className="flex items-center gap-2">
|
|
<FormControl>
|
|
<input
|
|
type="checkbox"
|
|
checked={field.value as boolean}
|
|
onChange={field.onChange}
|
|
className="border-input h-4 w-4 rounded"
|
|
/>
|
|
</FormControl>
|
|
<FormLabel className="!mt-0">{label}</FormLabel>
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
))}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Sonstiges</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="space-y-4">
|
|
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
|
|
<FormField
|
|
control={form.control}
|
|
name="salutation"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Anrede</FormLabel>
|
|
<FormControl>
|
|
<select
|
|
{...field}
|
|
className="border-input bg-background flex h-10 w-full rounded-md border px-3 py-2 text-sm"
|
|
>
|
|
<option value="">— Keine —</option>
|
|
<option value="Herr">Herr</option>
|
|
<option value="Frau">Frau</option>
|
|
</select>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="birthplace"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Geburtsort</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="street2"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Adresszusatz</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</div>
|
|
<FormField
|
|
control={form.control}
|
|
name="notes"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Notizen</FormLabel>
|
|
<FormControl>
|
|
<Textarea {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</CardContent>
|
|
</Card>
|
|
<div className="flex justify-end gap-2">
|
|
<Button type="button" variant="outline" onClick={() => router.back()}>
|
|
Abbrechen
|
|
</Button>
|
|
<Button type="submit" disabled={isPending}>
|
|
{isPending ? 'Wird erstellt...' : 'Mitglied erstellen'}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
|
|
<AlertDialog
|
|
open={duplicates.length > 0}
|
|
onOpenChange={() => setDuplicates([])}
|
|
>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Mögliches Duplikat gefunden</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Es wurden ähnliche Mitglieder gefunden:
|
|
<ul className="mt-2 list-inside list-disc">
|
|
{duplicates.map((d, i) => (
|
|
<li key={d.id ?? i}>{d.message}</li>
|
|
))}
|
|
</ul>
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Abbrechen</AlertDialogCancel>
|
|
<AlertDialogAction
|
|
onClick={() => {
|
|
router.push(
|
|
`/home/${account}/members-cms/${duplicates[0]?.id}`,
|
|
);
|
|
}}
|
|
>
|
|
Zum bestehenden Mitglied
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</Form>
|
|
);
|
|
}
|