-
-
Save mbriceno/f4867f64b3341b9300e08ffe99c7741d to your computer and use it in GitHub Desktop.
Dada un fecha en formato d-mmmm-yyyy, se debe hacer un GET a la siguiente API
This file contains hidden or 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
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> | |
<script> | |
Date.prototype.addDays = function(days) { | |
var date = new Date(this.valueOf()); | |
date.setDate(date.getDate() + days); | |
return date; | |
} | |
const getStockInformation = async (range) => { | |
/* Esta función devuelve el objeto del ejercicio pero falta definir si la url del api recibe algún | |
query param para traer otras paginas de la fecha consultada | |
*/ | |
const initDate = Date.parse(range.dateFrom); | |
const finishDate = Date.parse(range.dateTo); | |
const currentDate = initDate; | |
const dateRange = []; | |
const rangeData = {} | |
while (currentDate <= finishDate) { | |
dateRange.push(new Date (currentDate)); | |
currentDate = currentDate.addDays(1); | |
} | |
for (let i=0; i < dateRange.length; i++) { | |
try { | |
// Es posible que con momentsJs sea mas sencillo crear el formato adecuado pero para no complicarme | |
// lo dejo de esta forma con Date nativo de Javascript | |
const day = dateRange[i].getDay() | |
const month = dateRange[i].toLocaleString('default', { month: 'long' }) | |
const year = dateRange[i].getFullYear() | |
const date = `${day}-${month}-${year}` | |
// Es posible que se necesiten configurar cabeceras de axios para que no de conflictos con el api | |
// pero para ser mas practicos lo dejo de forma sencilla | |
const stocks = await axios.get(`http://34.194.78.120/api/test/stocks?date=${date}`) | |
if(stocks.status == 200) { | |
const data = stocks.data | |
for(let j=0; j<data.length; j++) { | |
const instance = data[j] | |
if (rangeData[instance.date]) | |
rangeData[instance.date] += 1 | |
else | |
rangeData[instance.date] = 1 | |
} | |
} | |
} catch (error) { | |
console.error(error) | |
} | |
} | |
return rangeData; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment