import { ChangeEvent, useCallback, useContext, useRef, useState } from 'react'; import { Button } from '@chakra-ui/button'; import { Checkbox, FormControl, FormErrorMessage, FormHelperText, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, Progress, } from '@chakra-ui/react'; import { IoCloseSharp } from '@react-icons/all-files/io5/IoCloseSharp'; import { useQueryClient } from '@tanstack/react-query'; import { EVENTS_TABLE } from '../../api/apiConstants'; import { uploadEvents } from '../../api/ontimeApi'; import { LoggingContext } from '../../context/LoggingContext'; import TooltipActionBtn from '../buttons/TooltipActionBtn'; import { validateFile } from './utils'; import style from './UploadModal.module.scss'; interface UploadModalProps { onClose: () => void; isOpen: boolean; } export default function UploadModal({ onClose, isOpen }: UploadModalProps) { const queryClient = useQueryClient(); const { emitError } = useContext(LoggingContext); const [errors, setErrors] = useState([]); const [file, setFile] = useState(null); const [progress, setProgress] = useState(0); const overrideOptionRef = useRef(null); const handleFile = useCallback((event: ChangeEvent) => { const fileUploaded = event?.target?.files?.[0]; if (!fileUploaded) return; const validate = validateFile(fileUploaded); setErrors(validate.errors); if (validate.isValid) { setFile(fileUploaded); } else { setFile(null); } }, []); const handleUpload = useCallback(async () => { if (file) { try { await uploadEvents(file, setProgress, { onlyEvents: overrideOptionRef?.current?.checked }); } catch (error) { emitError(`Failed uploading file: ${error}`); } finally { await queryClient.invalidateQueries(EVENTS_TABLE); setFile(null); } } }, [emitError, file, queryClient]); return ( File upload 0}> Select file to upload {errors.length === 0 ? ( .XLSX .JSON with max 1MB ) : ( {errors.map((error) => ( {error} ))} )}
Options Import only events This will prevent overriding user settings
{file && (
File ready to upload setFile(null)} tooltip='Cancel' aria-label='Cancel' className={style.corner} size='sm' variant='ghosted' icon={} />
  • {file.name}
  • {`${(file.size / 1024).toFixed(2)}kb`}
  • {file.type}
)}
); }