import { Box, Container, Input, Text, useToast } from '@chakra-ui/react'; import { __ } from '@wordpress/i18n'; import React, { useRef, useState } from 'react'; import { LuFileJson } from 'react-icons/lu'; interface Props { handleFileUpload: any; } const DNDJson: React.FC = ({ handleFileUpload }) => { const [fileDNDInProgress, setFileDNDInProgress] = useState(false); const fileInputRef = useRef(null); const toast = useToast(); const handleFileOpenClick = () => { if (fileInputRef.current) { fileInputRef.current.click(); } }; const handleFileChange = (e: React.ChangeEvent) => { e.preventDefault(); if (e.target.files) { const selectedFile = e.target.files[0]; if (selectedFile.type !== 'application/json') { return toast({ title: __( 'Selected file must only be JSON', 'learning-management-system', ), status: 'error', isClosable: true, }); } // Might need an API call here handleFileUpload.mutateAsync(e.target.files[0]); setFileDNDInProgress(false); } }; const handleDragStart = (e: React.DragEvent) => { e.dataTransfer.setData('text/plain', ''); }; const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); setFileDNDInProgress(true); }; const handleDragLeave = (e: React.DragEvent) => { e.preventDefault(); setFileDNDInProgress(false); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); const selectedFile = e.dataTransfer.files[0]; if (selectedFile.type !== 'application/json') { setFileDNDInProgress(false); return toast({ title: __( 'Selected file must only be JSON', 'learning-management-system', ), status: 'error', isClosable: true, }); } // Might need an API call here handleFileUpload.mutateAsync(e.dataTransfer.files[0]); setFileDNDInProgress(false); }; return ( {__( `Click / Drag & Drop Your JSON File Here`, 'learning-management-system', )} ); }; export default DNDJson;