parent
086ea9fd3d
commit
b2a19dcd44
@ -0,0 +1,20 @@
|
||||
import {CustomJoke} from "../../model/CustomJoke";
|
||||
import {SampleJoke} from "../../model/SampleJoke";
|
||||
|
||||
export enum ActionType {
|
||||
FETCH_JOKES = 'FETCH_JOKE',
|
||||
}
|
||||
|
||||
interface actionFetch {
|
||||
type: ActionType.FETCH_JOKES;
|
||||
payload: (CustomJoke | SampleJoke)[];
|
||||
}
|
||||
|
||||
export type Action = actionFetch;
|
||||
|
||||
export const setJokesList = (jokesList: (CustomJoke | SampleJoke)[]) => {
|
||||
return {
|
||||
type: ActionType.FETCH_JOKES,
|
||||
payload: jokesList,
|
||||
};
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
import {CustomJoke} from "../../model/CustomJoke";
|
||||
import {SampleJoke} from "../../model/SampleJoke";
|
||||
import {Action} from "redux";
|
||||
import {ActionType} from "../actions/JokeAction";
|
||||
|
||||
interface State {
|
||||
jokes: (CustomJoke | SampleJoke)[];
|
||||
favoriteJokes: (CustomJoke | SampleJoke)[],
|
||||
}
|
||||
|
||||
const initialState = {
|
||||
jokes: [],
|
||||
favoriteJokes: [],
|
||||
}
|
||||
|
||||
const appReducer = (state: State = initialState, action: Action) => {
|
||||
switch (action.type) {
|
||||
case ActionType.FETCH_JOKES:
|
||||
// @ts-ignore
|
||||
return {...state, jokes: action.payload};
|
||||
//case ActionType....
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
export default appReducer;
|
@ -0,0 +1,18 @@
|
||||
import {configureStore} from '@reduxjs/toolkit'
|
||||
import appReducer from './reducers/jokeReducer';
|
||||
|
||||
// Reference here all your application reducers
|
||||
const reducer = {
|
||||
appReducer: appReducer,
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const store = configureStore({
|
||||
reducer,
|
||||
middleware: (getDefaultMiddleware) =>
|
||||
getDefaultMiddleware({
|
||||
serializableCheck: false
|
||||
})
|
||||
},);
|
||||
|
||||
export default store;
|
@ -0,0 +1,23 @@
|
||||
//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))
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
//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 getLastSampleJokesList = () => {
|
||||
//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/lasts');
|
||||
//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))
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue