import { useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { client, errorMessage } from '../../api/client' import type { NotificationChannel, ReminderRule } from '../../api/types' import { useConfirm } from '../../components/Confirm' import { useToasts } from '../../components/Toasts' import { Badge, Button, Card, ErrorState, Input, Select, Spinner } from '../../components/ui' import { formatWithZone } from '../../lib/dates' import { describeOffset, REMINDER_PRESETS as PRESETS } from '../../lib/reminders' export function ReminderSection({ webinarId, timezone }: { webinarId: number; timezone: string }) { const queryClient = useQueryClient() const toasts = useToasts() const confirm = useConfirm() const [offset, setOffset] = useState('1440') const [channel, setChannel] = useState('EMAIL') const rules = useQuery({ queryKey: ['reminders', webinarId], queryFn: async () => { const response = await client.get(`/admin/webinars/${webinarId}/reminders`) return response.data }, }) function invalidate() { queryClient.invalidateQueries({ queryKey: ['reminders', webinarId] }) } const add = useMutation({ mutationFn: () => client.post(`/admin/webinars/${webinarId}/reminders`, { offsetMinutes: Number(offset), channel, template: 'reminder', }), onSuccess: () => { toasts.success('Reguła została dodana') invalidate() }, onError: (error) => toasts.error(errorMessage(error, 'Nie udało się dodać reguły')), }) const toggle = useMutation({ mutationFn: ({ id, value }: { id: number; value: boolean }) => client.post(`/admin/webinars/${webinarId}/reminders/${id}/enabled?value=${value}`), onSuccess: () => invalidate(), onError: (error) => toasts.error(errorMessage(error, 'Nie udało się zmienić reguły')), }) const remove = useMutation({ mutationFn: (id: number) => client.delete(`/admin/webinars/${webinarId}/reminders/${id}`), onSuccess: () => { toasts.success('Reguła została usunięta') invalidate() }, onError: (error) => toasts.error(errorMessage(error, 'Nie udało się usunąć reguły')), }) return ( {rules.isPending && } {rules.isError && ( )} {rules.data && (
{rules.data.length === 0 ? (

Brak reguł. Ten webinar nie wyśle żadnych przypomnień.

) : (
    {rules.data.map((rule) => (
  • {describeOffset(rule.offsetMinutes)} {rule.channel === 'SMS' ? 'SMS' : 'e-mail'}

    {formatWithZone(rule.scheduledAt, timezone)}

    {/* liczniki pokazuja, co juz poszlo z tej reguly */}
    {rule.sentCount > 0 && ( wysłano {rule.sentCount} )} {rule.queuedCount > 0 && ( w kolejce {rule.queuedCount} )} {rule.failedCount > 0 && ( błędy {rule.failedCount} )}
  • ))}
)}
setOffset(e.target.value)} placeholder="minuty" />
{rules.data.some((rule) => rule.channel === 'SMS') && (

SMS trafi wyłącznie do uczestników, którzy podali numer telefonu i wyrazili zgodę marketingową na SMS.

)}
)}
) }