From a9d402ab7e9b9765e3c49899c0067d1955aa9ad3 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+franjsco@users.noreply.github.com> Date: Tue, 7 Apr 2020 20:11:00 +0200 Subject: [PATCH] Modificate funzioni per il recupero dei dati --- package-lock.json | 10 --------- package.json | 2 -- src/api/DataFromRepository.js | 41 +++++------------------------------ 3 files changed, 6 insertions(+), 47 deletions(-) diff --git a/package-lock.json b/package-lock.json index 39ab525..75c98a4 100755 --- a/package-lock.json +++ b/package-lock.json @@ -4029,11 +4029,6 @@ "resolved": "https://registry.npmjs.org/csstype/-/csstype-2.6.9.tgz", "integrity": "sha512-xz39Sb4+OaTsULgUERcCk+TJj8ylkL4aSVDQiX/ksxbELSqwkgt4d4RD7fovIdgJGSuNYqwZEiVjYY5l0ask+Q==" }, - "csvjson-csv2json": { - "version": "5.0.6", - "resolved": "https://registry.npmjs.org/csvjson-csv2json/-/csvjson-csv2json-5.0.6.tgz", - "integrity": "sha512-to9QlVI4yQFPzSqi6XZ97lULKs5m3K8hlE/UkbvKSw81FfDwgV9MUtSbHFJ4SRT4IA7UPkuW2EqsH/MWN5YRow==" - }, "cyclist": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/cyclist/-/cyclist-1.0.1.tgz", @@ -4083,11 +4078,6 @@ } } }, - "date-fns": { - "version": "2.11.0", - "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.11.0.tgz", - "integrity": "sha512-8P1cDi8ebZyDxUyUprBXwidoEtiQAawYPGvpfb+Dg0G6JrQ+VozwOmm91xYC0vAv1+0VmLehEPb+isg4BGUFfA==" - }, "debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", diff --git a/package.json b/package.json index 12a10dc..59f6cf1 100755 --- a/package.json +++ b/package.json @@ -11,8 +11,6 @@ "@testing-library/user-event": "^7.2.1", "axios": "^0.19.2", "bootstrap": "^4.4.1", - "csvjson-csv2json": "^5.0.6", - "date-fns": "^2.11.0", "prop-types": "^15.7.2", "react": "^16.13.0", "react-bootstrap": "^1.0.0-beta.17", diff --git a/src/api/DataFromRepository.js b/src/api/DataFromRepository.js index b7b0b34..543c2e3 100755 --- a/src/api/DataFromRepository.js +++ b/src/api/DataFromRepository.js @@ -1,13 +1,8 @@ -// import axios from 'axios'; import axios from 'axios'; -import csv2json from 'csvjson-csv2json'; -import { format, addDays, getHours } from 'date-fns'; - - -const getCSVRegionifromRepository = async (date) => { - const url = 'https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/'; - const filename = `dpc-covid19-ita-regioni-${format(date, 'yyyyMMdd')}.csv`; +const getJsonRegioni = async () => { + const url = 'https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/'; + const filename = 'dpc-covid19-ita-regioni-latest.json'; const finalURL = `${url + filename}`; let result; @@ -22,35 +17,11 @@ const getCSVRegionifromRepository = async (date) => { }; -const convertCSVtoJSON = (csv) => { - if (!csv) { - return null; - } - - return csv2json(csv, { parseNumbers: true }); -}; - const getData = async () => { - const actualDate = new Date(); - const previousDate = addDays(actualDate, -1); - const hour = getHours(actualDate); - let result; + const nomeRegione = 'Campania'; - // preleva il CSV con data attuale solamente se sono passate le 18 - if (hour >= 18) { - result = await getCSVRegionifromRepository(actualDate); - } - - // preleva il CSV con data precedente qualora non fosse stato trovato quello attuale - if (!result) { - result = await getCSVRegionifromRepository(previousDate); - } - - // converte il CSV in JSON e filtra i dati per la regione Campania - if (result) { - const tmpJSON = convertCSVtoJSON(result); - result = tmpJSON.find((d) => d.denominazione_regione === 'Campania'); - } + const datiRegione = await getJsonRegioni(); + const result = datiRegione.find((regione) => regione.denominazione_regione === nomeRegione); return result; };