diff --git a/src/components/forms/DynamicForm.tsx b/src/components/forms/DynamicForm.tsx index 66a81ea..3755755 100644 --- a/src/components/forms/DynamicForm.tsx +++ b/src/components/forms/DynamicForm.tsx @@ -45,6 +45,8 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: const [loading, setLoading] = useState(true); const [error, setError] = useState(null); + const [chainedPrefillData, setChainedPrefillData] = useState | undefined>(); + useEffect(() => { let mounted = true; setLoading(true); @@ -132,6 +134,12 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: if (!imageFieldIds.has(k)) defaultValues[k] = v; }); } + + if (chainedPrefillData) { + Object.entries(chainedPrefillData).forEach(([k, v]) => { + if (!imageFieldIds.has(k)) defaultValues[k] = v; + }); + } setValues(defaultValues); setLoading(false); @@ -268,6 +276,39 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: const nextActivity = pending.shift(); if (nextActivity) { + let nextPrefillData = undefined; + if (nextActivity.activity_uid === ORDER_BOOKING.activities.POTENTIAL_MINING.uid) { + try { + const pmRes = await client.request<{ potential: { potential: any[] } }>( + 'POST', + '/api/papi2/potential-mining', + { instance_id: String(res.instance_id ?? currentInstanceId) }, + { 'TemplateID': '146' } + ); + const rawPotential = pmRes.potential?.potential || []; + const mappedPotential = rawPotential.map((row: any) => { + const cat = row.product_category || row.product_category_ || row.category; + return { + ...row, + product_category_: cat, + product_category: cat, + productcategory: cat, + category: cat, + product_category_1: cat + }; + }); + nextPrefillData = { potential: mappedPotential }; + } catch (e) { + console.error("Failed to fetch potential mining for chain", e); + } + } + + if (nextPrefillData) { + setChainedPrefillData(nextPrefillData); + } else { + setChainedPrefillData(undefined); + } + setChainQueue(pending); setCurrentActivityId(nextActivity.activity_uid); setCurrentInstanceId(res.instance_id ?? currentInstanceId); diff --git a/src/screens/AnalyticsPage.tsx b/src/screens/AnalyticsPage.tsx index 77b22fc..b2d8f12 100644 --- a/src/screens/AnalyticsPage.tsx +++ b/src/screens/AnalyticsPage.tsx @@ -4,8 +4,7 @@ import { ORDER_BOOKING } from '../api/config'; import { Spinner } from '../components/reusable/Spinner'; import { EmptyState } from '../components/reusable/EmptyState'; import { StatsTiles } from '../components/reusable/StatsTiles'; -import { Activity, Package, ShoppingBag, BarChart3, TrendingUp, Phone } from 'lucide-react'; -import { formatValue } from '../lib/format'; +import { TrendingUp } from 'lucide-react'; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip as RechartsTooltip, ResponsiveContainer, PieChart, Pie, Cell, Legend diff --git a/src/screens/OrdersPage.tsx b/src/screens/OrdersPage.tsx index 0b21a6e..b64b1a4 100644 --- a/src/screens/OrdersPage.tsx +++ b/src/screens/OrdersPage.tsx @@ -4,7 +4,8 @@ import { OrdersView } from '../components/rv'; import { OrderDetail } from '../components/dv'; import { useState } from 'react'; -import { ShoppingBag } from 'lucide-react'; +import { ShoppingBag, Plus } from 'lucide-react'; +import { Button } from '../components/buttons/Button'; import { DynamicForm } from '../components/forms/DynamicForm'; import { ORDER_BOOKING } from '../api/config'; import { orderBookingClient } from '../api/clients'; @@ -17,12 +18,63 @@ export function OrdersPage() { const [createTitle, setCreateTitle] = useState("Place Order"); const [refreshKey, setRefreshKey] = useState(0); const [activeActivity, setActiveActivity] = useState<{ id: string; name: string } | null>(null); + + const [isFabOpen, setIsFabOpen] = useState(false); + const [miningLoading, setMiningLoading] = useState(false); + const [miningPrefill, setMiningPrefill] = useState | undefined>(); + const [selectedRow, setSelectedRow] = useState | null>(null); + + const handlePotentialMiningClick = async () => { + setMiningLoading(true); + setMiningPrefill(undefined); + try { + const storeCode = selectedRow?.store_code || selectedRow?.code || selectedRow?.store?.store_code; + + if (!storeCode) { + console.warn("Store code not found on selected row, proceeding anyway."); + } + + const payload = { + store_code: storeCode, + instance_id: String(instanceId) + }; + + const response = await orderBookingClient.request<{ potential: { potential: any[] } }>( + 'POST', + '/api/papi2/potential-mining', + payload, + { 'TemplateID': '146' } + ); + + const rawPotential = response.potential?.potential || []; + const mappedPotential = rawPotential.map((row: any) => { + const cat = row.product_category || row.product_category_ || row.category; + return { + ...row, + product_category_: cat, + product_category: cat, + productcategory: cat, + category: cat, + product_category_1: cat + }; + }); + + setMiningPrefill({ potential: mappedPotential }); + setActiveActivity({ id: ORDER_BOOKING.activities.POTENTIAL_MINING.uid, name: 'Potential Mining' }); + } catch (e: any) { + alert("Failed to fetch potential mining data: " + (e.message || "Unknown error")); + } finally { + setMiningLoading(false); + setIsFabOpen(false); + } + }; return ( <> { + setSelectedRow(row); const id = row.instance_id as number | string | undefined; if (id != null) navigate(`/orders/${id}`); }} @@ -56,11 +108,66 @@ export function OrdersPage() { navigate(`/orders`)} + onClose={() => { navigate(`/orders`); setIsFabOpen(false); }} title={instanceId != null ? `Order #${instanceId}` : undefined} width="lg" > - {instanceId != null && } + {instanceId != null && ( + <> + +
+ {isFabOpen && ( +
+ + {(() => { + const stateName = String(selectedRow?.current_state_name || selectedRow?.current_state_name_ || selectedRow?.current_state || selectedRow?.status || '').toLowerCase(); + if (stateName.includes('ordered') || stateName === 'ordered') { + return ( + + ); + } else if (stateName.includes('productive')) { + return ( + + ); + } + return ( + + ); + })()} +
+ )} + +
+ + )}
{ setActiveActivity(null); setRefreshKey(k => k + 1);