427 lines
18 KiB
TypeScript
427 lines
18 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import { Truck, RefreshCw, CheckCircle2, MapPin, Key } from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav';
|
|
import { integrationService } from '@/services/api/integrationService';
|
|
|
|
const fieldClass =
|
|
'w-full h-9 px-3 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary disabled:opacity-60';
|
|
const areaClass =
|
|
'w-full px-3 py-2 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary disabled:opacity-60';
|
|
const primaryButton =
|
|
'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer disabled:opacity-50';
|
|
const secondaryButton =
|
|
'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control border border-border bg-card text-foreground text-[13px] font-medium hover:bg-muted cursor-pointer disabled:opacity-50';
|
|
const shell =
|
|
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0';
|
|
const labelClass = 'block text-[11px] font-semibold text-muted-foreground uppercase mb-1.5';
|
|
|
|
export default function ShippingIntegrationsPage() {
|
|
const [creds, setCreds] = useState<any>({
|
|
email: '',
|
|
pickup_location: 'Primary',
|
|
pickup_pin: '',
|
|
api_url: 'https://apiv2.shiprocket.in/v1/external',
|
|
source: 'unconfigured',
|
|
is_locked_by_config: false,
|
|
});
|
|
const [loading, setLoading] = useState(true);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
// Auth test
|
|
const [authEmail, setAuthEmail] = useState('');
|
|
const [authPassword, setAuthPassword] = useState('');
|
|
const [authing, setAuthing] = useState(false);
|
|
|
|
// Serviceability check
|
|
const [pickupPin, setPickupPin] = useState('');
|
|
const [deliveryPin, setDeliveryPin] = useState('');
|
|
const [weight, setWeight] = useState(0.5);
|
|
const [isCod, setIsCod] = useState(false);
|
|
const [checkingService, setCheckingService] = useState(false);
|
|
const [serviceResults, setServiceResults] = useState<any[]>([]);
|
|
|
|
const fetchShippingConfig = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await integrationService.getShiprocketCredentials();
|
|
setCreds(data);
|
|
if (data.pickup_pin) setPickupPin(data.pickup_pin);
|
|
if (data.email) setAuthEmail(data.email);
|
|
} catch (err: any) {
|
|
toast.error('Failed to load Shiprocket credentials');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchShippingConfig();
|
|
}, []);
|
|
|
|
const handleSave = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setSaving(true);
|
|
try {
|
|
await integrationService.saveShiprocketCredentials(creds);
|
|
toast.success('Shiprocket credentials saved successfully');
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to save Shiprocket credentials');
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
const handleAuthTest = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!authEmail.trim() || !authPassword.trim()) {
|
|
toast.error('Email and password are required for Shiprocket login');
|
|
return;
|
|
}
|
|
setAuthing(true);
|
|
try {
|
|
const res = await integrationService.authShiprocket({
|
|
email: authEmail.trim(),
|
|
password: authPassword.trim(),
|
|
});
|
|
if (res.status === 'SUCCESS') {
|
|
toast.success('Shiprocket Authentication Successful! Session token generated.');
|
|
} else {
|
|
toast.error(`Auth failed: ${res.detail || JSON.stringify(res)}`);
|
|
}
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Shiprocket authentication failed');
|
|
} finally {
|
|
setAuthing(false);
|
|
}
|
|
};
|
|
|
|
const handleCheckServiceability = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!pickupPin.trim() || !deliveryPin.trim()) {
|
|
toast.error('Both Pickup and Delivery Pincodes are required');
|
|
return;
|
|
}
|
|
setCheckingService(true);
|
|
try {
|
|
const res = await integrationService.checkShiprocketServiceability({
|
|
pickup_pin: pickupPin.trim(),
|
|
delivery_pin: deliveryPin.trim(),
|
|
weight,
|
|
cod: isCod,
|
|
});
|
|
if (res.status === 'SUCCESS') {
|
|
const couriers = res.data?.data?.available_courier_companies || [];
|
|
setServiceResults(couriers);
|
|
toast.success(`Serviceability confirmed! ${couriers.length} couriers available.`);
|
|
} else {
|
|
toast.error(`Serviceability check failed: ${res.detail || 'Pincode not serviceable'}`);
|
|
}
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Courier serviceability check failed');
|
|
} finally {
|
|
setCheckingService(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-5 min-w-0">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<h1 className="text-lg font-semibold text-foreground">Shiprocket</h1>
|
|
<span className={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${creds.is_locked_by_config ? 'bg-primary/10 text-primary' : 'bg-muted text-foreground'}`}>
|
|
{creds.is_locked_by_config ? 'Config' : 'Database'}
|
|
</span>
|
|
</div>
|
|
<IntegrationsHeaderNav activeTab="shipping" />
|
|
|
|
{/* Delhivery One Configuration Card */}
|
|
<div className={`${shell} p-5 bg-gradient-to-br from-card to-amber-500/5 border-amber-500/20`}>
|
|
<div className="flex flex-wrap items-center justify-between gap-3 mb-4">
|
|
<div className="flex items-center gap-2.5">
|
|
<div className="w-8 h-8 rounded-lg bg-red-500/10 text-red-600 flex items-center justify-center font-black text-[13px]">
|
|
D1
|
|
</div>
|
|
<div>
|
|
<div className="flex items-center gap-2">
|
|
<h3 className="font-bold text-foreground text-[15px]">Delhivery One Logistics</h3>
|
|
<span className="inline-flex px-2 py-0.5 crm-radius-badge bg-amber-500/15 text-amber-600 border border-amber-500/30 text-[10px] font-bold">
|
|
Trial Mode Active
|
|
</span>
|
|
<span className="inline-flex px-2 py-0.5 crm-radius-badge bg-emerald-500/15 text-emerald-600 border border-emerald-500/30 text-[10px] font-semibold">
|
|
Auto-Pickup Simulated
|
|
</span>
|
|
</div>
|
|
<p className="text-[12px] text-muted-foreground">
|
|
Configured with client credentials & MCP gateway • Safe simulated fulfillment
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<a
|
|
href="/delhivery-logistics"
|
|
className="inline-flex items-center gap-1.5 h-8 px-3 crm-radius-control bg-primary text-white text-[12px] font-semibold hover:bg-primary/90"
|
|
>
|
|
Open Delhivery Hub →
|
|
</a>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3.5 text-[12px]">
|
|
<div className="p-3 rounded-lg bg-card border border-border">
|
|
<span className="text-[10px] font-bold uppercase text-muted-foreground block mb-1">Client ID</span>
|
|
<code className="text-foreground font-mono">ucp-service-cli</code>
|
|
</div>
|
|
<div className="p-3 rounded-lg bg-card border border-border">
|
|
<span className="text-[10px] font-bold uppercase text-muted-foreground block mb-1">Client Realm</span>
|
|
<code className="text-foreground font-mono">ucp-Q72U870K1D05</code>
|
|
</div>
|
|
<div className="p-3 rounded-lg bg-card border border-border">
|
|
<span className="text-[10px] font-bold uppercase text-muted-foreground block mb-1">Client CMS ID</span>
|
|
<code className="text-foreground font-mono">cms::client::e97e8488-2ebd...</code>
|
|
</div>
|
|
<div className="p-3 rounded-lg bg-card border border-border">
|
|
<span className="text-[10px] font-bold uppercase text-muted-foreground block mb-1">Auth Endpoint</span>
|
|
<span className="text-muted-foreground truncate block">https://ucp-auth.delhivery.com/holyknight</span>
|
|
</div>
|
|
<div className="p-3 rounded-lg bg-card border border-border">
|
|
<span className="text-[10px] font-bold uppercase text-muted-foreground block mb-1">Origin Pickup PIN</span>
|
|
<span className="text-foreground font-bold">560001 (Bengaluru Central Hub)</span>
|
|
</div>
|
|
<div className="p-3 rounded-lg bg-card border border-border">
|
|
<span className="text-[10px] font-bold uppercase text-muted-foreground block mb-1">MCP Auto-Mint</span>
|
|
<span className="text-emerald-600 font-semibold">d1-mcp-mint@latest (Active)</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
|
|
{/* Credentials Form (Cols 1-6) */}
|
|
<div className={`${shell} p-4 lg:col-span-6`}>
|
|
<div className="flex items-center justify-between mb-4">
|
|
<h3 className="font-semibold text-foreground flex items-center gap-2">
|
|
<Key className="w-4 h-4 text-primary" />
|
|
Shiprocket Logistics Settings
|
|
</h3>
|
|
</div>
|
|
|
|
<form onSubmit={handleSave} className="space-y-4">
|
|
<div>
|
|
<label className={labelClass}>
|
|
Shiprocket Account Email
|
|
</label>
|
|
<input
|
|
type="email"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.email || ''}
|
|
onChange={(e) => setCreds({ ...creds, email: e.target.value })}
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className={labelClass}>
|
|
Primary Pickup Hub
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.pickup_location || 'Primary'}
|
|
onChange={(e) => setCreds({ ...creds, pickup_location: e.target.value })}
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
Warehouse Pincode
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.pickup_pin || ''}
|
|
onChange={(e) => setCreds({ ...creds, pickup_pin: e.target.value })}
|
|
placeholder="Pincode"
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{!creds.is_locked_by_config && (
|
|
<button
|
|
type="submit"
|
|
disabled={saving}
|
|
className={`${primaryButton} w-full`}
|
|
>
|
|
{saving ? 'Saving...' : 'Save Shiprocket Settings'}
|
|
</button>
|
|
)}
|
|
</form>
|
|
|
|
{/* Quick Login Authenticator */}
|
|
<div className="mt-6 pt-5 border-t border-border">
|
|
<h4 className="text-xs font-semibold text-foreground uppercase mb-2">
|
|
Refresh Authentication Token
|
|
</h4>
|
|
<form onSubmit={handleAuthTest} className="space-y-3">
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
|
<input
|
|
type="email"
|
|
placeholder="Account email"
|
|
value={authEmail}
|
|
onChange={(e) => setAuthEmail(e.target.value)}
|
|
className={fieldClass}
|
|
/>
|
|
<input
|
|
type="password"
|
|
placeholder="Account password"
|
|
value={authPassword}
|
|
onChange={(e) => setAuthPassword(e.target.value)}
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
<button
|
|
type="submit"
|
|
disabled={authing}
|
|
className={`${secondaryButton} w-full`}
|
|
>
|
|
<RefreshCw className={`w-3.5 h-3.5 ${authing ? 'animate-spin' : ''}`} />
|
|
{authing ? 'Authenticating...' : 'Authenticate & Generate Token'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Real-time Courier Serviceability Checker (Cols 7-12) */}
|
|
<div className={`${shell} p-4 lg:col-span-6 flex flex-col justify-between`}>
|
|
<div>
|
|
<h3 className="font-semibold text-foreground flex items-center gap-2 mb-4">
|
|
<MapPin className="w-4 h-4 text-primary" />
|
|
Pincode Serviceability & Rate Calculator
|
|
</h3>
|
|
|
|
<form onSubmit={handleCheckServiceability} className="space-y-4">
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className={labelClass}>
|
|
Pickup Pincode
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={pickupPin}
|
|
onChange={(e) => setPickupPin(e.target.value)}
|
|
placeholder="Pincode"
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
Destination Pincode
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={deliveryPin}
|
|
onChange={(e) => setDeliveryPin(e.target.value)}
|
|
placeholder="Pincode"
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4 items-center">
|
|
<div>
|
|
<label className={labelClass}>
|
|
Weight (kg)
|
|
</label>
|
|
<input
|
|
type="number"
|
|
step="0.1"
|
|
value={weight}
|
|
onChange={(e) => setWeight(parseFloat(e.target.value) || 0.5)}
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div className="pt-4">
|
|
<label className="flex items-center gap-2 cursor-pointer text-xs font-medium text-foreground">
|
|
<input
|
|
type="checkbox"
|
|
checked={isCod}
|
|
onChange={(e) => setIsCod(e.target.checked)}
|
|
className="rounded border-border text-primary focus:ring-primary w-4 h-4"
|
|
/>
|
|
Cash On Delivery (COD)
|
|
</label>
|
|
</div>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={checkingService}
|
|
className={`${primaryButton} w-full`}
|
|
>
|
|
<Truck className={`w-3.5 h-3.5 ${checkingService ? 'animate-bounce' : ''}`} />
|
|
{checkingService ? 'Checking Couriers...' : 'Calculate Serviceability'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
{/* Available Couriers Table */}
|
|
{serviceResults.length > 0 && (
|
|
<div className={`${shell} p-4`}>
|
|
<h3 className="font-semibold text-foreground flex items-center gap-2 mb-4">
|
|
<CheckCircle2 className="w-4 h-4 text-primary" />
|
|
Couriers ({serviceResults.length})
|
|
</h3>
|
|
|
|
<div className="overflow-x-auto">
|
|
<table className="crm-data-table w-full text-left">
|
|
<thead className="bg-muted/40 text-muted-foreground uppercase font-medium border-y border-border">
|
|
<tr>
|
|
<th className="py-2.5 px-3">Courier Name</th>
|
|
<th className="py-2.5 px-3">Estimated Delivery</th>
|
|
<th className="py-2.5 px-3">Rating</th>
|
|
<th className="py-2.5 px-3">Freight Charge</th>
|
|
<th className="py-2.5 px-3">COD Eligible</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-border">
|
|
{serviceResults.map((c: any, idx: number) => (
|
|
<tr key={idx} className="hover:bg-muted/40">
|
|
<td className="py-2.5 px-3 font-semibold text-foreground">
|
|
{c.courier_name || '—'}
|
|
</td>
|
|
<td className="py-2.5 px-3 text-muted-foreground">
|
|
{c.estimated_delivery_days ? `${c.estimated_delivery_days} Days` : '—'}
|
|
</td>
|
|
<td className="py-2.5 px-3 text-foreground">
|
|
{c.rating ?? '—'}
|
|
</td>
|
|
<td className="py-2.5 px-3 font-medium text-foreground">
|
|
{c.rate || c.freight_charge ? `₹${c.rate || c.freight_charge}` : '—'}
|
|
</td>
|
|
<td className="py-2.5 px-3">
|
|
<span
|
|
className={`inline-flex px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${
|
|
c.cod === 1 || c.is_cod
|
|
? 'bg-success/15 text-success'
|
|
: 'bg-muted text-muted-foreground'
|
|
}`}
|
|
>
|
|
{c.cod === 1 || c.is_cod ? 'Yes' : 'Prepaid Only'}
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|