314 lines
11 KiB
TypeScript
314 lines
11 KiB
TypeScript
import React from 'react';
|
|
import { useNavigate, useParams } from 'react-router-dom';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { CalendarDays, ChevronDown, MapPin } from 'lucide-react';
|
|
import { YStack, XStack } from '@tamagui/stacks';
|
|
import { SizableText as Text } from '@tamagui/text';
|
|
import { Switch } from '@tamagui/switch';
|
|
import { MobileShell } from './components/MobileShell';
|
|
import { MobileCard, CTAButton } from './components/Primitives';
|
|
import { createEvent, getEvent, updateEvent, getEventTypes, TenantEvent, TenantEventType } from '../api';
|
|
import { adminPath } from '../constants';
|
|
import { isAuthError } from '../auth/tokens';
|
|
import { getApiErrorMessage } from '../lib/apiError';
|
|
|
|
type FormState = {
|
|
name: string;
|
|
date: string;
|
|
eventTypeId: number | null;
|
|
description: string;
|
|
location: string;
|
|
enableBranding: boolean;
|
|
published: boolean;
|
|
};
|
|
|
|
export default function MobileEventFormPage() {
|
|
const { slug: slugParam } = useParams<{ slug?: string }>();
|
|
const slug = slugParam ?? null;
|
|
const isEdit = Boolean(slug);
|
|
const navigate = useNavigate();
|
|
const { t } = useTranslation('management');
|
|
|
|
const [form, setForm] = React.useState<FormState>({
|
|
name: '',
|
|
date: '',
|
|
eventTypeId: null,
|
|
description: '',
|
|
location: '',
|
|
enableBranding: false,
|
|
published: false,
|
|
});
|
|
const [eventTypes, setEventTypes] = React.useState<TenantEventType[]>([]);
|
|
const [typesLoading, setTypesLoading] = React.useState(false);
|
|
const [loading, setLoading] = React.useState(isEdit);
|
|
const [saving, setSaving] = React.useState(false);
|
|
const [error, setError] = React.useState<string | null>(null);
|
|
|
|
React.useEffect(() => {
|
|
if (!slug) return;
|
|
(async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await getEvent(slug);
|
|
setForm({
|
|
name: renderName(data.name),
|
|
date: toDateTimeLocal(data.event_date),
|
|
eventTypeId: data.event_type_id ?? data.event_type?.id ?? null,
|
|
description: typeof data.description === 'string' ? data.description : '',
|
|
location: resolveLocation(data),
|
|
enableBranding: Boolean((data.settings as Record<string, unknown>)?.branding_allowed ?? true),
|
|
published: data.status === 'published',
|
|
});
|
|
setError(null);
|
|
} catch (err) {
|
|
if (!isAuthError(err)) {
|
|
setError(getApiErrorMessage(err, t('events.errors.loadFailed', 'Event konnte nicht geladen werden.')));
|
|
}
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
})();
|
|
}, [slug, t, isEdit]);
|
|
|
|
React.useEffect(() => {
|
|
(async () => {
|
|
setTypesLoading(true);
|
|
try {
|
|
const types = await getEventTypes();
|
|
setEventTypes(types);
|
|
// Default to first type if none set
|
|
if (!form.eventTypeId && types.length) {
|
|
setForm((prev) => ({ ...prev, eventTypeId: types[0].id }));
|
|
}
|
|
} catch {
|
|
// silently ignore; fallback to null
|
|
} finally {
|
|
setTypesLoading(false);
|
|
}
|
|
})();
|
|
}, []);
|
|
|
|
async function handleSubmit() {
|
|
setSaving(true);
|
|
setError(null);
|
|
try {
|
|
if (isEdit && slug) {
|
|
await updateEvent(slug, {
|
|
name: form.name,
|
|
event_date: form.date || undefined,
|
|
event_type_id: form.eventTypeId ?? undefined,
|
|
status: form.published ? 'published' : 'draft',
|
|
settings: { branding_allowed: form.enableBranding, location: form.location },
|
|
});
|
|
navigate(adminPath(`/mobile/events/${slug}`));
|
|
} else {
|
|
const payload = {
|
|
name: form.name || 'Event',
|
|
slug: `${Date.now()}`,
|
|
event_type_id: form.eventTypeId ?? undefined,
|
|
event_date: form.date || undefined,
|
|
status: form.published ? 'published' : 'draft' as const,
|
|
settings: { branding_allowed: form.enableBranding, location: form.location },
|
|
};
|
|
const { event } = await createEvent(payload as any);
|
|
navigate(adminPath(`/mobile/events/${event.slug}`));
|
|
}
|
|
} catch (err) {
|
|
if (!isAuthError(err)) {
|
|
setError(getApiErrorMessage(err, t('events.errors.saveFailed', 'Event konnte nicht gespeichert werden.')));
|
|
}
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<MobileShell
|
|
activeTab="home"
|
|
title={isEdit ? t('events.form.editTitle', 'Edit Event') : t('events.form.createTitle', 'Create New Event')}
|
|
onBack={() => navigate(-1)}
|
|
>
|
|
{error ? (
|
|
<MobileCard>
|
|
<Text fontWeight="700" color="#b91c1c">
|
|
{error}
|
|
</Text>
|
|
</MobileCard>
|
|
) : null}
|
|
|
|
<MobileCard space="$3">
|
|
<Field label={t('events.form.name', 'Event Name')}>
|
|
<input
|
|
type="text"
|
|
value={form.name}
|
|
onChange={(e) => setForm((prev) => ({ ...prev, name: e.target.value }))}
|
|
placeholder="e.g., Smith Wedding"
|
|
style={inputStyle}
|
|
/>
|
|
</Field>
|
|
|
|
<Field label={t('events.form.date', 'Date & Time')}>
|
|
<XStack alignItems="center" space="$2">
|
|
<input
|
|
type="datetime-local"
|
|
value={form.date}
|
|
onChange={(e) => setForm((prev) => ({ ...prev, date: e.target.value }))}
|
|
style={{ ...inputStyle, flex: 1 }}
|
|
/>
|
|
<CalendarDays size={16} color="#9ca3af" />
|
|
</XStack>
|
|
</Field>
|
|
|
|
<Field label={t('eventForm.fields.type.label', 'Event type')}>
|
|
{typesLoading ? (
|
|
<Text fontSize="$sm" color="#6b7280">{t('eventForm.fields.type.loading', 'Loading event types…')}</Text>
|
|
) : eventTypes.length === 0 ? (
|
|
<Text fontSize="$sm" color="#6b7280">{t('eventForm.fields.type.empty', 'No event types available yet. Please add one in the admin area.')}</Text>
|
|
) : (
|
|
<select
|
|
value={form.eventTypeId ?? ''}
|
|
onChange={(e) => setForm((prev) => ({ ...prev, eventTypeId: Number(e.target.value) }))}
|
|
style={{ ...inputStyle, height: 44 }}
|
|
>
|
|
<option value="">{t('eventForm.fields.type.placeholder', 'Select event type')}</option>
|
|
{eventTypes.map((type) => (
|
|
<option key={type.id} value={type.id}>
|
|
{renderName(type.name as any) || type.slug}
|
|
</option>
|
|
))}
|
|
</select>
|
|
)}
|
|
</Field>
|
|
|
|
<Field label={t('events.form.description', 'Optional Details')}>
|
|
<textarea
|
|
value={form.description}
|
|
onChange={(e) => setForm((prev) => ({ ...prev, description: e.target.value }))}
|
|
placeholder={t('events.form.descriptionPlaceholder', 'Description')}
|
|
style={{ ...inputStyle, minHeight: 96 }}
|
|
/>
|
|
</Field>
|
|
|
|
<Field label={t('events.form.location', 'Location')}>
|
|
<XStack alignItems="center" space="$2">
|
|
<input
|
|
type="text"
|
|
value={form.location}
|
|
onChange={(e) => setForm((prev) => ({ ...prev, location: e.target.value }))}
|
|
placeholder={t('events.form.locationPlaceholder', 'Location')}
|
|
style={{ ...inputStyle, flex: 1 }}
|
|
/>
|
|
<MapPin size={16} color="#9ca3af" />
|
|
</XStack>
|
|
</Field>
|
|
|
|
<Field label={t('events.form.enableBranding', 'Enable Branding & Moderation')}>
|
|
<XStack alignItems="center" space="$2">
|
|
<Switch
|
|
checked={form.enableBranding}
|
|
onCheckedChange={(checked) =>
|
|
setForm((prev) => ({ ...prev, enableBranding: Boolean(checked) }))
|
|
}
|
|
size="$3"
|
|
aria-label={t('events.form.enableBranding', 'Enable Branding & Moderation')}
|
|
>
|
|
<Switch.Thumb />
|
|
</Switch>
|
|
<Text fontSize="$sm" color="#111827">
|
|
{form.enableBranding ? t('common.enabled', 'Enabled') : t('common.disabled', 'Disabled')}
|
|
</Text>
|
|
</XStack>
|
|
</Field>
|
|
|
|
<Field label={t('eventForm.fields.publish.label', 'Publish immediately')}>
|
|
<XStack alignItems="center" space="$2">
|
|
<Switch
|
|
checked={form.published}
|
|
onCheckedChange={(checked) =>
|
|
setForm((prev) => ({ ...prev, published: Boolean(checked) }))
|
|
}
|
|
size="$3"
|
|
aria-label={t('eventForm.fields.publish.label', 'Publish immediately')}
|
|
>
|
|
<Switch.Thumb />
|
|
</Switch>
|
|
<Text fontSize="$sm" color="#111827">
|
|
{form.published ? t('common.enabled', 'Enabled') : t('common.disabled', 'Disabled')}
|
|
</Text>
|
|
</XStack>
|
|
<Text fontSize="$xs" color="#6b7280">{t('eventForm.fields.publish.help', 'Enable if guests should see the event right away. You can change the status later.')}</Text>
|
|
</Field>
|
|
</MobileCard>
|
|
|
|
<YStack space="$2">
|
|
{!isEdit ? (
|
|
<button
|
|
type="button"
|
|
onClick={() => navigate(-1)}
|
|
style={{
|
|
...inputStyle,
|
|
height: 48,
|
|
borderRadius: 12,
|
|
border: '1px solid #e5e7eb',
|
|
background: '#f1f5f9',
|
|
fontWeight: 700,
|
|
}}
|
|
>
|
|
{t('events.form.saveDraft', 'Save as Draft')}
|
|
</button>
|
|
) : null}
|
|
<CTAButton label={saving ? t('events.form.saving', 'Saving...') : isEdit ? t('events.form.update', 'Update Event') : t('events.form.create', 'Create Event')} onPress={() => handleSubmit()} />
|
|
</YStack>
|
|
</MobileShell>
|
|
);
|
|
}
|
|
|
|
function Field({ label, children }: { label: string; children: React.ReactNode }) {
|
|
return (
|
|
<YStack space="$1.5">
|
|
<Text fontSize="$sm" fontWeight="800" color="#111827">
|
|
{label}
|
|
</Text>
|
|
{children}
|
|
</YStack>
|
|
);
|
|
}
|
|
|
|
const inputStyle: React.CSSProperties = {
|
|
width: '100%',
|
|
height: 44,
|
|
borderRadius: 10,
|
|
border: '1px solid #e5e7eb',
|
|
padding: '0 12px',
|
|
fontSize: 14,
|
|
background: 'white',
|
|
};
|
|
|
|
function renderName(name: TenantEvent['name']): string {
|
|
if (typeof name === 'string') return name;
|
|
if (name && typeof name === 'object') {
|
|
return name.de ?? name.en ?? Object.values(name)[0] ?? '';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function toDateTimeLocal(value?: string | null): string {
|
|
if (!value) return '';
|
|
const parsed = new Date(value);
|
|
if (!Number.isNaN(parsed.getTime())) {
|
|
return parsed.toISOString().slice(0, 16);
|
|
}
|
|
const fallback = value.replace(' ', 'T');
|
|
return fallback.length >= 16 ? fallback.slice(0, 16) : '';
|
|
}
|
|
|
|
function resolveLocation(event: TenantEvent): string {
|
|
const settings = (event.settings ?? {}) as Record<string, unknown>;
|
|
const candidate =
|
|
(settings.location as string | undefined) ??
|
|
(settings.address as string | undefined) ??
|
|
(settings.city as string | undefined);
|
|
if (candidate && candidate.trim()) return candidate;
|
|
return '';
|
|
}
|