112 lines
3.9 KiB
TypeScript
112 lines
3.9 KiB
TypeScript
import { useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { api } from '../../api/client';
|
|
import { Button, ErrorText, Field, Input, Loading, Muted, Textarea } from '../ui';
|
|
import type { AdminAnnouncement, Gym } from '../../api/types';
|
|
|
|
/** Awaryjne ogłoszenia w imieniu siłowni (gdy właściciel nie może sam). */
|
|
export function GymAnnouncementsSection({ gym }: { gym: Gym }) {
|
|
const { t } = useTranslation('gyms');
|
|
const queryClient = useQueryClient();
|
|
const { data, isLoading } = useQuery({
|
|
queryKey: ['admin-announcements', gym.id],
|
|
queryFn: () =>
|
|
api<{ announcements: AdminAnnouncement[] }>(
|
|
`/admin/announcements?gymId=${encodeURIComponent(gym.id)}`,
|
|
),
|
|
});
|
|
const announcements = data?.announcements ?? [];
|
|
const [title, setTitle] = useState('');
|
|
const [body, setBody] = useState('');
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const add = async () => {
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
await api('/admin/announcements', {
|
|
method: 'POST',
|
|
body: { title: title.trim(), body: body.trim(), gymId: gym.id },
|
|
});
|
|
setTitle('');
|
|
setBody('');
|
|
await queryClient.invalidateQueries({ queryKey: ['admin-announcements', gym.id] });
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : t('common:error'));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const remove = async (id: string) => {
|
|
setError(null);
|
|
try {
|
|
await api(`/admin/announcements/${id}`, { method: 'DELETE' });
|
|
await queryClient.invalidateQueries({ queryKey: ['admin-announcements', gym.id] });
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : t('common:error'));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<div className="flex flex-col gap-2.5 rounded-xl border border-line bg-input/40 p-3.5">
|
|
<Muted className="block">{t('announcementSendTo', { name: gym.name })}</Muted>
|
|
<Field label={t('announcementTitleLabel')}>
|
|
<Input
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
maxLength={120}
|
|
placeholder={t('announcementTitlePh')}
|
|
/>
|
|
</Field>
|
|
<Field label={t('announcementBodyLabel')}>
|
|
<Textarea
|
|
value={body}
|
|
onChange={(e) => setBody(e.target.value)}
|
|
maxLength={2000}
|
|
rows={3}
|
|
placeholder={t('announcementBodyPh')}
|
|
/>
|
|
</Field>
|
|
{error && <ErrorText>{error}</ErrorText>}
|
|
<div>
|
|
<Button onClick={() => void add()} disabled={busy || !title.trim() || !body.trim()}>
|
|
{busy ? t('publishing') : t('publish')}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
<div className="mt-4">
|
|
{isLoading ? (
|
|
<Loading />
|
|
) : announcements.length === 0 ? (
|
|
<Muted className="block">{t('noAnnouncements')}</Muted>
|
|
) : (
|
|
<div className="flex flex-col gap-2">
|
|
{announcements.map((a) => (
|
|
<div key={a.id} className="rounded-[10px] border border-line bg-card/50 px-3 py-2.5">
|
|
<div className="flex items-center justify-between gap-3">
|
|
<span className="min-w-0 truncate text-[0.9rem] font-bold">{a.title}</span>
|
|
{a.kind === 'MANUAL' && (
|
|
<Button
|
|
variant="danger"
|
|
size="sm"
|
|
onClick={() => void remove(a.id)}
|
|
aria-label={t('removeAria', { name: a.title })}
|
|
>
|
|
{t('common:remove')}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
<p className="mt-1 whitespace-pre-wrap text-dim text-[0.8125rem]">{a.body}</p>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|