import { useState, useEffect } from 'react' import axios from 'axios' import { Shield, Database, Save, Globe } from 'lucide-react' import { toast } from 'sonner' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { Switch } from '@/components/ui/switch' import { Button } from '@/components/ui/button' const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3001' interface Settings { appName: string appUrl: string supportEmail: string enableRegistration: boolean enableEmailVerification: boolean enablePaymentVerification: boolean maintenanceMode: boolean maintenanceMessage: string } export function AdminSettings() { const [settings, setSettings] = useState({ appName: 'Tabungin', appUrl: 'https://tabungin.app', supportEmail: 'support@tabungin.app', enableRegistration: true, enableEmailVerification: true, enablePaymentVerification: true, maintenanceMode: false, maintenanceMessage: 'System is under maintenance. Please try again later.', }) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) useEffect(() => { fetchSettings() }, []) const fetchSettings = async () => { try { setLoading(true) const token = localStorage.getItem('token') const response = await axios.get(`${API_URL}/api/admin/config/by-category`, { headers: { Authorization: `Bearer ${token}` }, }) // Convert config array to settings object interface ConfigItem { key: string value: string } interface ConfigData { general?: ConfigItem[] features?: ConfigItem[] system?: ConfigItem[] } const configData = response.data as ConfigData const settingsObj: Settings = { appName: configData.general?.find((c) => c.key === 'app_name')?.value || 'Tabungin', appUrl: configData.general?.find((c) => c.key === 'app_url')?.value || 'https://tabungin.app', supportEmail: configData.general?.find((c) => c.key === 'support_email')?.value || 'support@tabungin.app', enableRegistration: configData.features?.find((c) => c.key === 'enable_registration')?.value === 'true', enableEmailVerification: configData.features?.find((c) => c.key === 'enable_email_verification')?.value === 'true', enablePaymentVerification: configData.features?.find((c) => c.key === 'enable_payment_verification')?.value === 'true', maintenanceMode: configData.system?.find((c) => c.key === 'maintenance_mode')?.value === 'true', maintenanceMessage: configData.system?.find((c) => c.key === 'maintenance_message')?.value || 'System is under maintenance. Please try again later.', } setSettings(settingsObj) } catch (error) { console.error('Failed to fetch settings:', error) } finally { setLoading(false) } } const handleSave = async () => { try { setSaving(true) const token = localStorage.getItem('token') // Save each setting individually const configUpdates = [ { key: 'app_name', value: settings.appName, category: 'general', label: 'Application Name', type: 'text' }, { key: 'app_url', value: settings.appUrl, category: 'general', label: 'Application URL', type: 'text' }, { key: 'support_email', value: settings.supportEmail, category: 'general', label: 'Support Email', type: 'email' }, { key: 'enable_registration', value: String(settings.enableRegistration), category: 'features', label: 'New User Registration', type: 'boolean' }, { key: 'enable_email_verification', value: String(settings.enableEmailVerification), category: 'features', label: 'Email Verification', type: 'boolean' }, { key: 'enable_payment_verification', value: String(settings.enablePaymentVerification), category: 'features', label: 'Payment Verification', type: 'boolean' }, { key: 'maintenance_mode', value: String(settings.maintenanceMode), category: 'system', label: 'Maintenance Mode', type: 'boolean' }, { key: 'maintenance_message', value: settings.maintenanceMessage, category: 'system', label: 'Maintenance Message', type: 'text' }, ] await Promise.all( configUpdates.map((config) => axios.post(`${API_URL}/api/admin/config/${config.key}`, config, { headers: { Authorization: `Bearer ${token}` }, }) ) ) toast.success('Settings saved successfully') fetchSettings() // Refresh } catch (error) { console.error('Failed to save settings:', error) toast.error('Failed to save settings') } finally { setSaving(false) } } const handleChange = (field: keyof Settings, value: string | boolean) => { setSettings((prev) => ({ ...prev, [field]: value })) } if (loading) { return (
Loading...
) } return (

Application Settings

Manage system configuration and settings

{/* General Settings */}

General Settings

Basic application information

handleChange('appName', e.target.value)} />
handleChange('appUrl', e.target.value)} />
handleChange('supportEmail', e.target.value)} />
{/* Feature Toggles */}

Features & Security

Enable or disable features

New User Registration

Allow new users to register

handleChange('enableRegistration', checked)} />

Email Verification

Require email verification for new users

handleChange('enableEmailVerification', checked)} />

Manual Payment Verification

Enable manual verification for payments

handleChange('enablePaymentVerification', checked)} />
{/* Maintenance Mode */}

Maintenance Mode

Temporarily disable access for maintenance

Maintenance Mode

Enable to temporarily close access

handleChange('maintenanceMode', checked)} className="data-[state=checked]:bg-destructive" />
{settings.maintenanceMode && (