phx-react
Version:
PHX REACT
152 lines • 7.22 kB
JavaScript
import { EMPTY_VALUE } from '../../../..';
import { eventBus } from '../../utils/eventBus';
import { IN_BOOKSHELF_STATE_CODE, IN_WAREHOUSE_STATE_CODE, KEY_ALERT_LIBRARY, KEY_GIAO_VIEN_CODE, KEY_HOC_SINH_CODE, KEY_NHAN_VIEN_CODE, VALID_BOOK_CODE_LENGTH, } from './constants';
export const getValidBookCodeLength = (prefixCode) => prefixCode.length + VALID_BOOK_CODE_LENGTH;
export const getCurrentDateTime = (date = new Date()) => {
const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
const year = date.getFullYear();
const hour = date.getHours();
const minute = date.getMinutes() > 9 ? date.getMinutes() : `0${date.getMinutes()}`;
const second = date.getSeconds() > 9 ? date.getSeconds() : `0${date.getSeconds()}`;
return `${day}/${month}/${year} ${hour}:${minute}:${second}`;
};
export const getRoleName = (userRoles) => {
var _a, _b;
try {
if (!userRoles)
return EMPTY_VALUE;
// Nếu có role học sinh thì trả luôn là Học sinh
const studentRoleName = (_b = (_a = userRoles === null || userRoles === void 0 ? void 0 : userRoles.filter((item) => (item === null || item === void 0 ? void 0 : item.role.role_code) === KEY_HOC_SINH_CODE)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.role.name;
if (studentRoleName)
return studentRoleName;
const StaffAndTeacherRoleNameArray = [];
userRoles === null || userRoles === void 0 ? void 0 : userRoles.forEach((item) => {
const roleCode = item.role.role_code;
if (roleCode === KEY_NHAN_VIEN_CODE || roleCode === KEY_GIAO_VIEN_CODE) {
const roleName = item.role.name;
StaffAndTeacherRoleNameArray.push(roleName);
}
});
return StaffAndTeacherRoleNameArray.join(', ') || EMPTY_VALUE;
}
catch (e) {
console.log(e);
return EMPTY_VALUE;
}
};
export const getProfile = (user) => {
const validProfile = ['profile_staff', 'profile_student', 'profile_teacher']
.map((key) => user === null || user === void 0 ? void 0 : user[key])
.find((profile) => profile && (profile === null || profile === void 0 ? void 0 : profile.user_code) !== null);
return validProfile;
};
export const fomartBookCode = (code, qrPrefix) => {
if (!qrPrefix || !code)
return '';
if ((code === null || code === void 0 ? void 0 : code.length) > getValidBookCodeLength(qrPrefix))
return code;
let formatedCode = code.toUpperCase().replace(/P{2,}/g, '');
const splitCode = code.split('-');
if (splitCode.length < 2)
return formatedCode;
formatedCode = `${qrPrefix}-${splitCode[1]}`;
return qrPrefix ? formatedCode.slice(0, getValidBookCodeLength(qrPrefix)) : '';
};
export const processBorrowBook = (arrBook, listBookState) => {
const inBookshelfBookArray = [];
const inWarehouseBookArray = [];
const returnBorrowBook = [];
const returnAllTicketIdArray = [];
try {
// nếu tổng số sách đã quét của phiếu bằng tổng số sách mượn của phiếu thì phiếu đó sẽ được trả hết
// => chuyển trạng thái phiếu sang đã trả, push ticketId vào mảng returnAllTicketIdArray
// lưu sách mượn vào mảng returnBookArray để trả từng quyển
arrBook.forEach((item) => {
const { listborrowBook, ticketId, totalBorrowed, totalReturned } = item;
// nếu tổng số sách được quét của 1 vé mượn = tổng số sách mượn - tổng sách đã trả thì phiếu đó sẽ được chuyển sang đã trả
if (listborrowBook.length === totalBorrowed - totalReturned) {
returnAllTicketIdArray.push(ticketId);
}
listborrowBook.forEach((borrowBook) => {
var _a, _b;
const { bookBorrowId, bookId, preStateId } = borrowBook;
if (preStateId === ((_a = listBookState.find((state) => state.code === IN_WAREHOUSE_STATE_CODE)) === null || _a === void 0 ? void 0 : _a.id)) {
inWarehouseBookArray.push(bookId);
}
else if (preStateId === ((_b = listBookState.find((state) => state.code === IN_BOOKSHELF_STATE_CODE)) === null || _b === void 0 ? void 0 : _b.id)) {
inBookshelfBookArray.push(bookId);
}
else {
inBookshelfBookArray.push(bookId);
}
returnBorrowBook.push(bookBorrowId);
});
});
return {
inBookshelfBookArray,
inWarehouseBookArray,
returnBorrowBook,
returnAllTicketIdArray,
};
}
catch (e) {
console.log(e);
return {
inBookshelfBookArray,
inWarehouseBookArray,
returnBorrowBook,
returnAllTicketIdArray,
};
}
};
// hàm này trả ra mm/dd/yyyy cách ngày hiện tại daysOffset ngày
export function getDateOffsetByDays(daysOffset, type = 'default') {
const currentDate = new Date();
currentDate.setDate(currentDate.getDate() + daysOffset);
const day = currentDate.getDate();
const month = currentDate.getMonth() + 1;
const year = currentDate.getFullYear();
if (type === 'datePicker') {
return String(day).padStart(2, '0') + '-' + String(month).padStart(2, '0') + '-' + year;
}
return `${month}/${day}/${year}`;
}
export const sortClassroomByName = (arrayClass) => {
const newArrayClass = structuredClone(arrayClass);
newArrayClass === null || newArrayClass === void 0 ? void 0 : newArrayClass.sort((a, b) => {
var _a, _b;
// Tách số lớp và tên lớp
const matchA = (_a = a.name) === null || _a === void 0 ? void 0 : _a.match(/(\d+)(\D+)/);
const matchB = (_b = b.name) === null || _b === void 0 ? void 0 : _b.match(/(\d+)(\D+)/);
if (!matchA || !matchB)
return false;
// Lấy số lớp và tên lớp
const numberA = parseInt(matchA[1]);
const numberB = parseInt(matchB[1]);
const letterA = matchA[2];
const letterB = matchB[2];
// So sánh số lớp trước
if (numberA !== numberB) {
return numberA - numberB;
}
// Nếu số lớp bằng nhau thì so sánh tên lớp
return letterA.localeCompare(letterB);
});
return newArrayClass;
};
// chuyển data từ datepicker: từ dd/mm/yyy sang mm/dd/yyyy
export const formatDateMMDDYY = (date) => {
const arr = date.split('-');
return `${arr[1]}/${arr[0]}/${arr[2]}`;
};
export const triggerMessage = ({ message, type = 'custom', isFailure = false, }) => {
eventBus.emit(KEY_ALERT_LIBRARY, {
message,
show: true,
type,
isFailure,
});
};
export const dateStrToDate = (date, char = '/') => date ? date.slice(8, 10) + char + date.slice(5, 7) + char + date.slice(0, 4) : '';
//# sourceMappingURL=helper.js.map