import DownloadIcon from '@mui/icons-material/GetApp'; import { Typography, Button, Box } from '@mui/material'; import { useRequest } from 'alova'; import { toast } from 'react-toastify'; import type { FileUploadConfig } from 'api/endpoints'; import type { AxiosPromise } from 'axios'; import type { FC } from 'react'; import { SingleUpload, useFileUpload } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; import * as EMSESP from 'project/api'; interface UploadFileProps { // TODO fileupload move to alova uploadGeneralFile: (file: File, config?: FileUploadConfig) => AxiosPromise; } const GeneralFileUpload: FC = ({ uploadGeneralFile }) => { const [uploadFile, cancelUpload, uploading, uploadProgress, md5] = useFileUpload({ upload: uploadGeneralFile }); const { send: getSettings, onSuccess: onSuccessGetSettings } = useRequest(EMSESP.getSettings(), { immediate: false }); const { send: getCustomizations, onSuccess: onSuccessgetCustomizations } = useRequest(EMSESP.getCustomizations(), { immediate: false }); const { send: getEntities, onSuccess: onSuccessGetEntities } = useRequest(EMSESP.getEntities(), { immediate: false }); const { send: getSchedule, onSuccess: onSuccessGetSchedule } = useRequest(EMSESP.getSchedule(), { immediate: false }); const { LL } = useI18nContext(); const saveFile = (json: any, endpoint: string) => { const a = document.createElement('a'); const filename = 'emsesp_' + endpoint + '.json'; a.href = URL.createObjectURL( new Blob([JSON.stringify(json, null, 2)], { type: 'text/plain' }) ); a.setAttribute('download', filename); document.body.appendChild(a); a.click(); document.body.removeChild(a); toast.info(LL.DOWNLOAD_SUCCESSFUL()); }; onSuccessGetSettings((event) => { saveFile(event.data, 'settings'); }); onSuccessgetCustomizations((event) => { saveFile(event.data, 'customizations'); }); onSuccessGetEntities((event) => { saveFile(event.data, 'entities'); }); onSuccessGetSchedule((event) => { saveFile(event.data, 'schedule'); }); const downloadSettings = async () => { await getSettings().catch((error) => { toast.error(error.message); }); }; const downloadCustomizations = async () => { await getCustomizations().catch((error) => { toast.error(error.message); }); }; const downloadEntities = async () => { await getEntities().catch((error) => { toast.error(error.message); }); }; const downloadSchedule = async () => { await getSchedule().catch((error) => { toast.error(error.message); }); }; return ( <> {!uploading && ( <> {LL.UPLOAD()} {LL.UPLOAD_TEXT()} )} {md5 !== '' && ( {'MD5: ' + md5} )} {!uploading && ( <> {LL.DOWNLOAD(0)} {LL.DOWNLOAD_SETTINGS_TEXT()} {LL.DOWNLOAD_CUSTOMIZATION_TEXT()}{' '} {LL.DOWNLOAD_SCHEDULE_TEXT()}{' '} )} ); }; export default GeneralFileUpload;