From f73a9e8e36580102ba139ab7684945a414625529 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Sun, 1 Mar 2020 21:17:51 +0100 Subject: [PATCH] Fixed ISO-8859 bug --- package.json | 3 ++- src/Components/GasStation.js | 4 +++- src/Components/Map.js | 8 +++++++- src/helpers.js | 10 ++++++++-- yarn.lock | 7 +++++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 7456473..6b98f6b 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "bootstrap": "^4.4.1", "eslint-config-prettier": "^6.10.0", "eslint-plugin-prettier": "^3.1.2", + "iconv-lite": "^0.5.1", "mapbox-gl": "^1.8.1", "moment": "^2.24.0", "prop-types": "^15.7.2", @@ -64,4 +65,4 @@ "husky": "^4.2.3", "lint-staged": "^10.0.8" } -} +} \ No newline at end of file diff --git a/src/Components/GasStation.js b/src/Components/GasStation.js index a10c5b6..3745893 100644 --- a/src/Components/GasStation.js +++ b/src/Components/GasStation.js @@ -2,6 +2,8 @@ import React from 'react'; import { Modal, Button, ListGroup } from "react-bootstrap"; import PropTypes from 'prop-types'; +import { capitalizeFirstLetter} from "../helpers"; + class GasStation extends React.Component { renderPrices = () => { const { @@ -38,7 +40,7 @@ class GasStation extends React.Component { > - {`${selectedGasStation.address} - ${selectedGasStation.city}`} + {`${selectedGasStation.address} - ${capitalizeFirstLetter(selectedGasStation.city)}`} diff --git a/src/Components/Map.js b/src/Components/Map.js index 835fe09..d920763 100644 --- a/src/Components/Map.js +++ b/src/Components/Map.js @@ -4,6 +4,7 @@ import { Button } from "react-bootstrap"; import XmlReader from 'xml-reader'; import { withToastManager } from 'react-toast-notifications'; import PropTypes from 'prop-types'; +import iconv from "iconv-lite"; import { haveSelectedGas, extractGasStationFromXml } from '../helpers'; import 'mapbox-gl/dist/mapbox-gl.css'; @@ -89,8 +90,11 @@ class Map extends React.Component { toastManager, } = this.props; + toastManager.add('Chargement de la liste des stations...', { appearance: 'info', autoDismiss: true }); + fetch('/gasStations.xml') - .then((response) => response.text()) + .then(res => res.arrayBuffer()) + .then(arrayBuffer => iconv.decode(Buffer.from(arrayBuffer), 'iso-8859-1').toString()) .then((response) => { const reader = XmlReader.create(); @@ -110,6 +114,8 @@ class Map extends React.Component { })); }); reader.parse(response); + + toastManager.add('Liste des stations correctement chargée', { appearance: 'success', autoDismiss: true }); }).catch(() => { toastManager.add('Erreur lors du chargement de la liste des stations', { appearance: 'error', autoDismiss: true }); }); diff --git a/src/helpers.js b/src/helpers.js index 9acf064..aff2b6f 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -25,7 +25,7 @@ export const getPlvInformation = (pdv, name) => { if (currentChildren.type === 'element' && currentChildren.name === name) { if ( currentChildren.children && currentChildren.children.length > 0 ) { - return currentChildren.children[0].value; + return currentChildren.children[0].value.toLowerCase(); } return null; @@ -60,4 +60,10 @@ export const extractGasStationFromXml = (currentPdv ) => { city: getPlvInformation(currentPdv, 'ville') } } - \ No newline at end of file + +export const capitalizeFirstLetter = (string) => { + if ( !string){ + return ''; + } + return string.charAt(0).toUpperCase() + string.slice(1); +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index b472425..c580bed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5428,6 +5428,13 @@ iconv-lite@0.4.24, iconv-lite@^0.4.24: dependencies: safer-buffer ">= 2.1.2 < 3" +iconv-lite@^0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.5.1.tgz#b2425d3c7b18f7219f2ca663d103bddb91718d64" + integrity sha512-ONHr16SQvKZNSqjQT9gy5z24Jw+uqfO02/ngBSBoqChZ+W8qXX7GPRa1RoUnzGADw8K63R1BXUMzarCVQBpY8Q== + dependencies: + safer-buffer ">= 2.1.2 < 3" + icss-utils@^4.0.0, icss-utils@^4.1.1: version "4.1.1" resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-4.1.1.tgz#21170b53789ee27447c2f47dd683081403f9a467"