import React, { useEffect, useState } from 'react'; import api from '../../utils/api'; import { Card } from '../../components/ui/card'; import { Button } from '../../components/ui/button'; import { Badge } from '../../components/ui/badge'; import { Input } from '../../components/ui/input'; import { Label } from '../../components/ui/label'; import { Checkbox } from '../../components/ui/checkbox'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '../../components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '../../components/ui/select'; import { toast } from 'sonner'; import { Scale, Plus, Edit, Trash2, ExternalLink, Check } from 'lucide-react'; const AdminBylaws = () => { const [bylaws, setBylaws] = useState([]); const [loading, setLoading] = useState(true); const [dialogOpen, setDialogOpen] = useState(false); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [selectedBylaws, setSelectedBylaws] = useState(null); const [bylawsToDelete, setBylawsToDelete] = useState(null); const [uploadedFile, setUploadedFile] = useState(null); const [formData, setFormData] = useState({ title: '', version: '', effective_date: '', document_url: '', document_type: 'link', is_current: false }); const [submitting, setSubmitting] = useState(false); useEffect(() => { fetchBylaws(); }, []); const fetchBylaws = async () => { try { const response = await api.get('/bylaws/history'); setBylaws(response.data); } catch (error) { toast.error('Failed to fetch bylaws'); } finally { setLoading(false); } }; const handleCreate = () => { setSelectedBylaws(null); setFormData({ title: 'LOAF Bylaws', version: '', effective_date: new Date().toISOString().split('T')[0], document_url: '', document_type: 'link', is_current: bylaws.length === 0 // Auto-check if this is the first bylaws }); setUploadedFile(null); setDialogOpen(true); }; const handleEdit = (bylawsDoc) => { setSelectedBylaws(bylawsDoc); setFormData({ title: bylawsDoc.title, version: bylawsDoc.version, effective_date: new Date(bylawsDoc.effective_date).toISOString().split('T')[0], document_url: bylawsDoc.document_url, document_type: bylawsDoc.document_type, is_current: bylawsDoc.is_current }); setDialogOpen(true); }; const handleDelete = (bylawsDoc) => { setBylawsToDelete(bylawsDoc); setDeleteDialogOpen(true); }; const handleSubmit = async (e) => { e.preventDefault(); setSubmitting(true); try { const formDataToSend = new FormData(); formDataToSend.append('title', formData.title); formDataToSend.append('version', formData.version); formDataToSend.append('effective_date', new Date(formData.effective_date).toISOString()); formDataToSend.append('document_type', formData.document_type); formDataToSend.append('is_current', formData.is_current); // Handle file upload or URL based on document_type if (formData.document_type === 'upload') { if (!uploadedFile && !selectedBylaws) { toast.error('Please select a file to upload'); setSubmitting(false); return; } if (uploadedFile) { formDataToSend.append('file', uploadedFile); } } else { formDataToSend.append('document_url', formData.document_url); } if (selectedBylaws) { await api.put(`/admin/bylaws/${selectedBylaws.id}`, formDataToSend); toast.success('Bylaws updated successfully'); } else { await api.post('/admin/bylaws', formDataToSend); toast.success('Bylaws created successfully'); } setDialogOpen(false); setUploadedFile(null); fetchBylaws(); } catch (error) { toast.error(error.response?.data?.detail || 'Failed to save bylaws'); } finally { setSubmitting(false); } }; const confirmDelete = async () => { try { await api.delete(`/admin/bylaws/${bylawsToDelete.id}`); toast.success('Bylaws deleted successfully'); setDeleteDialogOpen(false); fetchBylaws(); } catch (error) { toast.error('Failed to delete bylaws'); } }; const formatDate = (dateString) => { return new Date(dateString).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); }; const currentBylaws = bylaws.find(b => b.is_current); const historicalBylaws = bylaws.filter(b => !b.is_current).sort((a, b) => new Date(b.effective_date) - new Date(a.effective_date) ); if (loading) { return (

Loading bylaws...

); } return (
{/* Header */}

Bylaws Management

Manage LOAF governing bylaws and version history

{/* Current Bylaws */} {currentBylaws ? (

{currentBylaws.title}

Current Version Version {currentBylaws.version}
Effective Date: {formatDate(currentBylaws.effective_date)} Document Type: {currentBylaws.document_type === 'upload' ? 'PDF Upload' : 'Link'}
) : (

No current bylaws set

)} {/* Historical Versions */} {historicalBylaws.length > 0 && (

Version History ({historicalBylaws.length})

{historicalBylaws.map(bylawsDoc => (

{bylawsDoc.title}

Version {bylawsDoc.version} Effective {formatDate(bylawsDoc.effective_date)}
))}
)} {/* Create/Edit Dialog */} {selectedBylaws ? 'Edit Bylaws' : 'Add Bylaws Version'} {selectedBylaws ? 'Update bylaws information' : 'Add a new version of the bylaws'}
setFormData({ ...formData, title: e.target.value })} placeholder="LOAF Bylaws" required />
setFormData({ ...formData, version: e.target.value })} placeholder="v1.0" required />
setFormData({ ...formData, effective_date: e.target.value })} required />
{formData.document_type === 'upload' ? (
setUploadedFile(e.target.files[0])} required={!selectedBylaws} /> {uploadedFile && (

Selected: {uploadedFile.name}

)} {selectedBylaws && !uploadedFile && (

Current file will be kept if no new file is selected

)}
) : (
setFormData({ ...formData, document_url: e.target.value })} placeholder="https://docs.google.com/... or https://example.com/file.pdf" required />

Paste the shareable link to your document (Google Drive, Dropbox, PDF URL, etc.)

)}
setFormData({ ...formData, is_current: checked })} />
{/* Delete Confirmation Dialog */} Delete Bylaws Are you sure you want to delete "{bylawsToDelete?.title} ({bylawsToDelete?.version})"? This action cannot be undone.
); }; export default AdminBylaws;