Files
biblical-guide.com/app/[locale]/prayers/page.tsx
andupetcu 6492601355 Implement comprehensive prayer writing and AI generation interface
- Add authentication-gated prayer creation with dual-mode interface
- Implement tabbed dialog: "Write Prayer" and "AI Generate" options
- Create AI prayer generation API endpoint with bilingual support (EN/RO)
- Add category-specific prayer templates with user prompt integration
- Include proper authentication checks and user-based prayer attribution
- Enhance UX with loading states, success feedback, and guided prompts
- Fix missing searchTypes translations in English locale for search functionality
- Restrict prayer creation to logged-in users with appropriate visual feedback
- Support both manual composition and AI-assisted prayer creation workflows

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-21 22:12:26 +03:00

583 lines
20 KiB
TypeScript

'use client'
import {
Container,
Grid,
Card,
CardContent,
Typography,
Box,
TextField,
Button,
Paper,
Avatar,
Chip,
IconButton,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Fab,
List,
ListItem,
ListItemAvatar,
ListItemText,
MenuItem,
useTheme,
CircularProgress,
Skeleton,
Alert,
Tabs,
Tab,
FormControlLabel,
Switch,
} from '@mui/material'
import {
Favorite,
Add,
Close,
Person,
AccessTime,
FavoriteBorder,
Share,
MoreVert,
AutoAwesome,
Edit,
Login,
} from '@mui/icons-material'
import { useState, useEffect } from 'react'
import { useTranslations, useLocale, useFormatter } from 'next-intl'
import { useAuth } from '@/hooks/use-auth'
interface PrayerRequest {
id: string
title: string
description: string
category: string
author: string
timestamp: Date
prayerCount: number
isPrayedFor: boolean
}
export default function PrayersPage() {
const theme = useTheme()
const locale = useLocale()
const t = useTranslations('pages.prayers')
const tc = useTranslations('common')
const f = useFormatter()
const { user } = useAuth()
const [prayers, setPrayers] = useState<PrayerRequest[]>([])
const [openDialog, setOpenDialog] = useState(false)
const [tabValue, setTabValue] = useState(0) // 0 = Write, 1 = AI Generate
const [newPrayer, setNewPrayer] = useState({
title: '',
description: '',
category: 'personal',
})
const [aiPrompt, setAiPrompt] = useState('')
const [isGenerating, setIsGenerating] = useState(false)
const [loading, setLoading] = useState(true)
const categories = [
{ value: 'personal', label: t('categories.personal'), color: 'primary' },
{ value: 'family', label: t('categories.family'), color: 'secondary' },
{ value: 'health', label: t('categories.health'), color: 'error' },
{ value: 'work', label: t('categories.work'), color: 'warning' },
{ value: 'ministry', label: t('categories.ministry'), color: 'success' },
{ value: 'world', label: t('categories.world'), color: 'info' },
]
// Sample data - in real app this would come from API
useEffect(() => {
// Simulate loading prayers
setTimeout(() => {
setPrayers([
{
id: '1',
title: t('samples.item1.title'),
description: t('samples.item1.description'),
category: 'health',
author: t('samples.item1.author'),
timestamp: new Date(Date.now() - 2 * 60 * 60 * 1000),
prayerCount: 23,
isPrayedFor: false,
},
{
id: '2',
title: t('samples.item2.title'),
description: t('samples.item2.description'),
category: 'work',
author: t('samples.item2.author'),
timestamp: new Date(Date.now() - 5 * 60 * 60 * 1000),
prayerCount: 15,
isPrayedFor: true,
},
{
id: '3',
title: t('samples.item3.title'),
description: t('samples.item3.description'),
category: 'family',
author: t('samples.item3.author'),
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000),
prayerCount: 41,
isPrayedFor: false,
},
])
setLoading(false)
}, 1000)
}, [locale])
const handleGenerateAIPrayer = async () => {
if (!aiPrompt.trim()) return
if (!user) return
setIsGenerating(true)
try {
const response = await fetch('/api/prayers/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('authToken')}`
},
body: JSON.stringify({
prompt: aiPrompt,
category: newPrayer.category,
locale
}),
})
if (response.ok) {
const data = await response.json()
setNewPrayer({
title: data.title || '',
description: data.prayer || '',
category: newPrayer.category
})
setTabValue(0) // Switch to write tab to review generated prayer
} else {
console.error('Failed to generate prayer')
}
} catch (error) {
console.error('Error generating prayer:', error)
} finally {
setIsGenerating(false)
}
}
const handleSubmitPrayer = async () => {
if (!newPrayer.title.trim() || !newPrayer.description.trim()) return
if (!user) return
const prayer: PrayerRequest = {
id: Date.now().toString(),
title: newPrayer.title,
description: newPrayer.description,
category: newPrayer.category,
author: user.name || (locale === 'en' ? 'You' : 'Tu'),
timestamp: new Date(),
prayerCount: 0,
isPrayedFor: false,
}
setPrayers([prayer, ...prayers])
setNewPrayer({ title: '', description: '', category: 'personal' })
setAiPrompt('')
setTabValue(0)
setOpenDialog(false)
// In real app, send to API
try {
await fetch('/api/prayers', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('authToken')}`
},
body: JSON.stringify(prayer),
})
} catch (error) {
console.error('Error submitting prayer:', error)
}
}
const handleOpenDialog = () => {
if (!user) {
// Could redirect to login or show login modal
return
}
setOpenDialog(true)
}
const handlePrayFor = async (prayerId: string) => {
setPrayers(prayers.map(prayer =>
prayer.id === prayerId
? { ...prayer, prayerCount: prayer.prayerCount + 1, isPrayedFor: true }
: prayer
))
// In real app, send to API
try {
await fetch(`/api/prayers/${prayerId}/pray`, { method: 'POST' })
} catch (error) {
console.error('Error updating prayer count:', error)
}
}
const getCategoryInfo = (category: string) => {
return categories.find(cat => cat.value === category) || categories[0]
}
const formatTimestamp = (timestamp: Date) => {
const now = new Date()
const diff = now.getTime() - timestamp.getTime()
const minutes = Math.floor(diff / (1000 * 60))
const hours = Math.floor(minutes / 60)
const days = Math.floor(hours / 24)
if (days > 0) return f.relativeTime(-days, 'day', { now })
if (hours > 0) return f.relativeTime(-hours, 'hour', { now })
if (minutes > 0) return f.relativeTime(-minutes, 'minute', { now })
return f.relativeTime(0, 'second', { now })
}
return (
<Box>
<Container maxWidth="lg" sx={{ py: 4 }}>
{/* Header */}
<Box sx={{ mb: 4, textAlign: 'center' }}>
<Typography variant="h3" component="h1" gutterBottom>
<Favorite sx={{ fontSize: 40, mr: 2, verticalAlign: 'middle', color: 'error.main' }} />
{t('title')}
</Typography>
<Typography variant="body1" color="text.secondary">
{t('subtitle')}
</Typography>
</Box>
<Grid container spacing={4}>
{/* Categories Filter */}
<Grid item xs={12} md={3}>
<Card>
<CardContent>
<Typography variant="h6" gutterBottom>
{t('categories.title')}
</Typography>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
{categories.map((category) => (
<Chip
key={category.value}
label={category.label}
color={category.color as any}
variant="outlined"
size="small"
sx={{ justifyContent: 'flex-start' }}
/>
))}
</Box>
<Typography variant="h6" sx={{ mt: 3, mb: 1 }}>
{t('stats.title')}
</Typography>
<Typography variant="body2" color="text.secondary">
{t('stats.activeRequests', { count: prayers.length })}<br />
{t('stats.totalPrayers', { count: prayers.reduce((sum, p) => sum + p.prayerCount, 0) })}<br />
{t('stats.youPrayed', { count: prayers.filter(p => p.isPrayedFor).length })}
</Typography>
</CardContent>
</Card>
</Grid>
{/* Prayer Requests */}
<Grid item xs={12} md={9}>
{loading ? (
<Box>
{Array.from({ length: 3 }).map((_, index) => (
<Card key={index} sx={{ mb: 3 }}>
<CardContent>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 2 }}>
<Box sx={{ flexGrow: 1 }}>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 1 }}>
<Skeleton variant="text" width="60%" height={32} />
<Skeleton variant="rounded" width={80} height={24} />
</Box>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 2, mb: 2 }}>
<Skeleton variant="circular" width={24} height={24} />
<Skeleton variant="text" width="30%" height={20} />
</Box>
<Skeleton variant="text" width="100%" height={24} />
<Skeleton variant="text" width="90%" height={24} />
<Skeleton variant="text" width="95%" height={24} />
</Box>
</Box>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<Box sx={{ display: 'flex', gap: 1 }}>
<Skeleton variant="rounded" width={100} height={32} />
<Skeleton variant="rounded" width={100} height={32} />
</Box>
<Skeleton variant="text" width="20%" height={20} />
</Box>
</CardContent>
</Card>
))}
</Box>
) : (
<Box>
{prayers.map((prayer) => {
const categoryInfo = getCategoryInfo(prayer.category)
return (
<Card key={prayer.id} sx={{ mb: 3 }}>
<CardContent>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 2 }}>
<Box sx={{ flexGrow: 1 }}>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 1 }}>
<Typography variant="h6" component="h3">
{prayer.title}
</Typography>
<Chip
label={categoryInfo.label}
color={categoryInfo.color as any}
size="small"
variant="outlined"
/>
</Box>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 2, mb: 2 }}>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<Avatar sx={{ width: 24, height: 24, bgcolor: 'primary.main' }}>
<Person sx={{ fontSize: 16 }} />
</Avatar>
<Typography variant="body2" color="text.secondary">
{prayer.author}
</Typography>
</Box>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
<AccessTime sx={{ fontSize: 16, color: 'text.secondary' }} />
<Typography variant="body2" color="text.secondary">
{formatTimestamp(prayer.timestamp)}
</Typography>
</Box>
</Box>
<Typography variant="body1" sx={{ mb: 2 }}>
{prayer.description}
</Typography>
</Box>
<IconButton size="small">
<MoreVert />
</IconButton>
</Box>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<Box sx={{ display: 'flex', gap: 1 }}>
<Button
variant={prayer.isPrayedFor ? "contained" : "outlined"}
color="primary"
size="small"
startIcon={prayer.isPrayedFor ? <Favorite /> : <FavoriteBorder />}
onClick={() => handlePrayFor(prayer.id)}
disabled={prayer.isPrayedFor}
>
{prayer.isPrayedFor ? t('buttons.prayed') : t('buttons.pray')}
</Button>
<Button
variant="outlined"
size="small"
startIcon={<Share />}
>
{t('buttons.share')}
</Button>
</Box>
<Typography variant="body2" color="text.secondary">
{t('stats.totalPrayers', { count: prayer.prayerCount })}
</Typography>
</Box>
</CardContent>
</Card>
)
})}
</Box>
)}
</Grid>
</Grid>
{/* Add Prayer FAB */}
{user ? (
<Fab
color="primary"
aria-label="add prayer"
sx={{ position: 'fixed', bottom: 24, right: 24 }}
onClick={handleOpenDialog}
>
<Add />
</Fab>
) : (
<Fab
color="primary"
aria-label="login to add prayer"
sx={{ position: 'fixed', bottom: 24, right: 24 }}
onClick={() => {
// Could redirect to login page or show login modal
console.log('Please login to add prayers')
}}
>
<Login />
</Fab>
)}
{/* Add Prayer Dialog */}
<Dialog
open={openDialog}
onClose={() => setOpenDialog(false)}
maxWidth="md"
fullWidth
>
<DialogTitle>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
{t('dialog.title')}
<IconButton onClick={() => setOpenDialog(false)} size="small">
<Close />
</IconButton>
</Box>
</DialogTitle>
{/* Tabs for Write vs AI Generate */}
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
<Tabs value={tabValue} onChange={(e, newValue) => setTabValue(newValue)} centered>
<Tab
icon={<Edit />}
label={locale === 'en' ? 'Write Prayer' : 'Scrie rugăciune'}
iconPosition="start"
/>
<Tab
icon={<AutoAwesome />}
label={locale === 'en' ? 'AI Generate' : 'Generează cu AI'}
iconPosition="start"
/>
</Tabs>
</Box>
<DialogContent sx={{ minHeight: 400 }}>
{/* Write Prayer Tab */}
{tabValue === 0 && (
<Box>
<TextField
fullWidth
label={t('dialog.titleLabel')}
value={newPrayer.title}
onChange={(e) => setNewPrayer({ ...newPrayer, title: e.target.value })}
sx={{ mb: 2, mt: 1 }}
/>
<TextField
fullWidth
label={t('dialog.categoryLabel')}
select
value={newPrayer.category}
onChange={(e) => setNewPrayer({ ...newPrayer, category: e.target.value })}
sx={{ mb: 2 }}
>
{categories.map((option) => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</TextField>
<TextField
fullWidth
label={t('dialog.descriptionLabel')}
multiline
rows={6}
value={newPrayer.description}
onChange={(e) => setNewPrayer({ ...newPrayer, description: e.target.value })}
placeholder={t('dialog.placeholder')}
/>
</Box>
)}
{/* AI Generate Prayer Tab */}
{tabValue === 1 && (
<Box>
<Alert severity="info" sx={{ mb: 2 }}>
{locale === 'en'
? 'Describe what you\'d like to pray about, and AI will help you create a meaningful prayer.'
: 'Descrie pentru ce ai vrea să te rogi, iar AI-ul te va ajuta să creezi o rugăciune semnificativă.'
}
</Alert>
<TextField
fullWidth
label={t('dialog.categoryLabel')}
select
value={newPrayer.category}
onChange={(e) => setNewPrayer({ ...newPrayer, category: e.target.value })}
sx={{ mb: 2 }}
>
{categories.map((option) => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</TextField>
<TextField
fullWidth
label={locale === 'en' ? 'What would you like to pray about?' : 'Pentru ce ai vrea să te rogi?'}
multiline
rows={4}
value={aiPrompt}
onChange={(e) => setAiPrompt(e.target.value)}
placeholder={locale === 'en'
? 'e.g., "Help me find peace during a difficult time at work" or "Guidance for my family\'s health struggles"'
: 'ex. "Ajută-mă să găsesc pace într-o perioadă dificilă la muncă" sau "Îndrumarea pentru problemele de sănătate ale familiei mele"'
}
sx={{ mb: 2 }}
/>
<Button
fullWidth
variant="contained"
onClick={handleGenerateAIPrayer}
disabled={!aiPrompt.trim() || isGenerating}
startIcon={isGenerating ? <CircularProgress size={20} /> : <AutoAwesome />}
sx={{ mb: 2 }}
>
{isGenerating
? (locale === 'en' ? 'Generating...' : 'Se generează...')
: (locale === 'en' ? 'Generate Prayer with AI' : 'Generează rugăciune cu AI')
}
</Button>
{newPrayer.title && newPrayer.description && (
<Alert severity="success" sx={{ mt: 2 }}>
{locale === 'en'
? 'Prayer generated! Switch to the "Write Prayer" tab to review and edit before submitting.'
: 'Rugăciune generată! Comută la tabul "Scrie rugăciune" pentru a revizui și edita înainte de a trimite.'
}
</Alert>
)}
</Box>
)}
</DialogContent>
<DialogActions>
<Button onClick={() => setOpenDialog(false)}>
{t('dialog.cancel')}
</Button>
<Button
onClick={handleSubmitPrayer}
variant="contained"
disabled={!newPrayer.title.trim() || !newPrayer.description.trim()}
>
{t('dialog.submit')}
</Button>
</DialogActions>
</Dialog>
</Container>
</Box>
)
}