You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
1.5 KiB
31 lines
1.5 KiB
import {SampleJoke} from "../../model/SampleJoke";
|
|
import {setCategoriesList, setJokesList} from "../actions/JokeAction";
|
|
import {Category} from "../../model/Category";
|
|
|
|
export const getList = <TList>(uri:string, constructor : (json:any) => TList, setList: (list: TList[]) => any) => {
|
|
//In order to use await your callback must be asynchronous using async keyword.
|
|
return async dispatch => {
|
|
//Then perform your asynchronous operations.
|
|
try {
|
|
const promise = await fetch(uri);
|
|
//Then use the json method to get json data from api/
|
|
const listJson = await promise.json();
|
|
const List: TList[] = listJson.map(elt => constructor(elt));
|
|
dispatch(setList(List));
|
|
} catch (error) {
|
|
console.log('Error---------', error);
|
|
}
|
|
}
|
|
}
|
|
|
|
export const getLastSampleJokesList = () => {
|
|
return getList('https://iut-weather-api.azurewebsites.net/jokes/lasts', (elt) => new SampleJoke(elt["id"], elt["type"], elt["setup"], elt["image"]), (list) => setJokesList(list))
|
|
}
|
|
|
|
export const getCategoriesList = () => {
|
|
return getList('https://iut-weather-api.azurewebsites.net/jokes/categories/top', (elt) => new Category(elt["name"], elt["number"]), (list) => setCategoriesList(list))
|
|
}
|
|
|
|
export const getSampleJokesList = () => {
|
|
return getList('https://iut-weather-api.azurewebsites.net/jokes/samples', (elt) => new SampleJoke(elt["id"], elt["type"], elt["setup"], elt["image"]), (list) => setJokesList(list))
|
|
} |