import Button from '@material-ui/core/Button'; import FormControl from '@material-ui/core/FormControl'; import React from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import { useMutation } from 'react-query'; import { useSelector } from 'react-redux'; import Client, { ErrorInfo } from '../../../../classes/client'; import { activeInstance } from '../../../../redux/clientSlice'; import Input from '../../../form/input'; import BaseDialog from '../base-dialog'; export type ImportModel = { title: string; description?: string; contentType?: string; content?: ArrayBuffer | null | string; } export type CreateProps = { onClose: () => void } const defaultModel: ImportModel = { title: '' }; const ImportDialog = ({onClose}: CreateProps) => { const client: Client = useSelector(activeInstance); const [model, setModel] = React.useState(defaultModel); const [error, setError] = React.useState(); const intl = useIntl(); const mutation = useMutation((model: ImportModel) => { return client.importMap(model); }, { onSuccess: (mapId: number) => { window.location.href = `/c/maps/${mapId}/edit`; }, onError: (error) => { setError(error); } } ); const handleOnClose = (): void => { onClose(); setModel(defaultModel); setError(undefined); }; const handleOnSubmit = (event: React.FormEvent): void => { event.preventDefault(); mutation.mutate(model); }; const handleOnChange = (event: React.ChangeEvent): void => { event.preventDefault(); const name = event.target.name; const value = event.target.value; setModel({ ...model, [name as keyof ImportModel]: value }); } const handleOnFileChange = (event: React.ChangeEvent) => { const files = event?.target?.files; const reader = new FileReader(); if (files) { const file = files[0]; let title = file.name; title = title.substring(0, title.lastIndexOf(".")); // Closure to capture the file information. reader.onload = (event) => { const fileContent = event?.target?.result; model.content = fileContent; // Suggest file name ... const fileName = file.name; if (fileName) { const title = fileName.split('.')[0] if (!model.title || 0 === model.title.length) { model.title = title; } } model.contentType = file.name.lastIndexOf(".wxml") != -1 ? "application/xml" : "application/freemind"; setModel({...model}); }; // Read in the image file as a data URL. reader.readAsText(file); } }; return (
); } export default ImportDialog;