@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
65 lines (64 loc) • 2.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var utils_1 = require("@open-tender/utils");
var react_1 = require("react");
var useEmployee = function (api) {
var isAborted = (0, react_1.useRef)(false);
var _a = (0, react_1.useState)(null), employee = _a[0], setEmployee = _a[1];
var _b = (0, react_1.useState)(null), error = _b[0], setError = _b[1];
(0, react_1.useEffect)(function () {
return function () {
isAborted.current = true;
};
}, []);
var fetch = (0, react_1.useCallback)(function (identifier_1) {
var args_1 = [];
for (var _i = 1; _i < arguments.length; _i++) {
args_1[_i - 1] = arguments[_i];
}
return tslib_1.__awaiter(void 0, tslib_1.__spreadArray([identifier_1], args_1, true), void 0, function (identifier, withPunches) {
var employee_1, employee_id, response, err_1, detail;
if (withPunches === void 0) { withPunches = false; }
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4, , 5]);
return [4 /*yield*/, api.getEmployee(identifier)];
case 1:
employee_1 = _a.sent();
if (!withPunches) return [3 /*break*/, 3];
employee_id = employee_1.employee_id;
return [4 /*yield*/, api.getTimePunchesReport(null, employee_id)];
case 2:
response = _a.sent();
if (response.length)
employee_1 = response[0];
_a.label = 3;
case 3:
if (!isAborted.current) {
setEmployee(employee_1);
}
return [3 /*break*/, 5];
case 4:
err_1 = _a.sent();
if (!isAborted.current) {
detail = (0, utils_1.handleRespError)(err_1).detail;
if (detail === null || detail === void 0 ? void 0 : detail.includes('does not exist')) {
detail = 'Employee not found. Please try again.';
}
setError(detail || 'Unknown erorr');
}
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}
});
});
}, [api]);
var reset = (0, react_1.useCallback)(function () {
setEmployee(null);
setError(null);
}, []);
return (0, react_1.useMemo)(function () { return ({ employee: employee, error: error, fetch: fetch, reset: reset }); }, [employee, error, fetch, reset]);
};
exports.default = useEmployee;