phx-react
Version:
PHX REACT
67 lines • 4.19 kB
JavaScript
'use client';
import { __awaiter, __generator, __spreadArray } from "tslib";
import { LockClosedIcon } from '@heroicons/react/24/solid';
import { PHXCard } from '../Card';
import React, { useEffect, useState } from 'react';
import Cookies from 'js-cookie';
import { isLoggedCookie } from '../../utils/constants';
import PHXClientQueryV3 from '../Func/clientQueryV3';
import { GET_PERMISSION_DETAIL_USER_V3 } from '../../query';
export function PHXPagePermissionV3(_a) {
var _this = this;
var children = _a.children, code = _a.code;
var _b = useState([]), perCode = _b[0], setPerCode = _b[1];
var cookie = Cookies.get(isLoggedCookie);
useEffect(function () {
if (cookie) {
var data = JSON.parse(cookie);
var id_1 = data.id, school_id_1 = data.school_id;
var fetchDataPer = function () { return __awaiter(_this, void 0, void 0, function () {
var res, userPermission, rolePermission, dataCodePermissionRole, dataCodePermissionUser, allCodes, uniqueValues, error_1;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
return [4 /*yield*/, PHXClientQueryV3({
query: GET_PERMISSION_DETAIL_USER_V3,
variables: { id: id_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 = __spreadArray(__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.createElement(React.Fragment, null, children);
}
else {
return (React.createElement("div", { className: 'mt-5' },
React.createElement(PHXCard, null,
React.createElement("div", { className: 'mb-10 mt-10 sm:mb-20 sm:mt-20 lg:mb-32 lg:mt-32' },
React.createElement("div", { className: 'flex items-center justify-center' },
React.createElement(LockClosedIcon, { className: 'text-gray-400', height: 70, width: 70 })),
React.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;
}
//# sourceMappingURL=PagePermissionV3.js.map