-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-- Adds new sources of data https://github.com/andrab/ecuacovid
- Loading branch information
Cesar Cobo
committed
May 9, 2020
1 parent
0ab4fb8
commit aed29df
Showing
8 changed files
with
121 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { getCSVFile } from '../utils/parser'; | ||
|
||
export default function useCSVParser({ url }, initialData = null) { | ||
const [data, setData] = useState(initialData); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [isError, setIsError] = useState(false); | ||
|
||
useEffect(() => { | ||
let didCancel = false; | ||
async function fetchData() { | ||
setIsLoading(true); | ||
setIsError(false); | ||
|
||
try { | ||
const data = await getCSVFile(url) | ||
setData(data); | ||
setIsLoading(false); | ||
} catch (err) { | ||
if (!didCancel) { | ||
setIsError(true); | ||
setIsLoading(false); | ||
} | ||
} | ||
} | ||
fetchData(); | ||
|
||
return () => { | ||
didCancel = true; | ||
}; | ||
}, [url]); | ||
return { | ||
data, | ||
isLoading, | ||
isError | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Papa from 'papaparse' | ||
|
||
export const parseProvincesFromCSVJSON = (csvJSON) => { | ||
const parsedProvinces = [] | ||
if(csvJSON && csvJSON.length > 0) { | ||
const headers = csvJSON[0]; | ||
const headersLength = headers.length; | ||
const provinces = [...csvJSON]; | ||
delete provinces[0] | ||
provinces.forEach((province, index) => { | ||
const provinceItem = { | ||
id: index, | ||
name: province[0], | ||
confirmed: province[headersLength - 1], | ||
lat: parseFloat(province[2]), | ||
lng: parseFloat(province[3]), | ||
status: getStatus(province[headersLength - 1]), | ||
} | ||
parsedProvinces.push(provinceItem) | ||
}); | ||
} | ||
|
||
return parsedProvinces; | ||
}; | ||
|
||
export const getCSVFile = (url) => new Promise( (resolve, reject) => { | ||
Papa.parse(url, { | ||
download: true, | ||
complete: (parsed) => { | ||
resolve(parsed.data); | ||
}, | ||
error: (e) => { | ||
reject(e); | ||
} | ||
}) | ||
}); | ||
|
||
const getStatus = (confirmed) => { | ||
if(confirmed > 5000) { | ||
return 'alert'; | ||
} else if(confirmed > 1500) { | ||
return 'warning'; | ||
} else if(confirmed > 500) { | ||
return 'caution'; | ||
} | ||
}; |