UNPKG

@open-tender/store

Version:

A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API

123 lines (122 loc) 5.05 kB
import { __awaiter, __generator } from "tslib"; import { handleRespError } from '@open-tender/utils'; import { useState, useEffect, useRef, useCallback, useMemo } from 'react'; import { makePunchMessage } from '../utils'; var codeErrMsg = 'Employee has been assigned a swipe card. Please use card to punch in / out.'; var deptErrMsg = "This employee doesn't have any departments. Please contact HR."; var usePunch = function (api) { var isAborted = useRef(false); var _a = useState(null), error = _a[0], setError = _a[1]; var _b = useState(null), msg = _b[0], setMsg = _b[1]; var _c = useState(null), modal = _c[0], setModal = _c[1]; useEffect(function () { return function () { isAborted.current = true; }; }, []); var reset = useCallback(function () { setError(null); setModal(null); setMsg(null); }, []); var print = useCallback(function (employeeId) { return __awaiter(void 0, void 0, void 0, function () { var err_1, detail; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, api.postPrintShiftSummary(employeeId)]; case 1: _a.sent(); return [3 /*break*/, 3]; case 2: err_1 = _a.sent(); if (!isAborted.current) { detail = handleRespError(err_1).detail; setError(detail || 'Unknown error'); } return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }, [api]); var send = useCallback(function (data, employee) { return __awaiter(void 0, void 0, void 0, function () { var err_2, detail; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, api.postTimePunch(data)]; case 1: _a.sent(); if (!isAborted.current) { setMsg(makePunchMessage(employee, data.punch_type)); } return [3 /*break*/, 3]; case 2: err_2 = _a.sent(); if (!isAborted.current) { detail = handleRespError(err_2).detail; setError(detail || 'Unknown error'); } return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }, [api]); var submit = useCallback(function (employee, blockPin) { if (employee && employee.code && blockPin) { setError(codeErrMsg); } else if (!employee.punches) { if (!employee.departments.length) { setError(deptErrMsg); } else if (employee.departments.length > 1) { setModal({ type: 'punchDept', args: { employee: employee } }); } else { var employee_id = employee.employee_id, departments = employee.departments; var data = { employee_id: employee_id, department_id: departments[0].id, punch_type: 'PUNCH_IN', tips: '0.00' }; send(data, employee); } } else { var lastPunch = employee.punches[employee.punches.length - 1]; var employee_id = lastPunch.employee_id, department_id = lastPunch.department_id, punch_type = lastPunch.punch_type; if (punch_type === 'BREAK_OUT') { var data = { employee_id: employee_id, department_id: department_id, punch_type: 'BREAK_IN', tips: '0.00' }; send(data, employee); } else if (punch_type === 'PUNCH_OUT') { if (employee.departments.length > 1) { setModal({ type: 'punchDept', args: { employee: employee } }); } else { var data = { employee_id: employee_id, department_id: employee.departments[0].id, punch_type: 'PUNCH_IN', tips: '0.00' }; send(data, employee); } } else { setModal({ type: 'punchType', args: { employee: employee } }); } } }, [send]); return useMemo(function () { return ({ submit: submit, send: send, print: print, error: error, msg: msg, modal: modal, reset: reset }); }, [submit, send, print, error, msg, modal, reset]); }; export default usePunch;