Skip to content

Commit c1ceb0c

Browse files
Add toggle to Family Prayer page
1 parent 49a3750 commit c1ceb0c

1 file changed

Lines changed: 142 additions & 6 deletions

File tree

src/pages/FamilyPrayerDetail.tsx

Lines changed: 142 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
import { useEffect, useState, useCallback } from 'react';
22
import { useParams, Link } from 'react-router-dom';
33
import { useTranslation } from 'react-i18next';
4+
import ReactMarkdown from 'react-markdown';
45
import { supabase } from '@/integrations/supabase/client';
56
import { Button } from '@/components/ui/button';
6-
import { ArrowLeft } from 'lucide-react';
7+
import { Card, CardContent } from '@/components/ui/card';
8+
import { Switch } from '@/components/ui/switch';
9+
import { Label } from '@/components/ui/label';
10+
import { ArrowLeft, Calendar } from 'lucide-react';
711
import WorldKidsNewsSlides from '@/components/WorldKidsNewsSlides';
812
import SocialShareDropdown from '@/components/SocialShareDropdown';
913

1014
interface FamilyPrayer {
1115
id: string;
1216
week_date: string;
1317
image_urls: string[];
18+
prayer_id?: string;
1419
world_kids_news_translations?: {
1520
title?: string;
1621
content?: string;
1722
language: string;
1823
}[];
24+
prayers?: {
25+
id: string;
26+
week_date: string;
27+
image_url?: string;
28+
prayer_translations: {
29+
title: string;
30+
content: string;
31+
}[];
32+
};
1933
}
2034

2135
const FamilyPrayerDetail = () => {
2236
const { date, id } = useParams<{ date?: string; id?: string }>();
2337
const { t, i18n } = useTranslation();
2438
const [familyPrayer, setFamilyPrayer] = useState<FamilyPrayer | null>(null);
2539
const [loading, setLoading] = useState(true);
40+
const [showFamilyPrayer, setShowFamilyPrayer] = useState(true);
2641

2742
const formatUrlDateToDbDate = (urlDate: string) => {
2843
if (urlDate && urlDate.length === 8) {
@@ -40,7 +55,14 @@ const FamilyPrayerDetail = () => {
4055
id,
4156
week_date,
4257
image_urls,
43-
world_kids_news_translations!inner(title, content, language)
58+
prayer_id,
59+
world_kids_news_translations!inner(title, content, language),
60+
prayers(
61+
id,
62+
week_date,
63+
image_url,
64+
prayer_translations!inner(title, content, language)
65+
)
4466
`
4567
)
4668
.eq('world_kids_news_translations.language', i18n.language);
@@ -79,10 +101,28 @@ const FamilyPrayerDetail = () => {
79101
}
80102
}, [date, id, fetchFamilyPrayer]);
81103

104+
const formatDate = (dateString: string) => {
105+
const date = new Date(dateString);
106+
return date.toLocaleDateString(
107+
i18n.language === 'zh-TW' ? 'zh-TW' : 'en-US',
108+
{
109+
year: 'numeric',
110+
month: 'long',
111+
day: 'numeric',
112+
}
113+
);
114+
};
115+
82116
const formatDateForUrl = (dateString: string) => {
83117
return dateString.replace(/-/g, '');
84118
};
85119

120+
// Check if corresponding prayer exists
121+
const hasCorrespondingPrayer = Boolean(
122+
familyPrayer?.prayers?.prayer_translations &&
123+
familyPrayer.prayers.prayer_translations.length > 0
124+
);
125+
86126
if (loading) {
87127
return (
88128
<div className="min-h-screen bg-gradient-to-br from-background via-background to-accent/10 flex items-center justify-center">
@@ -128,10 +168,106 @@ const FamilyPrayerDetail = () => {
128168
<SocialShareDropdown url={currentUrl} title={safeTitle} />
129169
</div>
130170

131-
{/* Family Prayer Content */}
132-
<div className="max-w-4xl mx-auto">
133-
<WorldKidsNewsSlides worldKidsNews={familyPrayer} />
134-
</div>
171+
{/* Family Prayer Content */}
172+
<div className="max-w-4xl mx-auto">
173+
{/* Toggle for Prayer - Only show if corresponding prayer exists */}
174+
{hasCorrespondingPrayer && (
175+
<div className="mb-6 flex items-center justify-center space-x-4">
176+
<Label htmlFor="prayer-toggle" className="text-sm font-medium">
177+
{showFamilyPrayer ? t('prayer.switchToPrayer') : t('prayer.switchToFamilyPrayer')}
178+
</Label>
179+
<Switch
180+
id="prayer-toggle"
181+
checked={showFamilyPrayer}
182+
onCheckedChange={setShowFamilyPrayer}
183+
/>
184+
</div>
185+
)}
186+
187+
{showFamilyPrayer ? (
188+
/* Family Prayer View */
189+
<WorldKidsNewsSlides worldKidsNews={familyPrayer} />
190+
) : (
191+
/* Prayer View */
192+
<>
193+
{familyPrayer.prayers?.image_url && (
194+
<div className="mb-8 overflow-hidden rounded-lg shadow-prayer">
195+
<img
196+
src={familyPrayer.prayers.image_url}
197+
alt={familyPrayer.prayers.prayer_translations[0]?.title}
198+
className="w-full h-auto object-contain md:max-h-[400px]"
199+
/>
200+
</div>
201+
)}
202+
203+
<Card className="bg-gradient-to-br from-card to-card/80 border-border shadow-prayer">
204+
<CardContent className="p-8">
205+
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
206+
<div className="flex items-center text-sm text-muted-foreground">
207+
<Calendar className="h-4 w-4 mr-2" />
208+
{formatDate(familyPrayer.prayers?.week_date || familyPrayer.week_date)}
209+
</div>
210+
211+
<SocialShareDropdown
212+
url={currentUrl}
213+
title={familyPrayer.prayers?.prayer_translations[0]?.title || ''}
214+
/>
215+
</div>
216+
217+
{/* Title */}
218+
<h1 className="text-3xl md:text-4xl font-bold text-foreground mb-8 leading-tight">
219+
{familyPrayer.prayers?.prayer_translations[0]?.title}
220+
</h1>
221+
222+
{/* Content */}
223+
<div className="prose prose-lg max-w-none text-foreground">
224+
<ReactMarkdown
225+
components={{
226+
h1: ({ children }) => (
227+
<h1 className="text-2xl font-bold text-foreground mt-8 mb-4">
228+
{children}
229+
</h1>
230+
),
231+
h2: ({ children }) => (
232+
<h2 className="text-xl font-semibold text-foreground mt-6 mb-3">
233+
{children}
234+
</h2>
235+
),
236+
h3: ({ children }) => (
237+
<h3 className="text-lg font-medium text-foreground mt-4 mb-2">
238+
{children}
239+
</h3>
240+
),
241+
p: ({ children }) => (
242+
<p className="text-foreground mb-4 leading-relaxed">
243+
{children}
244+
</p>
245+
),
246+
blockquote: ({ children }) => (
247+
<blockquote className="border-l-4 border-primary pl-4 my-4 italic text-muted-foreground">
248+
{children}
249+
</blockquote>
250+
),
251+
ul: ({ children }) => (
252+
<ul className="list-disc list-inside mb-4 text-foreground space-y-1">
253+
{children}
254+
</ul>
255+
),
256+
ol: ({ children }) => (
257+
<ol className="list-decimal list-inside mb-4 text-foreground space-y-1">
258+
{children}
259+
</ol>
260+
),
261+
}}
262+
>
263+
{familyPrayer.prayers?.prayer_translations[0]?.content || ''}
264+
</ReactMarkdown>
265+
</div>
266+
</CardContent>
267+
</Card>
268+
</>
269+
)}
270+
</div>
135271
</div>
136272
</div>
137273
);

0 commit comments

Comments
 (0)