const updatePalletCountAsync = async ( lineNum, subtract = false, partial = 0 ) => { setGlobalSpinner(true); const palletValue = subtract ? -1 : 1; // this may be seperated out later let tempPalletTallyCount = headingPalletsTally; // look into if this is the right way of doing it let tempPalletCount = palletCount; let isPartial = false; if (partial === 0) { // this is a full pallet update tempPalletTallyCount += palletValue; tempPalletCount += palletValue; } else { // this is a partial count update isPartial = true; } if (!subtract || (subtract && tempPalletCount >= 0)) { const result = await getPalletTallyAsync(); const tally = result.find((t) => t.ORDER_ID === orderNumber); let tempPalletTally; if (tally) { if (!subtract) { tempPalletTally = tally.TallyCount + 1; } else { tempPalletTally = tally.TallyCount - 1; } } else { if (!subtract) { tempPalletTally = 1; } else { tempPalletTally = 0; } } if (bagsPerPallet === 0 && partial === 0) { Store.addNotification({ type: 'danger', // eslint-disable-next-line title: 'Error', message: 'Cannot send a quantity of zero bags.', insert: 'top', container: 'top-center', animationIn: ['animated', 'bounceIn'], animationOut: ['animated', 'fadeOut'], dismiss: { duration: 4000, onScreen: true, }, }); return { success: false }; } else { const palletCountInfo = { orderID: orderNumber, itemID: itemNumber, palletCount: palletValue, bagsPerPallet: subtract ? -1 * bagsPerPallet : bagsPerPallet, lineNum, shiftTallyCount: tempPalletTally, partial, isPartial, }; updatePalletCount(palletCountInfo).then((res) => { if (res.status === 200) { console.log({ res1: res }); setPalletCount(tempPalletCount); const rem = parseInt(activeOrderObj.palletsRem, 10); const count = parseInt(activeOrderObj.palletCount, 10); const tempOrder = { ...activeOrderObj }; console.log({ tempOrder, count }); if (!isPartial) { tempOrder.palletsRem = count <= tempOrder.palletsOrd ? subtract ? rem + 1 : rem > 0 ? rem - 1 : 0 : 0; const tempTotalBagsUsed = subtract ? tempOrder.totalBagsUsed - tempOrder.bagsPerPallet : tempOrder.totalBagsUsed + tempOrder.bagsPerPallet; setTotalBagsUsed(tempTotalBagsUsed); tempOrder.totalBagsUsed = tempTotalBagsUsed; tempOrder.palletCount = subtract ? count - 1 : count + 1; } else { tempOrder.partialPalletCount = tempOrder.partialPalletCount + partial; console.log({ partial }); } setUpdateHeadingPallets(false); // setOrders((prevOrders) => { // return prevOrders.map((order) => { // return order.orderNumber === // activeOrderObj.orderNumber // ? activeOrderObj // : order; // }); // }); setLineOrders((prevOrders) => { return prevOrders.map((order) => { return order.orderNumber === tempOrder.orderNumber ? tempOrder : order; }); }); setActiveOrderObj(tempOrder); // setPalletsDone(activeOrderObj.palletCount); setGlobalSpinner(false); console.log({ tempPalletTallyCount, headingPalletsTally, }); if (tempPalletTallyCount !== headingPalletsTally) { setHeadingPalletsTally(tempPalletTallyCount); setChangedCount(true); setTimeout(() => { setChangedCount(false); }, 2000); // send websocket message for tally count update if (socket) { socket.send( JSON.stringify({ eventType: 'orderUpdate', updatedVar: 'tallyCountUpdate', orderNum: tempOrder.orderNumber, newData: { tallyCount: tempPalletTallyCount, palletsLeft: tempOrder.palletsRem, palletCount: tempOrder.palletCount, totalBagsUsed: tempOrder.totalBagsUsed, }, }) ); } } if (socket && partial !== 0) { // send partial pallet update to websockets socket.send( JSON.stringify({ eventType: 'orderUpdate', updatedVar: 'partialPallet', orderNum: tempOrder.orderNumber, newData: { partial, }, }) ); } return { success: true }; } else { Store.addNotification({ type: 'danger', // eslint-disable-next-line title: 'Error', message: res.error, insert: 'top', container: 'top-center', animationIn: ['animated', 'bounceIn'], animationOut: ['animated', 'fadeOut'], dismiss: { duration: 4000, onScreen: true, }, }); return { success: false }; } }); } } else { // pallet count is already zero, show warning message Store.addNotification({ type: 'warning', // eslint-disable-next-line title: "Can't subtract!", message: 'Pallet count is already zero.', insert: 'top', container: 'top-center', animationIn: ['animated', 'bounceIn'], animationOut: ['animated', 'fadeOut'], dismiss: { duration: 4000, onScreen: true, }, }); return { success: false }; } };