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) => { }} />
-
{ onClick={openMyPrinter} > {/* */} - My Printer + My Printer
{FeatureAddonData?.FeatureDtls?.find( - (item) => - item?.FeatureAddonName?.toLowerCase() === 'kiosk sales' - ) && + (item) => + item?.FeatureAddonName?.toLowerCase() === 'kiosk sales' + ) &&
{/* */} - Kiosk Printer + Kiosk Printer
}
- - -
- {MyPrinter && ( -
- { - const uniquePrinters = []; - const seenPhysicalIds = new Set(); - const userMappedPhysicalIds = new Set(); - - // First, collect physical printer IDs (ConnectionKey + MacAddress) - // that are actively mapped to current user (MappingPrintType === 'U' && UserId === UserId && ChkStatus === 'Y') - // Only exclude printers that are currently active, not removed ones - printers.forEach(p => { - if (p.MappingPrintType === 'U' && p.UserId === UserId && p.ChkStatus === 'Y') { - userMappedPhysicalIds.add(p.id); - } - }); - - // Build dropdown: show each physical printer once - // Exclude printers actively mapped to current user (ChkStatus === 'Y') - // Include: unmapped (null), kiosk (K), removed user mappings (ChkStatus === 'N'), and other users' mappings - printers.forEach(p => { - const physicalId = p.id; // ConnectionKey + MacAddress - - // Skip if this physical printer is actively mapped to current user (ChkStatus === 'Y') - if (userMappedPhysicalIds.has(physicalId)) return; - - // Show each physical printer only once - if (!seenPhysicalIds.has(physicalId)) { - seenPhysicalIds.add(physicalId); - uniquePrinters.push({ - label: `${p.ConnectionKey}${p.PrinterName && p.PrinterName !== 'Unknown' ? ` (${p.PrinterName})` : ''}${(p.MacAddress !== 'N/A' && p.MacAddress !== null) ? ` (${p.MacAddress})` : ''}`, - value: p.PrintUniqId, // Use PrintUniqId (will create new user mapping in toggleSetPrint) - }); - } - }); - - return uniquePrinters; - })()} - defaultValue={null} - valueData={selectedPrinterId} - placeholder="Select Printer" - onChangeFunction={(value) => { - const printer = printers.find(p => p.PrintUniqId === value); - if (printer) { - toggleSetPrint(printer.PrintUniqId); - } - setSelectedPrinterId(null); - }} - /> + +
+ {MyPrinter && ( +
+ { + const uniquePrinters = []; + const seenPhysicalIds = new Set(); + const userMappedPhysicalIds = new Set(); + + // First, collect physical printer IDs (ConnectionKey + MacAddress) + // that are actively mapped to current user (MappingPrintType === 'U' && UserId === UserId && ChkStatus === 'Y') + // Only exclude printers that are currently active, not removed ones + printers.forEach(p => { + if (p.MappingPrintType === 'U' && p.UserId === UserId && p.ChkStatus === 'Y') { + userMappedPhysicalIds.add(p.id); + } + }); + + // Build dropdown: show each physical printer once + // Exclude printers actively mapped to current user (ChkStatus === 'Y') + // Include: unmapped (null), kiosk (K), removed user mappings (ChkStatus === 'N'), and other users' mappings + printers.forEach(p => { + const physicalId = p.id; // ConnectionKey + MacAddress + + // Skip if this physical printer is actively mapped to current user (ChkStatus === 'Y') + if (userMappedPhysicalIds.has(physicalId)) return; + + // Show each physical printer only once + if (!seenPhysicalIds.has(physicalId)) { + seenPhysicalIds.add(physicalId); + uniquePrinters.push({ + label: `${p.ConnectionKey}${p.PrinterName && p.PrinterName !== 'Unknown' ? ` (${p.PrinterName})` : ''}${(p.MacAddress !== 'N/A' && p.MacAddress !== null) ? ` (${p.MacAddress})` : ''}`, + value: p.PrintUniqId, // Use PrintUniqId (will create new user mapping in toggleSetPrint) + }); + } + }); + + return uniquePrinters; + })()} + defaultValue={null} + valueData={selectedPrinterId} + placeholder="Select Printer" + onChangeFunction={(value) => { + const printer = printers.find(p => p.PrintUniqId === value); + if (printer) { + toggleSetPrint(printer.PrintUniqId); + } + setSelectedPrinterId(null); + }} + /> + +
+ {(() => { + const uniquePrinters = []; + const seenKeys = new Set(); + + printers + .filter(p => p.ChkStatus === 'Y' && p.MappingPrintType === 'U' && p.UserId === UserId) + .forEach(printer => { + if (!seenKeys.has(printer.id)) { + seenKeys.add(printer.id); + uniquePrinters.push(printer); + } + }); + + return uniquePrinters; + })().map((printer) => (
- {(() => { - const uniquePrinters = []; - const seenKeys = new Set(); - - printers - .filter(p => p.ChkStatus === 'Y' && p.MappingPrintType === 'U' && p.UserId === UserId) - .forEach(printer => { - if (!seenKeys.has(printer.id)) { - seenKeys.add(printer.id); - uniquePrinters.push(printer); - } - }); - - return uniquePrinters; - })().map((printer) => ( -
+ 🖨️ {printer.name || printer.id} +
+
+
+ +
-
-
- -
+
+ { + const newValue = checked ? 'G' : 'I'; + updatePrinterMappingType(printer.id, newValue); + }} + checkedChildren="Group" + unCheckedChildren="Individual" + style={{ + transform: 'scale(1.2)', + transformOrigin: 'left center', + width: '85px', + }} + /> +
+
+ + updateCustomerCopy(printer.id, e.target.value)} + style={{ + width: "70px", + padding: "6px 8px", + borderRadius: "4px", + border: "1px solid #ccc", + outline: "none", + }} + /> +
+
+ -
- { - const newValue = checked ? 'G' : 'I'; - updatePrinterMappingType(printer.id, newValue); - }} - checkedChildren="Group" - unCheckedChildren="Individual" - style={{ - transform: 'scale(1.2)', - transformOrigin: 'left center', - width: '85px', - }} - /> -
-
- - updateCustomerCopy(printer.id, e.target.value)} - style={{ - width: "70px", - padding: "6px 8px", - borderRadius: "4px", - border: "1px solid #ccc", - outline: "none", - }} - /> -
-
+ -
+ + updateTokenAvailable(printer.id, "Dine", e.target.checked) + } + /> + Dine + +
+
+ +
+ {counters?.map((counter) => { + const isCounterUsedElsewhere = printers?.some( + (p) => p.id !== printer.id && p.CounterId === counter.CounterId + ); + + return ( +
-
- ))} - + toggleCounter(printer.id, counter)} + disabled={isCounterUsedElsewhere} + /> + {counter?.CounterName} + + ); + })} +
+ ))} -
- } - htmlType={true} - handleSubmit={handleSave} - /> -
-
- )} - {MyKioskPrinter && ( -
- {' '} - setOpenModel(false)} +
+ +
+ } + htmlType={true} + handleSubmit={handleSave} />
- )} + + )} + {MyKioskPrinter && ( +
+ {' '} + setOpenModel(false)} + /> +
+ )} ); };