import { ChangeEvent, useEffect,useRef, useState } from 'react'; import { Button, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, } from '@chakra-ui/react'; import { IoArrowDownCircleOutline } from '@react-icons/all-files/io5/IoArrowDownCircleOutline'; import { IoArrowUpCircleOutline } from '@react-icons/all-files/io5/IoArrowUpCircleOutline'; import { IoCheckmarkCircleOutline } from '@react-icons/all-files/io5/IoCheckmarkCircleOutline'; import { IoCloseCircleOutline } from '@react-icons/all-files/io5/IoCloseCircleOutline'; import { useQueryClient } from '@tanstack/react-query'; import { GoogleSheetState, OntimeRundown, ProjectData, UserFields } from 'ontime-types'; import { PROJECT_DATA, RUNDOWN, USERFIELDS } from '../../../common/api/apiConstants'; import { maybeAxiosError } from '../../../common/api/apiUtils'; import { getSheetsAuthUrl, getSheetSettings, getSheetstate, patchData, postPreviewSheet, postPushSheet, postSheetSettings, uploadSheetClientFile, } from '../../../common/api/ontimeApi'; import { projectDataPlaceholder } from '../../../common/models/ProjectData'; import { userFieldsPlaceholder } from '../../../common/models/UserFields'; import PreviewExcel from '../upload-modal/preview/PreviewExcel'; interface SheetsModalProps { onClose: () => void; isOpen: boolean; } export default function SheetsModal(props: SheetsModalProps) { const { isOpen, onClose } = props; const fileInputRef = useRef(null); const [rundown, setRundown] = useState(null); const [userFields, setUserFields] = useState(null); const [project, setProject] = useState(null); const [sheetState, setSheetState] = useState({ auth: false, id: false, worksheet: false }); const queryClient = useQueryClient(); const sheetid = useRef(null); const worksheet = useRef(null); const handleClose = () => { setRundown(null); setProject(null); setUserFields(null); onClose(); }; const handleClick = () => { fileInputRef.current?.click(); }; const handleFile = async (event: ChangeEvent) => { const selectedFile = event?.target?.files?.[0]; if (!selectedFile) { return; } else { await uploadSheetClientFile(selectedFile); _onChange(); } }; const _onChange = async () => { setSheetState(await getSheetstate()); }; useEffect(() => { getSheetSettings().then((data) => { if (sheetid.current?.value != data.id || worksheet.current?.value != data.worksheet) { _onChange(); } if (sheetid.current) { sheetid.current.value = data.id; } if (worksheet.current) { worksheet.current.value = data.worksheet; } }); }); const handelSave = () => { postSheetSettings({ id: sheetid.current?.value ?? '', worksheet: worksheet.current?.value ?? '' }).then((data) => { _onChange(); if (sheetid.current) { sheetid.current.value = data.id; } if (worksheet.current) { worksheet.current.value = data.worksheet; } }); }; const handleAuthenticate = () => { getSheetsAuthUrl().then((data) => { if (data != 'bad') { window.open(data, '_blank', 'noreferrer'); //TODO: can we detect when this window is closed } }); }; const handlePullData = () => { postPreviewSheet().then((data) => { setProject(data.project); setRundown(data.rundown); setUserFields(data.userFields); }); }; const handlePushData = () => { postPushSheet().then((data) => { console.log(data); }); }; const handleFinalise = async () => { // this step is currently only used for excel files, after preview if (rundown && userFields && project) { let doClose = false; try { await patchData({ rundown, userFields, project }); queryClient.setQueryData(RUNDOWN, rundown); queryClient.setQueryData(USERFIELDS, userFields); queryClient.setQueryData(PROJECT_DATA, project); await queryClient.invalidateQueries({ queryKey: [...RUNDOWN, ...USERFIELDS, ...PROJECT_DATA], }); doClose = true; } catch (error) { const message = maybeAxiosError(error); console.log(message); // setErrors(`Failed applying changes ${message}`); } finally { if (doClose) { handleClose(); } } } }; return ( Sheets! {rundown && ( <> )} {!rundown && ( <>
Need to add some help here
{sheetState.auth ?
You are authenticated
:
You are not authenticated
}
{sheetState.id ? : }
{sheetState.worksheet ? : }
)}
{!rundown && (
)} {!rundown && ( )} {rundown && ( )}
); }