@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* @file Category.ts
|
||||||
|
* @brief Définition de la classe Catégory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class
|
||||||
|
* @brief Représente une catégorie nom.
|
||||||
|
*/
|
||||||
|
export class Category {
|
||||||
|
private _name: string;
|
||||||
|
private _number: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Constructeur de la classe Category.
|
||||||
|
* @param {string} name - Le nom de la catégorie.
|
||||||
|
* @param {number} number - Le nombre de la catégorie.
|
||||||
|
*/
|
||||||
|
constructor(name: string, number: number) {
|
||||||
|
this._name = name;
|
||||||
|
this._number = number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Obtient le nom de la catégorie.
|
||||||
|
* @return {string} Le nom de la catégorie.
|
||||||
|
*/
|
||||||
|
get name(): string {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Obtient le nombre de la catégorie.
|
||||||
|
* @return {number} Le nombre de la catégorie.
|
||||||
|
*/
|
||||||
|
get number(): number {
|
||||||
|
return this._number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Modifie le nom de la catégorie.
|
||||||
|
* @param {string} name - Le nom de la categorie.
|
||||||
|
*/
|
||||||
|
set name(name: string) {
|
||||||
|
this._name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Modifie le nombre de la catégorie.
|
||||||
|
* @param {number} number - Le nombre de la catégorie.
|
||||||
|
*/
|
||||||
|
set number(number: number) {
|
||||||
|
this._number = number;
|
||||||
|
}
|
||||||
|
}
|
@ -1,23 +0,0 @@
|
|||||||
//Define your action creators that will be responsible for asynchronous operations
|
|
||||||
import {CustomJoke} from "../../model/CustomJoke";
|
|
||||||
import {SampleJoke} from "../../model/SampleJoke";
|
|
||||||
import {setJokesList} from "../actions/JokeAction";
|
|
||||||
|
|
||||||
export const getSampleJokesList = () => {
|
|
||||||
//In order to use await your callback must be asynchronous using async keyword.
|
|
||||||
return async dispatch => {
|
|
||||||
//Then perform your asynchronous operations.
|
|
||||||
try {
|
|
||||||
//Have it first fetch data from our starwars url.
|
|
||||||
const jokesPromise = await fetch('https://iut-weather-api.azurewebsites.net/jokes/samples');
|
|
||||||
//Then use the json method to get json data from api/
|
|
||||||
const jokesListJson = await jokesPromise.json();
|
|
||||||
const jokesList: SampleJoke[] = jokesListJson.map(elt => new SampleJoke(elt["id"], elt["type"], elt["setup"], elt["image"]));
|
|
||||||
dispatch(setJokesList(jokesList));
|
|
||||||
} 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,23 +1,31 @@
|
|||||||
//Define your action creators that will be responsible for asynchronous operations
|
|
||||||
import {CustomJoke} from "../../model/CustomJoke";
|
|
||||||
import {SampleJoke} from "../../model/SampleJoke";
|
import {SampleJoke} from "../../model/SampleJoke";
|
||||||
import {setJokesList} from "../actions/JokeAction";
|
import {setCategoriesList, setJokesList} from "../actions/JokeAction";
|
||||||
|
import {Category} from "../../model/Category";
|
||||||
|
|
||||||
export const getLastSampleJokesList = () => {
|
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.
|
//In order to use await your callback must be asynchronous using async keyword.
|
||||||
return async dispatch => {
|
return async dispatch => {
|
||||||
//Then perform your asynchronous operations.
|
//Then perform your asynchronous operations.
|
||||||
try {
|
try {
|
||||||
//Have it first fetch data from our starwars url.
|
const promise = await fetch(uri);
|
||||||
const jokesPromise = await fetch('https://iut-weather-api.azurewebsites.net/jokes/lasts');
|
|
||||||
//Then use the json method to get json data from api/
|
//Then use the json method to get json data from api/
|
||||||
const jokesListJson = await jokesPromise.json();
|
const listJson = await promise.json();
|
||||||
const jokesList: SampleJoke[] = jokesListJson.map(elt => new SampleJoke(elt["id"], elt["type"], elt["setup"], elt["image"]));
|
const List: TList[] = listJson.map(elt => constructor(elt));
|
||||||
dispatch(setJokesList(jokesList));
|
dispatch(setList(List));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('Error---------', 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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
Loading…
Reference in new issue