UNPKG

phx-react

Version:

PHX REACT

78 lines 4.93 kB
"use strict"; 'use client'; exports.__esModule = true; exports.PHXPagePermission = void 0; var tslib_1 = require("tslib"); var solid_1 = require("@heroicons/react/24/solid"); var Card_1 = require("../Card"); var react_1 = tslib_1.__importStar(require("react")); var gql_1 = require("../../query/gql"); var client_1 = require("@apollo/client"); var js_cookie_1 = tslib_1.__importDefault(require("js-cookie")); var constants_1 = require("../../utils/constants"); function PHXPagePermission(_a) { var _this = this; var children = _a.children, code = _a.code; var _b = (0, react_1.useState)([]), perCode = _b[0], setPerCode = _b[1]; var cookie = js_cookie_1["default"].get(constants_1.isLoggedCookie); var getDataPermission = (0, client_1.useLazyQuery)(gql_1.GET_PERMISSION_DETAIL_USER, { variables: { id: '' }, fetchPolicy: 'network-only' })[0]; (0, react_1.useEffect)(function () { var _a; if (cookie) { var data = JSON.parse(cookie); var allRoleCodes_1 = (_a = data === null || data === void 0 ? void 0 : data.user_roles) === null || _a === void 0 ? void 0 : _a.map(function (userRole) { var _a; return (_a = userRole === null || userRole === void 0 ? void 0 : userRole.role) === null || _a === void 0 ? void 0 : _a.role_code; }); var id_1 = data.id, school_id_1 = data.school_id; var fetchDataPer = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var res, userPermission, rolePermission, dataCodePermissionRole, dataCodePermissionUser, allCodes, uniqueValues, error_1; var _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 2, , 3]); return [4 /*yield*/, getDataPermission({ variables: { id: id_1, code: allRoleCodes_1, school_id: school_id_1 } })]; case 1: res = _c.sent(); userPermission = (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.permission_user; rolePermission = (_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.permission_role; dataCodePermissionRole = rolePermission === null || rolePermission === void 0 ? void 0 : rolePermission.map(function (item) { var _a; return (_a = item === null || item === void 0 ? void 0 : item.permission) === null || _a === void 0 ? void 0 : _a.code; }); dataCodePermissionUser = userPermission === null || userPermission === void 0 ? void 0 : userPermission.map(function (item) { var _a; return (_a = item === null || item === void 0 ? void 0 : item.permission) === null || _a === void 0 ? void 0 : _a.code; }); allCodes = tslib_1.__spreadArray(tslib_1.__spreadArray([], dataCodePermissionRole, true), dataCodePermissionUser, true); uniqueValues = Array.from(new Set(allCodes)); setPerCode(uniqueValues); return [3 /*break*/, 3]; case 2: error_1 = _c.sent(); console.error('Error fetching school data:', error_1); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; fetchDataPer()["catch"](console.error); } }, []); if (perCode.length > 0) { var checkPermission = perCode.some(function (item) { return item === code; }); if (checkPermission) { return react_1["default"].createElement(react_1["default"].Fragment, null, children); } else { return (react_1["default"].createElement("div", { className: 'mt-5' }, react_1["default"].createElement(Card_1.PHXCard, null, react_1["default"].createElement("div", { className: 'mb-10 mt-10 sm:mb-20 sm:mt-20 lg:mb-32 lg:mt-32' }, react_1["default"].createElement("div", { className: 'flex items-center justify-center' }, react_1["default"].createElement(solid_1.LockClosedIcon, { className: 'text-gray-400', height: 70, width: 70 })), react_1["default"].createElement("div", { className: 'mt-5 flex items-center justify-center text-base font-bold text-gray-800' }, "B\u1EA1n kh\u00F4ng c\u00F3 quy\u1EC1n truy c\u1EADp"))))); } } return null; } exports.PHXPagePermission = PHXPagePermission; //# sourceMappingURL=PagePermission.js.map