parent
68df4a5e3f
commit
9830b6777d
@ -1,22 +0,0 @@
|
||||
import { City } from '../../data/stub';
|
||||
import {GET_CITIES} from '../constants';
|
||||
import { setCityList } from './setCityList';
|
||||
|
||||
export const getCityList = () => {
|
||||
return async dispatch => {
|
||||
|
||||
try {
|
||||
const cityPromise = await fetch('https://iut-weather-api.azurewebsites.net/cities');
|
||||
|
||||
const cityListJson = await cityPromise.json();
|
||||
|
||||
const cityList: City[] = cityListJson.map((elt: { [x: string]: any; }) => new City(elt["name"], elt["latitude"], elt["longitude"]));
|
||||
|
||||
dispatch(setCityList(cityList));
|
||||
} catch (error) {
|
||||
console.log('Error---------', error);
|
||||
//You can dispatch to another action if you want to display an error message in the application
|
||||
//dispatch(fetchDataRejected(error))
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
import { City, Weather } from '../../data/stub';
|
||||
import {GET_CITIES} from '../constants';
|
||||
import { setWeather } from './setWeather';
|
||||
|
||||
export const getWeatherList = () => {
|
||||
return async dispatch => {
|
||||
|
||||
try {
|
||||
const cityPromise = await fetch('https://iut-weather-api.azurewebsites.net/cities');
|
||||
|
||||
const cityListJson = await cityPromise.json();
|
||||
|
||||
const cityList: City[] = cityListJson.map((elt: { [x: string]: any; }) => new City(elt["name"], elt["latitude"], elt["longitude"]));
|
||||
|
||||
const promises = cityList.map( (city) =>{
|
||||
return fetch('https://iut-weather-api.azurewebsites.net/weather/city/name/'+city.name);
|
||||
})
|
||||
Promise.all(promises).then((values) => {
|
||||
const prom = values.map(async resp => {
|
||||
const weatherJson = await resp.json();
|
||||
return new Weather(weatherJson["at"], weatherJson["visibility"], weatherJson["weatherType"], weatherJson["weatherDescription"], weatherJson["temperature"], weatherJson["temperatureFeelsLike"], weatherJson["humidity"], weatherJson["windspeed"], weatherJson["pressure"], new City(weatherJson["city"]["name"], weatherJson["city"]["latitude"], weatherJson["city"]["longitude"]));
|
||||
});
|
||||
Promise.all(prom).then((values) => {
|
||||
dispatch(setWeather(values));
|
||||
})
|
||||
|
||||
})
|
||||
} catch (error) {
|
||||
console.log('Error---------', error);
|
||||
//You can dispatch to another action if you want to display an error message in the application
|
||||
//dispatch(fetchDataRejected(error))
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
import { City } from '../../data/stub';
|
||||
import {FETCH_CITIES} from '../constants';
|
||||
|
||||
export const setCityList = (cityList: City[]) => {
|
||||
return {
|
||||
type: FETCH_CITIES,
|
||||
payload: cityList,
|
||||
};
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
import { Weather } from "../../data/stub";
|
||||
import { FETCH_WEATHER } from "../constants";
|
||||
|
||||
export const setWeather = (weather: Weather[]) => {
|
||||
console.log(weather)
|
||||
return {
|
||||
type: FETCH_WEATHER,
|
||||
payload: weather,
|
||||
};
|
||||
}
|
Loading…
Reference in new issue