import React, { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { api } from '@/lib/api'; import { SettingsLayout } from './components/SettingsLayout'; import { SettingsCard } from './components/SettingsCard'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { MapPin, Plus, Trash2, RefreshCw, Edit } from 'lucide-react'; import { toast } from 'sonner'; import { __ } from '@/lib/i18n'; interface PickupLocation { id: string; name: string; address: string; city: string; state: string; postcode: string; phone?: string; hours?: string; enabled: boolean; } export default function LocalPickupSettings() { const queryClient = useQueryClient(); const [showDialog, setShowDialog] = useState(false); const [editingLocation, setEditingLocation] = useState(null); // Fetch pickup locations const { data: locations = [], isLoading, refetch } = useQuery({ queryKey: ['pickup-locations'], queryFn: () => api.get('/settings/pickup-locations'), }); // Save location mutation const saveMutation = useMutation({ mutationFn: async (location: Partial) => { if (location.id) { return api.post(`/settings/pickup-locations/${location.id}`, location); } return api.post('/settings/pickup-locations', location); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['pickup-locations'] }); toast.success(__('Pickup location saved')); setShowDialog(false); setEditingLocation(null); }, onError: (error: any) => { toast.error(error?.message || __('Failed to save location')); }, }); // Delete location mutation const deleteMutation = useMutation({ mutationFn: async (id: string) => { return api.del(`/settings/pickup-locations/${id}`); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['pickup-locations'] }); toast.success(__('Pickup location deleted')); }, onError: (error: any) => { toast.error(error?.message || __('Failed to delete location')); }, }); // Toggle location mutation const toggleMutation = useMutation({ mutationFn: async ({ id, enabled }: { id: string; enabled: boolean }) => { return api.post(`/settings/pickup-locations/${id}/toggle`, { enabled }); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['pickup-locations'] }); toast.success(__('Location updated')); }, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const formData = new FormData(e.currentTarget); const location: Partial = { id: editingLocation?.id, name: formData.get('name') as string, address: formData.get('address') as string, city: formData.get('city') as string, state: formData.get('state') as string, postcode: formData.get('postcode') as string, phone: formData.get('phone') as string, hours: formData.get('hours') as string, enabled: true, }; saveMutation.mutate(location); }; if (isLoading) { return (
); } return ( } >
{/* Info Card */}

{__('Add multiple pickup locations where customers can collect their orders. Each location can have its own address, phone number, and business hours.')}

{__('Customers will see available pickup locations during checkout and can choose their preferred location.')}

{/* Pickup Locations List */} {locations.length === 0 ? (

{__('Add your first pickup location to get started')}

) : (
{locations.map((location: PickupLocation) => (

{location.name}

{location.enabled ? __('Active') : __('Inactive')}

{location.address}

{location.city}, {location.state} {location.postcode}

{location.phone &&

📞 {location.phone}

} {location.hours &&

🕐 {location.hours}

}
))}
)}
{/* Add/Edit Location Dialog */} {editingLocation ? __('Edit Pickup Location') : __('Add Pickup Location')}
); }