From 594d58c0d82251ff1e96843bc8e89c95473c25f9 Mon Sep 17 00:00:00 2001 From: Pierre Ferreira Date: Sun, 26 Feb 2023 12:05:11 +0100 Subject: [PATCH] :construction: Ajout de la methode getAllcards qui appel un action en fonction de l'API, fonctionnel quand nous aurons officielement une classe Card :beers: --- redux/actions/action_setCardsList.tsx | 11 +++++ redux/actions/getAllCards.tsx | 61 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 redux/actions/action_setCardsList.tsx create mode 100644 redux/actions/getAllCards.tsx diff --git a/redux/actions/action_setCardsList.tsx b/redux/actions/action_setCardsList.tsx new file mode 100644 index 0000000..1dd8200 --- /dev/null +++ b/redux/actions/action_setCardsList.tsx @@ -0,0 +1,11 @@ +import {ADD_FAVORITE_DATA, FETCH_DATA} from '../constants'; + +//? Changer cette importe quand la classe sera definit dans un fichier correctement. +import {Card} from './getAllCards' + +export const setCardsList = (List: Card[]) => { + return { + type: FETCH_DATA, + payload: List, + }; +} \ No newline at end of file diff --git a/redux/actions/getAllCards.tsx b/redux/actions/getAllCards.tsx new file mode 100644 index 0000000..21f5b58 --- /dev/null +++ b/redux/actions/getAllCards.tsx @@ -0,0 +1,61 @@ +import { setCardsList } from "./action_setCardsList"; + + + +//! classe pour tester +export class Card { + cardId : String + name : String + manaCost : number + attack : number + health : number + desc : String + + // constructor() { + // this.cardId = "cardId"; + // this.name = "name"; + // this.manaCost = 0; + // this.attack = 0; + // this.health = 0; + // this.desc = "desc"; + // } + + constructor(cardId : String, name : String, manaCost : number, attack : number, health : number, desc : String){ + this.cardId = cardId; + this.name = name; + this.manaCost = manaCost; + this.attack = attack; + this.health = health; + this.desc = desc; + } + + +} + + +//Define your action creators that will be responsible for asynchronous operations +export const getAllCards = () => { + //In order to use await your callback must be asynchronous using async keyword. + + //@ts-ignore + return async dispatch => { + //Then perform your asynchronous operations. + try { + //Have it first fetch data from our starwars url. + const CardsPromise = await fetch('https://omgvamp-hearthstone-v1.p.rapidapi.com/cards'); + + //Then use the json method to get json data from api/ + const CardsListJson = await CardsPromise.json(); + + //@ts-ignore + const CardsList: Card[] = CardsListJson.map(elt => new Card(elt["cardId"], elt["name"], elt["manaCost"], elt["attack"], elt["health"],elt["desc"])); + + //call the action + dispatch(setCardsList(CardsList)); + } 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)) + } + } +} \ No newline at end of file