import { Alert } from '@mui/material'; import Button from '@mui/material/Button'; import FormControl from '@mui/material/FormControl'; import { Importer, TextImporterFactory } from '@wisemapping/mindplot'; 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?: null | string; }; export type CreateProps = { onClose: () => void; }; type ErrorFile = { error: boolean; message: string; } const defaultModel: ImportModel = { title: '' }; const ImportDialog = ({ onClose }: CreateProps): React.ReactElement => { const client: Client = useSelector(activeInstance); const [model, setModel] = React.useState(defaultModel); const [error, setError] = React.useState(); const [errorFile, setErrorFile] = React.useState({error: false, message: ''}); 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]; // Closure to capture the file information. reader.onload = (event) => { // Suggest file name ... const fileName = file.name; if (fileName) { const title = fileName.split('.')[0]; if (!model.title || 0 === model.title.length) { model.title = title; } } const extensionFile = file.name.split('.').pop(); const extensionAccept = ['wxml', 'mm']; if (!extensionAccept.includes(extensionFile)) { setErrorFile({ error: true, message: intl.formatMessage({ id: 'import.error-file', defaultMessage: 'You can import WiseMapping and Freemind maps to your list of maps. Select the file you want to import.' }) }); } model.contentType = 'application/xml' const fileContent = event?.target?.result; const mapConent: string = typeof fileContent === 'string' ? fileContent : fileContent.toString(); try { const importer: Importer = TextImporterFactory.create(extensionFile, mapConent) importer.import(model.title, model.description) .then(res => { model.content = res; setModel({ ...model }); }) } catch (e) { if (e instanceof Error) { setErrorFile({ error: true, message: e.message }); } } }; // Read in the image file as a data URL. reader.readAsText(file); } }; return (
{errorFile.error &&

{errorFile.message}

}
); }; export default ImportDialog;