phx-react
Version:
PHX REACT
169 lines • 7.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTargetSelect = void 0;
const tslib_1 = require("tslib");
const react_1 = require("react");
const constant_1 = require("../constant");
const mutation_1 = require("../graphql/mutation");
const query_1 = require("../graphql/query");
const get_object_type_1 = require("../ultis/get-object-type");
const getLoginInfo_1 = tslib_1.__importDefault(require("../../Func/getLoginInfo"));
const getSchoolYearV3_1 = tslib_1.__importDefault(require("../../Func/SchoolYearV3/getSchoolYearV3"));
const PHXGrpcClientV3_1 = require("../../Func/GRPC/PHXGrpcClientV3");
const useTargetSelect = () => {
var _a, _b;
const userInfo = (0, getLoginInfo_1.default)();
const schoolYear = (0, getSchoolYearV3_1.default)();
const schoolYearId = (_b = (_a = schoolYear === null || schoolYear === void 0 ? void 0 : schoolYear.current_school_year) === null || _a === void 0 ? void 0 : _a.schoolYear) === null || _b === void 0 ? void 0 : _b.id;
const [roles, setRoles] = (0, react_1.useState)([]);
const getDataTargetSubmit = (data, targetId) => {
const result = [];
data.forEach((item) => {
var _a;
const { filter, notifyWith, target } = item;
const roleId = (_a = roles.find((role) => role.role_code === target)) === null || _a === void 0 ? void 0 : _a.id;
if (!roleId && target !== constant_1.ERoleCode.DEPARTMENT)
return;
const isDepartmentOrStaff = target === constant_1.ERoleCode.DEPARTMENT || target === constant_1.ERoleCode.STAFF;
const isAll = filter.some((f) => f.filterBy === constant_1.FILTER_BY.ALL && isDepartmentOrStaff);
if (isAll) {
result.push({
role_id: roleId,
is_all: true,
notify_with: notifyWith,
object_type: (0, get_object_type_1.getObjectType)(target),
...(targetId ? { target_id: targetId } : {}),
});
return;
}
const submitItem = {
role_id: roleId,
is_all: false,
notify_with: notifyWith,
object_type: (0, get_object_type_1.getObjectType)(target),
target_educational_levels: { data: [] },
target_grades: { data: [] },
target_classrooms: { data: [] },
target_users: { data: [] },
target_departments: { data: [] },
...(targetId ? { target_id: targetId } : {}),
};
filter.forEach((f) => {
const { filterBy, filterValues } = f;
if (filterBy === constant_1.FILTER_BY.EDUCATIONAL_LEVEL) {
submitItem.target_educational_levels.data = filterValues.map((v) => ({
educational_level_id: v.id,
}));
}
if (filterBy === constant_1.FILTER_BY.GRADE) {
submitItem.target_grades.data = filterValues.map((v) => ({
grade_id: v.id,
}));
}
if (filterBy === constant_1.FILTER_BY.CLASSROOM) {
submitItem.target_classrooms.data = filterValues.map((v) => ({
classroom_id: v.id,
}));
}
if (filterBy === constant_1.FILTER_BY.DEPARTMENT) {
submitItem.target_departments.data = filterValues.map((v) => ({
department_id: v.id,
}));
}
if (filterBy === constant_1.FILTER_BY.CUSTOM_LIST) {
submitItem.target_users.data = filterValues.map((v) => ({
user_id: v.id,
}));
}
});
result.push(submitItem);
});
return result;
};
const handleInsertTarget = async ({ data }) => {
var _a;
const typeCode = data === null || data === void 0 ? void 0 : data.type_code;
const dataSubmit = {
school_id: userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id,
creator_id: userInfo === null || userInfo === void 0 ? void 0 : userInfo.id,
name: data.name,
school_year_id: schoolYearId,
target_by_roles: {
data: getDataTargetSubmit(data.target),
},
...(typeCode ? { type_code: typeCode } : {}),
};
const { data: { insert_user_segment_target: { returning }, }, } = await (0, PHXGrpcClientV3_1.PHXClientMutationV3)({
query: mutation_1.INSERT_TARGET_MUTATION,
variables: {
data: dataSubmit,
},
isDelay: false,
});
return { id: (_a = returning[0]) === null || _a === void 0 ? void 0 : _a.id };
};
const handleUpdateTarget = async (id, data) => {
if (!id)
return;
try {
await (0, PHXGrpcClientV3_1.PHXClientMutationV3)({
query: mutation_1.UPDATE_TARGET_BY_PK_MUTATION,
variables: {
id,
data: {
...data,
updated_at: 'now',
},
},
isDelay: false,
});
}
catch (e) {
console.error(e);
}
};
const handleUpdateTargetByRole = async ({ data, id }) => {
try {
const { target, ...rest } = data;
await (0, PHXGrpcClientV3_1.PHXClientMutationV3)({
query: mutation_1.UPDATE_TARGET_BY_ROLE_MUTATION,
variables: {
targetId: id,
targetByRoles: getDataTargetSubmit(target, id),
},
isDelay: false,
});
const typeCode = data === null || data === void 0 ? void 0 : data.type_code;
const payloadUpdateTarget = {
name: rest.name,
school_year_id: rest.school_year_id || schoolYearId,
...(typeCode ? { type_code: typeCode } : {}),
};
await handleUpdateTarget(id, payloadUpdateTarget);
}
catch (e) {
console.error(e);
}
};
const initData = async () => {
try {
const { data: { role }, } = await (0, PHXGrpcClientV3_1.PHXClientQueryV3)({
query: query_1.GET_LIST_ROLE_QUERY,
variables: {
schoolId: userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id,
},
isDelay: false,
});
setRoles(role);
}
catch (e) {
console.error(e);
}
};
(0, react_1.useEffect)(() => {
initData();
}, []);
return { handleInsertTarget, handleUpdateTargetByRole, handleUpdateTarget };
};
exports.useTargetSelect = useTargetSelect;
//# sourceMappingURL=use-target-select.js.map