LoadGame.js 963 B

123456789101112131415161718192021222324252627282930313233343536
  1. import React from "react";
  2. import { useDropzone } from "react-dropzone";
  3. const LoadGame = ({ onLoad = () => {} }) => {
  4. const onDrop = React.useCallback(
  5. (acceptedFiles) => {
  6. acceptedFiles.forEach((file) => {
  7. const reader = new FileReader();
  8. reader.onabort = () => console.log("file reading was aborted");
  9. reader.onerror = () => console.log("file reading has failed");
  10. reader.onload = () => {
  11. try {
  12. const result = JSON.parse(reader.result);
  13. onLoad(result);
  14. } catch (e) {
  15. console.log("File parsing failed", e);
  16. }
  17. };
  18. reader.readAsText(file);
  19. });
  20. },
  21. [onLoad]
  22. );
  23. const { getRootProps, getInputProps } = useDropzone({ onDrop });
  24. return (
  25. <div {...getRootProps()}>
  26. <input {...getInputProps()} />
  27. <p>Drag 'n' drop some files here, or click to select files</p>
  28. </div>
  29. );
  30. };
  31. export default LoadGame;