import React, { useState, useEffect } from 'react'; import { SettingsLayout } from '@/routes/Settings/components/SettingsLayout'; import { SettingsCard } from '@/routes/Settings/components/SettingsCard'; import { SettingsSection } from '@/routes/Settings/components/SettingsSection'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Switch } from '@/components/ui/switch'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Plus, X } from 'lucide-react'; import { toast } from 'sonner'; import { api } from '@/lib/api'; import { useModules } from '@/hooks/useModules'; interface SocialLink { id: string; platform: string; url: string; } interface FooterSection { id: string; title: string; type: 'menu' | 'contact' | 'social' | 'newsletter' | 'custom'; content: any; visible: boolean; } interface ContactData { email: string; phone: string; address: string; show_email: boolean; show_phone: boolean; show_address: boolean; } export default function AppearanceFooter() { const { isEnabled, isLoading: modulesLoading } = useModules(); const [loading, setLoading] = useState(true); const [columns, setColumns] = useState('4'); const [style, setStyle] = useState('detailed'); const [copyrightText, setCopyrightText] = useState('© 2024 WooNooW. All rights reserved.'); const [elements, setElements] = useState({ newsletter: true, social: true, payment: true, copyright: true, menu: true, contact: true, }); const [socialLinks, setSocialLinks] = useState([]); const [sections, setSections] = useState([]); const [contactData, setContactData] = useState({ email: '', phone: '', address: '', show_email: true, show_phone: true, show_address: true, }); const defaultSections: FooterSection[] = [ { id: '1', title: 'Contact', type: 'contact', content: '', visible: true }, { id: '2', title: 'Quick Links', type: 'menu', content: '', visible: true }, { id: '3', title: 'Follow Us', type: 'social', content: '', visible: true }, { id: '4', title: 'Newsletter', type: 'newsletter', content: '', visible: true }, ]; const [labels, setLabels] = useState({ contact_title: 'Contact', menu_title: 'Quick Links', social_title: 'Follow Us', newsletter_title: 'Newsletter', newsletter_description: 'Subscribe to get updates', }); useEffect(() => { const loadSettings = async () => { try { const response = await api.get('/appearance/settings'); const footer = response.data?.footer; if (footer) { if (footer.columns) setColumns(footer.columns); if (footer.style) setStyle(footer.style); if (footer.copyright_text) setCopyrightText(footer.copyright_text); if (footer.elements) setElements(footer.elements); if (footer.social_links) setSocialLinks(footer.social_links); if (footer.sections && footer.sections.length > 0) { setSections(footer.sections); } else { setSections(defaultSections); } if (footer.contact_data) setContactData(footer.contact_data); if (footer.labels) setLabels(footer.labels); } else { setSections(defaultSections); } // Fetch store identity data try { const identityResponse = await api.get('/settings/store-identity'); const identity = identityResponse.data; if (identity && !footer?.contact_data) { setContactData(prev => ({ ...prev, email: identity.email || prev.email, phone: identity.phone || prev.phone, address: identity.address || prev.address, })); } } catch (err) { console.log('Store identity not available'); } } catch (error) { console.error('Failed to load settings:', error); } finally { setLoading(false); } }; loadSettings(); }, []); const toggleElement = (key: keyof typeof elements) => { setElements({ ...elements, [key]: !elements[key] }); }; const addSocialLink = () => { setSocialLinks([ ...socialLinks, { id: Date.now().toString(), platform: '', url: '' }, ]); }; const removeSocialLink = (id: string) => { setSocialLinks(socialLinks.filter(link => link.id !== id)); }; const updateSocialLink = (id: string, field: 'platform' | 'url', value: string) => { setSocialLinks(socialLinks.map(link => link.id === id ? { ...link, [field]: value } : link )); }; const addSection = () => { setSections([ ...sections, { id: Date.now().toString(), title: 'New Section', type: 'custom', content: '', visible: true, }, ]); }; const removeSection = (id: string) => { setSections(sections.filter(s => s.id !== id)); }; const updateSection = (id: string, field: keyof FooterSection, value: any) => { setSections(sections.map(s => s.id === id ? { ...s, [field]: value } : s)); }; const handleSave = async () => { try { const payload = { columns, style, copyrightText, elements, socialLinks, sections, contactData, labels, }; const response = await api.post('/appearance/footer', payload); toast.success('Footer settings saved successfully'); } catch (error) { console.error('Save error:', error); toast.error('Failed to save settings'); } }; return ( {/* Layout */} {/* Labels */} setLabels({ ...labels, contact_title: e.target.value })} placeholder="Contact" /> setLabels({ ...labels, menu_title: e.target.value })} placeholder="Quick Links" /> setLabels({ ...labels, social_title: e.target.value })} placeholder="Follow Us" /> setLabels({ ...labels, newsletter_title: e.target.value })} placeholder="Newsletter" /> setLabels({ ...labels, newsletter_description: e.target.value })} placeholder="Subscribe to get updates" /> {/* Contact Data */} setContactData({ ...contactData, email: e.target.value })} placeholder="info@store.com" />
setContactData({ ...contactData, show_email: checked })} />
setContactData({ ...contactData, phone: e.target.value })} placeholder="(123) 456-7890" />
setContactData({ ...contactData, show_phone: checked })} />