phx-react
Version:
PHX REACT
61 lines • 3.62 kB
JavaScript
import { __awaiter, __generator, __rest } from "tslib";
import { useLazyQuery } from '@apollo/client';
import { useState, useEffect } from 'react';
import PHXFuncGetLoggedInfo from './getLoginInfo';
import { querySchoolYearHocvu } from '../../query/gql';
function PHXGetCurrentYearWithTermHocVu() {
var _this = this;
var schoolInfo = PHXFuncGetLoggedInfo();
var _a = useState(), currentYear = _a[0], setCurrentYear = _a[1];
var today = new Date();
var getSchoolYear = useLazyQuery(querySchoolYearHocvu, {
fetchPolicy: 'network-only',
notifyOnNetworkStatusChange: true
})[0];
useEffect(function () {
var getData = function () { return __awaiter(_this, void 0, void 0, function () {
var res, school_year, _a, school_terms, current_term, schoolYear, schoolTerm, currentMonth, monthBeforeActive, gteAndLte, res_1, school_year_1, _b, school_terms, current_term, schoolYear, schoolTerm, err_1;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 5, , 6]);
return [4 /*yield*/, getSchoolYear({
variables: { lte: today, gte: today, school_id: schoolInfo === null || schoolInfo === void 0 ? void 0 : schoolInfo.school_id }
})];
case 1:
res = _c.sent();
school_year = res.data.school_year;
if (!(school_year.length > 0)) return [3 /*break*/, 2];
_a = school_year[0] ? school_year[0] : {}, school_terms = _a.school_terms, current_term = _a.current_term, schoolYear = __rest(_a, ["school_terms", "current_term"]);
schoolTerm = current_term ? current_term[0] : {};
setCurrentYear({ current_school_year: { schoolYear: schoolYear, school_terms: school_terms }, current_school_term: schoolTerm });
return [3 /*break*/, 4];
case 2:
currentMonth = today.getMonth();
monthBeforeActive = currentMonth - 3;
gteAndLte = new Date(today.getFullYear(), monthBeforeActive, today.getDate());
return [4 /*yield*/, getSchoolYear({
variables: { lte: gteAndLte, gte: gteAndLte, school_id: schoolInfo === null || schoolInfo === void 0 ? void 0 : schoolInfo.school_id }
})];
case 3:
res_1 = _c.sent();
school_year_1 = res_1.data.school_year;
_b = school_year_1[0] ? school_year_1[0] : {}, school_terms = _b.school_terms, current_term = _b.current_term, schoolYear = __rest(_b, ["school_terms", "current_term"]);
schoolTerm = current_term ? current_term[0] : {};
setCurrentYear({ current_school_year: { schoolYear: schoolYear, school_terms: school_terms }, current_school_term: schoolTerm });
_c.label = 4;
case 4: return [3 /*break*/, 6];
case 5:
err_1 = _c.sent();
console.log(err_1);
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
}); };
getData();
}, []);
return currentYear;
}
export default PHXGetCurrentYearWithTermHocVu;
//# sourceMappingURL=getSchoolYearHocVu.js.map