import React, { useState } from 'react'; import { IonPage, IonContent, IonHeader, IonToolbar, IonButtons, IonButton, IonIcon, IonSpinner, useIonViewWillEnter, } from '@ionic/react'; import { useHistory, useParams } from 'react-router-dom'; import { supabase } from '../supabase'; import { useAuth } from '../contexts/AuthContext'; import { NewsItem } from '../types'; import { chevronBackOutline, createOutline } from 'ionicons/icons'; const NewsDetail: React.FC = () => { const history = useHistory(); const { id } = useParams<{ id: string }>(); const { profile } = useAuth(); const [news, setNews] = useState(null); const [loading, setLoading] = useState(true); useIonViewWillEnter(() => { fetchNewsItem(); }); const fetchNewsItem = async () => { try { const { data, error } = await supabase .from('news') .select('*') .eq('id', id) .single(); if (error) throw error; setNews(data); } catch (err) { console.error('Error fetching news:', err); } finally { setLoading(false); } }; if (loading) { return ( history.goBack()}>
); } if (!news) { return ( history.goBack()}>
News item not found.
); } return ( history.goBack()}> {profile?.role === 'admin' && ( history.push(`/admin/news/${news.id}`)}> )}
{news.image_url && (
)}
{new Date(news.created_at).toLocaleDateString(undefined, { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric', })}

{news.title}

{news.content}
); }; export default NewsDetail;