diff --git a/frontend/src/features/offerings/components/OfferingBannerDialog.tsx b/frontend/src/features/offerings/components/OfferingBannerDialog.tsx new file mode 100644 index 0000000..34a94a6 --- /dev/null +++ b/frontend/src/features/offerings/components/OfferingBannerDialog.tsx @@ -0,0 +1,190 @@ +import { useState } from 'react'; +import { useForm } from 'react-hook-form'; +import { zodResolver } from '@hookform/resolvers/zod'; +import { useTranslation } from 'react-i18next'; +import { toast } from 'sonner'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { FieldError } from '@/components/ui/FieldError'; +import { FormErrorBanner } from '@/components/ui/FormErrorBanner'; +import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; +import { NetworkError } from '@/lib/api-client'; +import { + fromLocalInput, + offeringBannerFormSchema, + toLocalInput, + type OfferingBannerFormData, +} from '../schemas/offeringBanner'; +import { useSaveOfferingBanner } from '../services/useSaveOfferingBanner'; +import type { OfferingBannerDto } from '../services/types'; + +interface OfferingBannerDialogProps { + /** Open for editing this banner; null with `creating` = a new one. */ + banner: OfferingBannerDto | null; + creating: boolean; + onClose: () => void; +} + +const EMPTY: OfferingBannerFormData = { + title: '', + text: '', + linkLabel: '', + linkUrl: '', + isActive: true, + startsAt: '', + endsAt: '', +}; + +function toForm(banner: OfferingBannerDto | null): OfferingBannerFormData { + if (!banner) return EMPTY; + return { + title: banner.title, + text: banner.text, + linkLabel: banner.linkLabel ?? '', + linkUrl: banner.linkUrl ?? '', + isActive: banner.isActive, + startsAt: toLocalInput(banner.startsAt), + endsAt: toLocalInput(banner.endsAt), + }; +} + +export function OfferingBannerDialog({ banner, creating, onClose }: OfferingBannerDialogProps) { + const { t } = useTranslation(); + const open = creating || banner !== null; + + return ( + { if (!next) onClose(); }}> + + + + {banner ? t('offerings.banners.editTitle') : t('offerings.banners.createTitle')} + + + {/* Content is unmounted while closed, so every opening starts from fresh form state. */} + + + + ); +} + +function OfferingBannerForm({ banner, onClose }: { banner: OfferingBannerDto | null; onClose: () => void }) { + const { t } = useTranslation(); + const [serverError, setServerError] = useState(null); + const save = useSaveOfferingBanner(); + + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ + resolver: zodResolver(offeringBannerFormSchema), + mode: 'onTouched', + defaultValues: toForm(banner), + }); + + const onSubmit = async (values: OfferingBannerFormData) => { + setServerError(null); + try { + await save.mutateAsync({ + id: banner?.id, + body: { + title: values.title, + text: values.text, + linkLabel: values.linkLabel || null, + linkUrl: values.linkUrl || null, + isActive: values.isActive, + startsAt: fromLocalInput(values.startsAt), + endsAt: fromLocalInput(values.endsAt), + }, + }); + toast.success(t('offerings.banners.savedToast')); + onClose(); + } catch (err) { + setServerError(err instanceof NetworkError ? t('errors.network') : t('errors.generic')); + } + }; + + return ( + <> + setServerError(null)} + /> + +
+
+ + + +
+ +
+ +