import { toast } from 'react-toastify'; import DialogTitle from '@mui/material/DialogTitle'; import DialogContent from '@mui/material/DialogContent'; import DialogContentText from '@mui/material/DialogContentText'; import DialogActions from '@mui/material/DialogActions'; import { Button, Grid, TextField, Typography, InputLabel, FormControl } from '@mui/material'; import Dialog from '@mui/material/Dialog'; import * as React from 'react'; import Autocomplete from '@mui/material/Autocomplete'; import {useEffect, useRef, useState} from 'react'; import { format } from 'date-fns'; import { GridOverlay } from '@mui/x-data-grid'; import axios from 'axios'; import {apiPath, getUserData} from '../auth/utils'; import {GENERAL_RED_COLOR, GENERAL_TEXT_COLOR} from '../themes/colorConst'; // import { Route } from '@mui/icons-material'; import { MuiFileInput } from 'mui-file-input'; import { POST_THUMBNAIL_PATH } from './ApiPathConst'; import { useForm } from 'react-hook-form'; import {isObjEmpty} from "./Utils"; import AttachFileIcon from '@mui/icons-material/AttachFile' import CloudUploadIcon from '@mui/icons-material/CloudUpload'; //====================COMMON FUNCTION====================// export function getUserAuthList() { const abilities = localStorage.getItem('abilities'); if (abilities !== null) { const abilitiesList = abilities.split(','); return abilitiesList; } return []; } export function handleRouteAbility(canAccess, targetPage, redirectPage) { return canAccess ? targetPage : redirectPage; } export function readFile(input, curBlob, setCurBlob) { let reader = new FileReader(); //console.log(input); reader.readAsArrayBuffer(input.data); reader.onload = function () { //console.log(reader.result); let temp = curBlob; temp[input.id] = reader.result; setCurBlob(temp); }; } export function convertXmlToObject(xmlArray) { let output = []; for (let i = 0; i < xmlArray.length; i++) { let tempObj = {}; const keys = Object.keys(xmlArray[i]); const values = Object.values(xmlArray[i]); tempObj['id'] = i; tempObj['key'] = i; let userType = ''; let userFirstName = ''; let userLastName = ''; //let userNameZh = ""; for (let j = 0; j < Object.keys(xmlArray[i]).length; j++) { if (values[j][0] === 'NULL') { tempObj[keys[j]] = null; } else { tempObj[keys[j]] = values[j][0]; } // if(keys[j] === "EMSDknownas"){ // userNameZh = values[j][0]; // } if (keys[j] === 'EMSDlotus') { userType = values[j][0]; } if (keys[j] === 'EMSDlastname') { userLastName = values[j][0]; } if (keys[j] === 'EMSDfirstname') { userFirstName = values[j][0]; } } let displayLabel = userFirstName + ' ' + userLastName + ' (' + userType + ')'; tempObj['label'] = displayLabel; if(displayLabel.trim() !== "()" && displayLabel.trim().length > 3){ output.push(tempObj); } } return output; } //Get user added record by compare with the original data export function getDeletedRecordWithRefList(referenceList, updatedList) { return referenceList.filter((x) => !updatedList.includes(x)); } //Get user deleted record by compare with the original data export function getNewRecordWithRefList(referenceList, updatedList) { return updatedList.filter((x) => !referenceList.includes(x)); } //Get single combo value by using of label export function getComboValueByLabel(comboList, input) { for (let i = 0; i < comboList.length; i++) { if (comboList[i].label === input) { return comboList[i]; } } return (input === undefined || input === "") ? null : input; } export function getComboValueById(comboList, input) { for (let i = 0; i < comboList.length; i++) { if (comboList[i].id === input) { return comboList[i]; } } return input === undefined ? null : input; } export const getRowHeight = (params, fieldName, lengthToSplit) => { let temp = params.model[fieldName]; //console.log(temp); if(!isObjEmpty(temp)){ const SINGLE_LINE_HEIGHT = 40; const HEIGHT_WITH_PADDING = 35; const LINE_HEIGHT = 25; // Adjust this value based on your font size and row padding const numberOfLines = Math.ceil(temp.length / lengthToSplit); // Change 50 to your desired line length const height = numberOfLines < 2 ? SINGLE_LINE_HEIGHT : HEIGHT_WITH_PADDING*2 + (LINE_HEIGHT * (numberOfLines-2) ); //console.log(height); return height <= SINGLE_LINE_HEIGHT ? SINGLE_LINE_HEIGHT : height; } else { return 40; } }; //Get multi combo value by using of id export function getComboValueByIdList(comboList, idList) { if (!isObjEmpty(idList) && idList !== undefined) { const output = idList.map(function (id) { for (let i = 0; i < comboList.length; i++) { if (comboList[i].id === id) { return comboList[i]; } } }) .filter(Boolean); // Filter out undefined values from the output array return output; } return []; } //Check if user subDivisionId in List export function isUserDivisionIdInList(input, idList) { if (idList.length < 1) { return false; } for (let i = 0; i < idList.length; i++) { if (idList[i] === parseInt(input)) { return true; } } return false; } //Format List to String by using comma export function castListToString(input) { let output = ''; for (let i = 0; i < input.length; i++) { if (i !== 0) { output = output + ','; } output = output + input[i]; } return output; } //Convert Object list to id list export function getIdList(input) { const output = input.map(function (obj) { return obj.id; }); return output; } //Convert Object list to JSON Object id List export function getJSONIdList(data) { return data.map(({ id }) => ({ id })); } //Convert Object list to field list export function getListByFieldName(input, name) { const output = input.map(function (obj) { return obj[name]; }); return output; } export function isStringEmptyAfterTrim(data){ return (!data || data.trim().length === 0) ; } export function isFormEmpty(data){ return (Object.values(data).every(value => !value)) ; } export function trimListDataBeforePost(dataList) { return dataList.map((dataObject) => { for (let key in dataObject) { if (typeof dataObject[key] === 'string') { dataObject[key] = dataObject[key].trim(); } } return dataObject; }); } export function trimDataBeforePost(dataObject){ for (let key in dataObject) { if (typeof dataObject[key] === 'string') { dataObject[key] = dataObject[key].trim(); } } return dataObject; } export function getObjectByValueName(list, valueName, value) { const obj = list.find((element) => { return element[valueName] === value; }); return obj === undefined || Object.keys(obj).length <= 0 ? null : obj; } export function getObjectByName(list, name) { const obj = list.find((element) => { return element.name === name; }); return obj === undefined || Object.keys(obj).length <= 0 ? null : obj; } export function getObjectById(list, id) { const obj = list.find((element) => { return element.id === id; }); return obj === undefined || Object.keys(obj).length <= 0 ? null : obj; } export function removeObjectWithId(arr, id) { const arrCopy = Array.from(arr); const objWithIdIndex = arrCopy.findIndex((obj) => obj.id === id); arrCopy.splice(objWithIdIndex, 1); return arrCopy; } /** * Display the date array in string format, default format is 'yyyy-MM-dd'. * * @param {*} dateInput - The date value to be displayed, can be array or timestamp. * @param {*} [formatOption] - Set to True to display also the timestamp, or input custom format text. */ export function getDateString(dateInput, formatOption=false) { if (dateInput === null || dateInput === undefined) { return null; } let dateFormat = 'yyyy-MM-dd'; if (formatOption==true) { dateFormat = 'yyyy-MM-dd HH:mm:ss'; } else if (typeof(formatOption) == 'string') { dateFormat = formatOption; } let inputType = 0; // Array if (!Array.isArray(dateInput)) { inputType = 1; } let date = new Date(); if (inputType == 0) { let queryDateArray = dateInput; date = new Date( queryDateArray[0], queryDateArray[1]-1, queryDateArray[2], queryDateArray[3] !== undefined ? queryDateArray[3] : 0, queryDateArray[4] !== undefined ? queryDateArray[4] : 0, queryDateArray[5] !== undefined ? queryDateArray[5] : 0 ); } else { date = new Date(dateInput); } return format(date, dateFormat); } export const isOptionEqualToValue = (option, value) => { return option.id === value?.id; }; export const dateComparator = (date1, date2) => { const dateString1 = getDateString(date1,false); const dateString2 = getDateString(date2,false); const date1Obj = new Date(dateString1); const date2Obj = new Date(dateString2); if (date1Obj < date2Obj) { return -1; } if (date1Obj > date2Obj) { return 1; } return 0; }; //====================COMMON NOTIFICATION====================// export const notifySaveSuccess = () => toast.success('Save success!', { position: 'bottom-left', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: 'light' }); export const notifySendSuccess = () => toast.success('Send success!', { position: 'bottom-left', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: 'light' }); export const notifyLoadSuccess = () => toast.success('Load success!', { position: 'bottom-left', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: 'light' }); export const notifyDeleteSuccess = () => toast.success('Delete Success!', { position: 'bottom-left', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: 'light' }); export const notifyInfo = (infoString) => toast.info(infoString, { position: 'bottom-left', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: 'light' }); export const notifySuccess = (infoString) => toast.success(infoString, { position: 'bottom-left', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: 'light' }); export const notifyDeleteError = (errorString) => toast.error(errorString, { position: 'bottom-left', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: 'light' }); export const notifyNoPermission = () => toast.error('You do not have permission to access this page', { position: 'bottom-left', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: 'light' }); //====================COMMON COMPONENT====================// export function CustomNoRowsOverlay() { return ( No record found ); } export function GeneralConfirmWindow({ isWindowOpen, title, content, onNormalClose, onConfirmClose }) { return ( {title} {content} Cancel Confirm ); } export function GeneralComboWindow({ isWindowOpen, title, content, comboList, setValueCallback, onNormalClose, onConfirmClose }) { const [errors, setErrors] = useState({}); const [temp, setTemp] = useState(null); function validate() { const formErrors = {}; if (temp == null) { formErrors.error = 'Event must not be null'; } setErrors(formErrors); if (Object.keys(formErrors).length === 0) { onConfirmClose(); } } return ( {title} {content} * { setValueCallback(newValue); setTemp(newValue); }} renderInput={(params) => ( )} /> Cancel Confirm ); } export function GeneralTwoComboWindow({ isWindowOpen, title, content, content2, comboList, comboList2, setValueCallback, setValueCallback2, onNormalClose, onConfirmClose }) { const [errors, setErrors] = useState({}); const [temp, setTemp] = useState(null); const [temp2, setTemp2] = useState(null); function validate() { const formErrors = {}; if (temp === null) { formErrors.error = 'Event must not be null'; } if (temp2 === null) { formErrors.error2 = 'Application must not be null'; } setErrors(formErrors); if (Object.keys(formErrors).length === 0) { onConfirmClose(); } } useEffect(() => { setValueCallback2(null); setTemp2(null); }, [temp]); return ( {title} {content} * { setValueCallback(newValue); setTemp(newValue); }} renderInput={(params) => ( )} /> {content2} * { setValueCallback2(newValue); setTemp2(newValue); }} value={temp2 === null? null : temp2} disabled={temp === null} renderInput={(params) => ( )} /> Cancel Confirm ); } export function GeneralCreateTemplateWindow({ isWindowOpen, title, content, setValueCallback, validatePath, onNormalClose, onConfirmClose, module }) { const [errors, setErrors] = useState({}); const [temp, setTemp] = useState(null); function validateTemplateName() { const userData = getUserData(); axios .get(`${apiPath}${validatePath}`, { params: { name: temp.trim(), userId: userData.id, module: module } }) .then((response) => { if (response.status === 200) { const formErrors = {}; if (response.data.isTaken) { formErrors.error = 'Name have been taken'; setErrors(formErrors); } else if (!response.data.isTaken) { if (Object.keys(formErrors).length === 0) { onConfirmClose(); } } } }) .catch((error) => { console.log(error); return true; }); } function validate() { const formErrors = {}; if (temp === null) { formErrors.error = 'Template name must not be null'; } else if (temp.trim().length === 0){ formErrors.error = 'Template name must not be null'; } setErrors(formErrors); if (Object.keys(formErrors).length === 0) { validateTemplateName(); } } const onInputChange = (event) => { setTemp(event.target.value); setValueCallback(event.target.value); }; return ( {title} {content} * Cancel Confirm ); } export function UploadFileWindow({ isWindowOpen, title, video, onNormalClose, onConfirmClose }) { const [errors, setErrors] = useState({}); const [file, setFile] = React.useState(null) const fileInputRef = useRef(null); const { register, getValues, setValue } = useForm(); useEffect(() => { setValue('remarks', video.remarks); }, [video]); const handleOnClose = () => { setFile(null); onNormalClose(); }; const handleFieldChange = (newFile) => { setFile(newFile); }; const handleChange = (event) => { if(event !== null){ const newFile = event.target.files[0]; setFile(newFile); } else{ setFile(null) } }; const handleOpenFileSelect = () => { fileInputRef.current.click(); }; function validate() { const formErrors = {}; /*if (file == null) { formErrors.error = 'File must not be null'; }*/ if (file !== null) { if (file.type.split('/')[0] !== 'image') { formErrors.error = 'File must be a image'; } } setErrors(formErrors); if (Object.keys(formErrors).length === 0) { uploadData(); } } function uploadData() { const values = getValues(); let formData = new FormData(); let containData = false; formData.append('fileId', video.id); if (file !== null) { formData.append('file', file); containData = true; } if (values.remarks) { formData.append('remarks', values.remarks); containData = true; } if (containData) { axios .post(`${apiPath}${POST_THUMBNAIL_PATH}`, formData, { headers: { 'Content-Type': 'multipart/form-data' } }) .then((response) => { if (response.status === 200) { notifySaveSuccess(); setFile(null); onConfirmClose(); } }) .catch((error) => { console.log(error); return false; }); } } return ( {title} File Name: {video.key} {video.mimetype === 'video' ? ( Thumbnail: } inputprops={{ accept: '.png, .jpeg, .jpg', startAdornment: }} onClick={handleOpenFileSelect} > Upload file {/*Selected file: {file ? file.name : 'None'}*/} ) : ( )} Remark: Cancel Confirm ); }