diff --git a/admin-spa/src/routes/Settings/Shipping.tsx b/admin-spa/src/routes/Settings/Shipping.tsx index fbb8194..4c7d045 100644 --- a/admin-spa/src/routes/Settings/Shipping.tsx +++ b/admin-spa/src/routes/Settings/Shipping.tsx @@ -7,7 +7,6 @@ import { ToggleField } from './components/ToggleField'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Drawer, DrawerContent, DrawerHeader, DrawerTitle } from '@/components/ui/drawer'; -import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Globe, Truck, MapPin, Edit, Trash2, RefreshCw, Loader2, ExternalLink, Settings, Plus, X } from 'lucide-react'; import { toast } from 'sonner'; import { __ } from '@/lib/i18n'; @@ -34,8 +33,9 @@ export default function ShippingPage() { const [togglingMethod, setTogglingMethod] = useState(null); const [selectedZone, setSelectedZone] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); - const [activeTab, setActiveTab] = useState('methods'); const [showAddMethod, setShowAddMethod] = useState(false); + const [editingMethod, setEditingMethod] = useState(null); + const [showEditDialog, setShowEditDialog] = useState(false); const isDesktop = useMediaQuery("(min-width: 768px)"); // Fetch shipping zones from WooCommerce @@ -87,14 +87,38 @@ export default function ShippingPage() { // Delete shipping method mutation const deleteMethodMutation = useMutation({ mutationFn: async ({ zoneId, instanceId }: { zoneId: number; instanceId: number }) => { - return api.delete(`/settings/shipping/zones/${zoneId}/methods/${instanceId}`); + return api.del(`/settings/shipping/zones/${zoneId}/methods/${instanceId}`); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['shipping-zones'] }); - toast.success(__('Shipping method deleted successfully')); + toast.success(__('Delivery option deleted')); }, onError: (error: any) => { - toast.error(error?.message || __('Failed to delete shipping method')); + toast.error(error?.message || __('Failed to delete delivery option')); + }, + }); + + // Fetch method settings for editing + const { data: methodSettings, isLoading: isLoadingSettings } = useQuery({ + queryKey: ['method-settings', selectedZone?.id, editingMethod?.instance_id], + queryFn: () => api.get(`/settings/shipping/zones/${selectedZone.id}/methods/${editingMethod.instance_id}/settings`), + enabled: !!editingMethod && !!selectedZone, + }); + + // Update method settings mutation + const updateSettingsMutation = useMutation({ + mutationFn: async ({ zoneId, instanceId, settings }: { zoneId: number; instanceId: number; settings: any }) => { + return api.post(`/settings/shipping/zones/${zoneId}/methods/${instanceId}/settings`, { settings }); + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['shipping-zones'] }); + queryClient.invalidateQueries({ queryKey: ['method-settings'] }); + toast.success(__('Settings saved')); + setShowEditDialog(false); + setEditingMethod(null); + }, + onError: (error: any) => { + toast.error(error?.message || __('Failed to save settings')); }, }); @@ -278,88 +302,65 @@ export default function ShippingPage() { {selectedZone.regions}

- - - {__('Shipping Methods')} - {__('Zone Details')} - - - -
- {/* Add Method Button */} - +
+
+ {/* Add Delivery Option Button */} + - {/* Methods List */} -
- {selectedZone.rates?.map((rate: any) => ( -
-
-
-
- - -
-
- {__('Cost')}:{' '} - -
+ {/* Delivery Options List */} +
+ {selectedZone.rates?.map((rate: any) => ( +
+
+
+
+ +
-
- + + {rate.enabled ? __('Active') : __('Inactive')} -
-
- ))} -
-
- - - -
-
-
-
-

{__('Zone Name')}

-

{selectedZone.name}

-
-
-

{__('Regions')}

-

{selectedZone.regions}

-
-
-

{__('Zone Order')}

-

{selectedZone.order}

-

{__('Priority in shipping calculations')}

+
+ + +
-
-

- {__('To edit zone name, regions, or order, use WooCommerce.')} -

+ ))}
-
- +
+
- {/* Shipping Methods */} -
-
-

{__('Shipping Methods')}

- - {selectedZone.rates?.length} {selectedZone.rates?.length === 1 ? 'method' : 'methods'} - -
-
- {selectedZone.rates?.map((rate: any) => ( -
-
-
-
- - -
-
- {__('Cost')}:{' '} - -
+ {/* Delivery Options List */} +
+ {selectedZone.rates?.map((rate: any) => ( +
+
+
+
+ +
- - {rate.enabled ? __('Active') : __('Inactive')} - +
+ + + {rate.enabled ? __('On') : __('Off')} + +
+
+
+ +
- ))} -
+
+ ))}
@@ -454,16 +467,15 @@ export default function ShippingPage() { ) )} - {/* Add Method Dialog */} + {/* Add Delivery Option Dialog */} - {__('Add Shipping Method')} + {__('Add Delivery Option')}

- {__('Select a shipping method to add to this zone:')} -

+ {__('Select a delivery option to add:')}

{availableMethods.map((method: any) => (
+ + {/* Edit Settings Dialog */} + { + setShowEditDialog(open); + if (!open) setEditingMethod(null); + }}> + + + {__('Edit Delivery Option')} + + {isLoadingSettings ? ( +
+ +
+ ) : methodSettings ? ( +
+
+ + +

+ {__('Name shown to customers at checkout')} +

+
+ + {methodSettings.settings?.cost && ( +
+ + + {methodSettings.settings.cost.description && ( +

+ {methodSettings.settings.cost.description} +

+ )} +
+ )} + + {methodSettings.settings?.min_amount && ( +
+ + + {methodSettings.settings.min_amount.description && ( +

+ {methodSettings.settings.min_amount.description} +

+ )} +
+ )} + +
+ + +
+
+ ) : null} +
+
); }