'use client'; import { useState, useEffect } from 'react'; import { Box, Typography, Button, Card, CardContent, TextField, Select, MenuItem, FormControl, InputLabel, Dialog, DialogTitle, DialogContent, DialogActions, Switch, FormControlLabel, IconButton, Chip, Alert, Breadcrumbs, Link } from '@mui/material'; import { Add as AddIcon, Edit as EditIcon, Delete as DeleteIcon, Home as HomeIcon, Share as ShareIcon, Facebook, Twitter, Instagram, YouTube, LinkedIn, GitHub, MusicNote as TikTok } from '@mui/icons-material'; import { DataGrid, GridColDef, GridActionsCellItem } from '@mui/x-data-grid'; interface SocialMediaLink { id: string; platform: string; name: string; url: string; icon: string; isEnabled: boolean; order: number; createdAt: string; updatedAt: string; creator: { name: string; email: string }; updater: { name: string; email: string }; } const platformIcons = { 'Facebook': Facebook, 'Twitter': Twitter, 'Instagram': Instagram, 'YouTube': YouTube, 'LinkedIn': LinkedIn, 'GitHub': GitHub, 'TikTok': TikTok }; const platformOptions = [ { value: 'facebook', label: 'Facebook', icon: 'Facebook' }, { value: 'twitter', label: 'Twitter', icon: 'Twitter' }, { value: 'instagram', label: 'Instagram', icon: 'Instagram' }, { value: 'youtube', label: 'YouTube', icon: 'YouTube' }, { value: 'linkedin', label: 'LinkedIn', icon: 'LinkedIn' }, { value: 'github', label: 'GitHub', icon: 'GitHub' }, { value: 'tiktok', label: 'TikTok', icon: 'TikTok' } ]; export default function SocialMediaManagement() { const [socialLinks, setSocialLinks] = useState([]); const [loading, setLoading] = useState(true); const [selectedLink, setSelectedLink] = useState(null); const [editorOpen, setEditorOpen] = useState(false); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [linkToDelete, setLinkToDelete] = useState(null); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [formData, setFormData] = useState({ platform: '', name: '', url: '', icon: '', isEnabled: true, order: 0 }); useEffect(() => { fetchSocialLinks(); }, []); const fetchSocialLinks = async () => { try { const response = await fetch('/api/admin/social-media', { credentials: 'include' }); if (!response.ok) { throw new Error('Failed to fetch social media links'); } const data = await response.json(); setSocialLinks(data.data || []); } catch (error) { console.error('Error fetching social media links:', error); setError('Failed to load social media links'); } finally { setLoading(false); } }; const handleCreateLink = () => { setSelectedLink(null); setFormData({ platform: '', name: '', url: '', icon: '', isEnabled: true, order: socialLinks.length }); setEditorOpen(true); }; const handleEditLink = (link: SocialMediaLink) => { setSelectedLink(link); setFormData({ platform: link.platform, name: link.name, url: link.url, icon: link.icon, isEnabled: link.isEnabled, order: link.order }); setEditorOpen(true); }; const handleDeleteLink = (link: SocialMediaLink) => { setLinkToDelete(link); setDeleteDialogOpen(true); }; const handleSaveLink = async () => { try { const url = selectedLink ? `/api/admin/social-media/${selectedLink.id}` : '/api/admin/social-media'; const method = selectedLink ? 'PUT' : 'POST'; const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(formData) }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || 'Failed to save social media link'); } setSuccess('Social media link saved successfully'); setEditorOpen(false); fetchSocialLinks(); } catch (error) { console.error('Error saving social media link:', error); setError(error instanceof Error ? error.message : 'Failed to save social media link'); } }; const confirmDeleteLink = async () => { if (!linkToDelete) return; try { const response = await fetch(`/api/admin/social-media/${linkToDelete.id}`, { method: 'DELETE', credentials: 'include' }); if (!response.ok) { throw new Error('Failed to delete social media link'); } setSuccess('Social media link deleted successfully'); setDeleteDialogOpen(false); setLinkToDelete(null); fetchSocialLinks(); } catch (error) { console.error('Error deleting social media link:', error); setError('Failed to delete social media link'); } }; const handlePlatformChange = (platform: string) => { const platformOption = platformOptions.find(p => p.value === platform); if (platformOption) { setFormData(prev => ({ ...prev, platform, name: platformOption.label, icon: platformOption.icon })); } }; const renderIcon = (iconName: string) => { const IconComponent = platformIcons[iconName as keyof typeof platformIcons]; return IconComponent ? : ; }; const columns: GridColDef[] = [ { field: 'icon', headerName: 'Icon', width: 80, renderCell: (params) => renderIcon(params.value) }, { field: 'name', headerName: 'Platform', width: 120, renderCell: (params) => ( {params.value} {params.row.platform} ) }, { field: 'url', headerName: 'URL', width: 300, renderCell: (params) => ( {params.value} ) }, { field: 'isEnabled', headerName: 'Status', width: 100, renderCell: (params) => ( ) }, { field: 'order', headerName: 'Order', width: 80 }, { field: 'updatedAt', headerName: 'Last Updated', width: 150, renderCell: (params) => new Date(params.value).toLocaleDateString() }, { field: 'actions', type: 'actions', headerName: 'Actions', width: 120, getActions: (params) => [ } label="Edit" onClick={() => handleEditLink(params.row)} />, } label="Delete" onClick={() => handleDeleteLink(params.row)} /> ] } ]; return ( {/* Breadcrumbs */} Admin Social Media {/* Header */} Social Media Management Manage social media links displayed in the footer {/* Alerts */} {error && ( setError(null)} sx={{ mb: 2 }}> {error} )} {success && ( setSuccess(null)} sx={{ mb: 2 }}> {success} )} {/* Social Links Table */} {/* Editor Dialog */} setEditorOpen(false)} maxWidth="sm" fullWidth> {selectedLink ? 'Edit Social Media Link' : 'Add Social Media Link'} Platform setFormData(prev => ({ ...prev, name: e.target.value }))} /> setFormData(prev => ({ ...prev, url: e.target.value }))} placeholder="https://..." /> setFormData(prev => ({ ...prev, order: parseInt(e.target.value) || 0 }))} /> setFormData(prev => ({ ...prev, isEnabled: e.target.checked }))} /> } label="Enabled" /> {/* Delete Confirmation Dialog */} setDeleteDialogOpen(false)}> Delete Social Media Link Are you sure you want to delete the {linkToDelete?.name} link? This action cannot be undone. ); }