diff --git a/src/Pages/BookingScreen/Components/PrintMainPage/PrinterSelection.jsx b/src/Pages/BookingScreen/Components/PrintMainPage/PrinterSelection.jsx index 9146c44..5c58a16 100644 --- a/src/Pages/BookingScreen/Components/PrintMainPage/PrinterSelection.jsx +++ b/src/Pages/BookingScreen/Components/PrintMainPage/PrinterSelection.jsx @@ -30,7 +30,7 @@ const PrinterSelection = (props) => { const [initialPrinters, setInitialPrinters] = useState([]); const PrinterDetails = useSelector(GlobalPrinterMappingDtls); const FeatureAddonData = useSelector(GlobalFeatAddOnData); - const [MyPrinter, setMyPrinter] = useState(true); + const [MyPrinter, setMyPrinter] = useState(true); const [MyKioskPrinter, setMyKioskPrinter] = useState(false); const { onClose } = props; // const initialPrinters = [ @@ -50,23 +50,23 @@ const PrinterSelection = (props) => { getCounters(); }, []); useEffect(() => { - const onFocus = () => { - const isTriggered = localStorage.getItem("trigger_from_app"); + const onFocus = () => { + const isTriggered = localStorage.getItem("trigger_from_app"); - if (isTriggered === "1") { - console.log("App trigger detected — refreshing printer data..."); - getPrinters(); - getCounters(); + if (isTriggered === "1") { + console.log("App trigger detected — refreshing printer data..."); + getPrinters(); + getCounters(); - // Clear flag so it doesn't run again - localStorage.removeItem("trigger_from_app"); - } - }; + // Clear flag so it doesn't run again + localStorage.removeItem("trigger_from_app"); + } + }; - window.addEventListener("focus", onFocus); - return () => window.removeEventListener("focus", onFocus); + window.addEventListener("focus", onFocus); + return () => window.removeEventListener("focus", onFocus); -}, []); + }, []); const getPrinters = async () => { const data = { AppId: AppId, @@ -79,17 +79,17 @@ const PrinterSelection = (props) => { const printerData = response?.data?.data || []; const printMappingDtl = printerData?.[0]?.PrintMappingDtl || []; setInitialPrinters(printMappingDtl); - + // Map API data: preserve original MappingPrintType (K/U/null) // For user mappings, use backend values; for others, create user mapping structure const mappedPrinters = printMappingDtl.map((printer) => { const isUserMapping = printer.MappingPrintType === 'U' && printer.UserId === UserId; const physicalId = `${printer.ConnectionKey}${(printer.MacAddress !== 'N/A' && printer.MacAddress !== null) ? `_${printer.MacAddress}` : ''}`; // Preserve null MappingPrintType - don't convert to 'U' - const mappingPrintType = printer.MappingPrintType !== null && printer.MappingPrintType !== undefined - ? printer.MappingPrintType + const mappingPrintType = printer.MappingPrintType !== null && printer.MappingPrintType !== undefined + ? printer.MappingPrintType : null; - + return { ConnectionKey: printer.ConnectionKey, PrinterName: printer.PrinterName, @@ -108,9 +108,12 @@ const PrinterSelection = (props) => { MappingPrintType: mappingPrintType, UserId: printer.UserId || UserId, DeviceAddress: printer.DeviceAddress || null, + TakeAwayTokenFlg: printer.TakeAwayTokenFlg || "N", + DineInTokenFlg: printer.DineInTokenFlg || "N" + }; }); - + setPrinters(mappedPrinters); } else { setInitialPrinters([]) @@ -128,7 +131,7 @@ const PrinterSelection = (props) => { setcounters(counterdata); } - + const toggleSetPrint = (printerId) => { const selectedPrinter = printers.find(p => p.PrintUniqId === printerId); console.log(printerId, selectedPrinter, printers, 'printerId'); @@ -136,9 +139,9 @@ const PrinterSelection = (props) => { setPrinters((prev) => { // Check if user mapping already exists for this physical printer - const existingUserMapping = prev.find(p => - p.id === selectedPrinter.id && - p.MappingPrintType === 'U' && + const existingUserMapping = prev.find(p => + p.id === selectedPrinter.id && + p.MappingPrintType === 'U' && p.UserId === UserId ); @@ -184,10 +187,10 @@ const PrinterSelection = (props) => { setPrinters((prev) => prev.map((p) => { - if (p.ConnectionKey === selectedPrinter.ConnectionKey && - p.MacAddress === selectedPrinter.MacAddress && - p.UserId === UserId && - p.MappingPrintType === 'U') { + if (p.ConnectionKey === selectedPrinter.ConnectionKey && + p.MacAddress === selectedPrinter.MacAddress && + p.UserId === UserId && + p.MappingPrintType === 'U') { return { ...p, CounterId: p.CounterId === counter.CounterId ? null : counter.CounterId, @@ -204,10 +207,10 @@ const PrinterSelection = (props) => { setPrinters(prev => prev.map(p => { - if (p.ConnectionKey === selectedPrinter.ConnectionKey && - p.MacAddress === selectedPrinter.MacAddress && - p.UserId === UserId && - p.MappingPrintType === 'U') { + if (p.ConnectionKey === selectedPrinter.ConnectionKey && + p.MacAddress === selectedPrinter.MacAddress && + p.UserId === UserId && + p.MappingPrintType === 'U') { return { ...p, MappingType: newType }; } return p; @@ -220,28 +223,28 @@ const PrinterSelection = (props) => { // Remove duplicates by PrintUniqId, keep only unique entries const uniquePrinters = []; const seenKeys = new Set(); - + printers.forEach(printer => { if (!seenKeys.has(printer.PrintUniqId)) { seenKeys.add(printer.PrintUniqId); uniquePrinters.push(printer); } }); - + console.log(uniquePrinters, 'uniquePrinters'); - + // IMPORTANT: Send ALL user mappings (both Y and N) so backend can remove mappings // Filter to only user mappings (MappingPrintType === 'U') for current user - const userMappings = uniquePrinters.filter(p => + const userMappings = uniquePrinters.filter(p => p.MappingPrintType === 'U' && p.UserId === UserId ); - + const PostPrintMappingdata = { AppId: AppId, - CompId: CompId, - BranchId: BranchId, - UserId: UserId, - UpdatedBy: UserId, + CompId: CompId, + BranchId: BranchId, + UserId: UserId, + UpdatedBy: UserId, // Send all user mappings (Y and N) so removal works PrintMappingDtl: userMappings } @@ -250,14 +253,14 @@ const PrinterSelection = (props) => { if (response?.data?.statusCode === 1) { setMessageType("success"); setMessageData("Printer settings saved successfully."); - const data = { - AppId: AppId, - CompId: CompId, - BranchId: BranchId, - UserId: UserId - } - await dispatch(getPrinterMappingDetails(data)).unwrap(); - getCounters(); + const data = { + AppId: AppId, + CompId: CompId, + BranchId: BranchId, + UserId: UserId + } + await dispatch(getPrinterMappingDetails(data)).unwrap(); + getCounters(); } else { setMessageType("error"); setMessageData(response?.data?.message || "Failed to save printer settings."); @@ -277,16 +280,39 @@ const PrinterSelection = (props) => { setPrinters((prev) => prev.map((p) => { - if (p.ConnectionKey === selectedPrinter.ConnectionKey && - p.MacAddress === selectedPrinter.MacAddress && - p.UserId === UserId && - p.MappingPrintType === 'U') { + if (p.ConnectionKey === selectedPrinter.ConnectionKey && + p.MacAddress === selectedPrinter.MacAddress && + p.UserId === UserId && + p.MappingPrintType === 'U' + ) { return { ...p, CustomerCopy: value }; } return p; }) ); }; + const updateTokenAvailable = (printerId, type, checked) => { + const selectedPrinter = printers.find(p => p.id === printerId); + if (!selectedPrinter) return; + + setPrinters((prev) => + prev.map((p) => { + if ( + p.ConnectionKey === selectedPrinter.ConnectionKey && + p.MacAddress === selectedPrinter.MacAddress + ) { + if (type === "Takeaway") { + return { ...p, TakeAwayTokenFlg: checked ? "Y" : "N" }; + } + if (type === "Dine") { + return { ...p, DineInTokenFlg: checked ? "Y" : "N" }; + } + } + return p; + }) + ); + }; + const openKioskPrinter = () => { setMyKioskPrinter(true); setMyPrinter(false); @@ -306,7 +332,7 @@ const PrinterSelection = (props) => { }} />