Commit f5e31110 authored by Nicolas Frandeboeuf's avatar Nicolas Frandeboeuf Committed by Benjamin Bouvier
Browse files

Simplify import (fix #600)

parent 099bfffa
......@@ -7,74 +7,42 @@ import { translate as $t } from '../../../helpers';
const ImportModule = props => {
let fileNameInput = null;
let fileInput = null;
const handleImport = e => {
if (!fileInput.files || !fileInput.files.length) {
alert($t('client.settings.no_file_selected'));
e.preventDefault();
return;
}
let fileReader = new FileReader();
fileReader.onload = fileEvent => {
try {
props.importInstance(JSON.parse(fileEvent.target.result));
} catch (err) {
if (err instanceof SyntaxError) {
alert($t('client.settings.import_invalid_json'));
} else {
alert(`Unexpected error: ${err.message}`);
let filename = e.target.value.split('\\').pop();
if (window.confirm($t('client.settings.confirm_import', { filename }))) {
let fileReader = new FileReader();
fileReader.onload = fileEvent => {
try {
props.importInstance(JSON.parse(fileEvent.target.result));
} catch (err) {
if (err instanceof SyntaxError) {
alert($t('client.settings.import_invalid_json'));
} else {
alert(`Unexpected error: ${err.message}`);
}
}
}
};
fileReader.readAsText(fileInput.files[0]);
fileInput.value = '';
fileNameInput.value = '';
e.preventDefault();
};
};
const handleChange = e => {
fileNameInput.value = e.target.value;
};
fileReader.readAsText(e.target.files[0]);
}
let refFileNameInput = input => {
fileNameInput = input;
};
let refFileInput = input => {
fileInput = input;
e.target.value = '';
};
return (
<div className="input-group import-file">
<div>
<input
type="text"
className="form-control"
readOnly={ true }
ref={ refFileNameInput }
type="file"
className="hidden-file-input"
id="import"
onChange={ handleImport }
/>
<span className="input-group-btn">
<div className="btn btn-primary btn-file">
{ $t('client.settings.browse') }
<input
type="file"
name="importFile"
ref={ refFileInput }
onChange={ handleChange }
/>
</div>
</span>
<span className="input-group-btn">
<button
className="btn btn-primary"
onClick={ handleImport }>
{ $t('client.settings.go_import_instance') }
</button>
</span>
<label
htmlFor="import"
className="btn btn-primary">
{ $t('client.settings.go_import_instance') }
</label>
</div>
);
};
......
......@@ -665,9 +665,10 @@ button:focus, input:focus {outline:none !important}
border-radius: 0;
}
.import-file input {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
.hidden-file-input {
position: absolute;
top: -9999px;
left: -9999px;
}
@media screen and (min-width: 992px) {
......
......@@ -211,7 +211,7 @@ module.exports = {
import_instance: 'Import Kresus instance',
go_import_instance: 'Import',
import_instance_help: `This will import an existing instance, exported with the above button. It won't try to merge any data, so please ensure that your data is clean and delete any existing data with the DataBrowser, if needed.`,
no_file_selected: 'No file selected',
confirm_import: 'Are you sure you want to import this instance: %{filename}?',
import_invalid_json: 'The JSON file to import is not valid!',
title: 'Settings',
......
......@@ -211,7 +211,7 @@ module.exports = {
import_instance: 'Importer une instance',
go_import_instance: 'Importer',
import_instance_help: `Cela va importer une instance déjà existante, exportée à l'aide du bouton ci-dessus. Aucune donnée ne sera fusionnée avec les données existantes, il est donc nécessaire de vous assurer que vous n'avez pas déjà des données présentes ; si besoin est, vous pouvez supprimer des données existantes à l'aide de l'application DataBrowser.`,
no_file_selected: 'Aucun fichier sélectionné',
confirm_import: 'Êtes-vous certain de vouloir importer cette instance : %{filename} ?',
import_invalid_json: 'Le fichier JSON à importer est invalide !',
title: 'Paramètres',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment