diff --git a/src/components/btns/import-route/ImportRoute.js b/src/components/btns/import-route/ImportRoute.js index 4827337..a4e46f5 100644 --- a/src/components/btns/import-route/ImportRoute.js +++ b/src/components/btns/import-route/ImportRoute.js @@ -50,6 +50,7 @@ export class ImportRoute extends Component { showPreviews={true} showPreviewsInDropzone={false} maxFileSize={5000000} + filesLimit={10} clearOnUnmount={true} onSave={(files) => this.handleSave(files)} onClose={this.handleClose.bind(this)} diff --git a/src/components/containers/importrouteform/ImportRouteForm.js b/src/components/containers/importrouteform/ImportRouteForm.js index 6bb408d..d4b2bd8 100644 --- a/src/components/containers/importrouteform/ImportRouteForm.js +++ b/src/components/containers/importrouteform/ImportRouteForm.js @@ -5,6 +5,8 @@ import { withStyles, Typography, Paper, Grid, Button } from '@material-ui/core'; import Route from '../../../entities/Route'; import TrackPoint from '../../../entities/TrackPoint'; import ImportRouteCard from '../../ui/ImportRouteCard'; +import { parseGpxToRoutes } from '../../../parser/ParserGpxToRoute'; +import ParserJsonLdToRoute from '../../../parser/ParserJsonLdToRoute'; export class ImportRouteForm extends Component { @@ -12,16 +14,76 @@ export class ImportRouteForm extends Component { super(props); // console.log(this.props.location.routes); this.state = { - // routes: this.props.location.routes, - routes: [new Route("San Silvestre", "Esto es una ruta de prueba", [new TrackPoint(43, 5, 10), new TrackPoint(47, 8, 10)], null, null, null, null, null), - new Route("Recorrido por Oviedo", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", [new TrackPoint(41, 5, 10), new TrackPoint(43, 8, 10)], null, null, null, null, null)] + files: this.props.location.routes, + routes: [] + // routes: [new Route("San Silvestre", "Esto es una ruta de prueba", [new TrackPoint(43, 5, 10), new TrackPoint(47, 8, 10)], null, null, null, null, null), + // new Route("Recorrido por Oviedo", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", [new TrackPoint(41, 5, 10), new TrackPoint(43, 8, 10)], null, null, null, null, null)] } + } + } + componentDidMount() { + if (!this.state.files) { + this.props.history.push('/404'); + return; } + this.handleFiles(); + } + + handleFiles() { + let re = /(?:\.([^.]+))?$/; + + this.state.files.forEach((file) => { + + const fileReader = new FileReader(); + + fileReader.onerror = () => alert("ERROR IMPORTING ROUTE"); + fileReader.onabort = () => alert("ABORT IMPORTING ROUTE"); + fileReader.onload = () => { + const content = fileReader.result; + try { + let ext = re.exec(file.name)[0]; + if (ext === '.gpx') { + this.handleGPX(content); + } else if (ext === '.jsonld' || ext === '.json') { + this.handleJSON(content); + } + } catch (error) { + alert(error); + } + }; + fileReader.readAsText(file); + }); + } + + handleGPX(file) { + let routesList = []; + parseGpxToRoutes(file, function (routeArray) { + routeArray.forEach((route) => { + routesList.push(route); + }); + }); + + routesList.forEach((r) => { + this.state.routes.push(r); + }); + + const { routes } = this.state; + this.setState({ routes: routes.slice() }); + } + + handleJSON(file) { + let parser = new ParserJsonLdToRoute(); + let route = parser.parse(file); + this.state.routes.push(route); + const { routes } = this.state; + this.setState({ routes: routes.slice() }); } render() { const { classes } = this.props; + const { files } = this.state; const { routes } = this.state; + return (
{point.getName()}
Latitude: {point.getLatitude()}
Longitude: {point.getLongitude()}
Elevation: {point.getElevation()}
diff --git a/src/components/ui/ImportRouteCard.js b/src/components/ui/ImportRouteCard.js index 357677a..c456e53 100644 --- a/src/components/ui/ImportRouteCard.js +++ b/src/components/ui/ImportRouteCard.js @@ -38,12 +38,20 @@ function ImportRouteCard(props) {