commit
3cc455b3a5
@ -0,0 +1,19 @@
|
|||||||
|
import {Category} from "../../model/Category";
|
||||||
|
|
||||||
|
export enum ActionType {
|
||||||
|
FETCH_CATEGORIES = 'FETCH_CATEGORIES',
|
||||||
|
}
|
||||||
|
|
||||||
|
type actionFetch = {
|
||||||
|
type: ActionType.FETCH_CATEGORIES;
|
||||||
|
payload: Category[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Action = actionFetch;
|
||||||
|
|
||||||
|
export const setCategoriesList = (categoriesList: Category[]) => {
|
||||||
|
return {
|
||||||
|
type: ActionType.FETCH_CATEGORIES,
|
||||||
|
payload: categoriesList,
|
||||||
|
};
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
import {CustomJoke} from "../../model/CustomJoke";
|
||||||
|
import {SampleJoke} from "../../model/SampleJoke";
|
||||||
|
|
||||||
|
export enum ActionType {
|
||||||
|
FETCH_JOKES = 'FETCH_JOKES',
|
||||||
|
POST_CUSTOM_JOKE = "POST_CUSTOM_JOKE",
|
||||||
|
}
|
||||||
|
|
||||||
|
type actionPostFetch = {
|
||||||
|
type: ActionType.POST_CUSTOM_JOKE;
|
||||||
|
payload: CustomJoke;
|
||||||
|
}
|
||||||
|
type actionGetFetch = {
|
||||||
|
type: ActionType.FETCH_JOKES;
|
||||||
|
payload: CustomJoke[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Action = actionPostFetch | actionGetFetch;
|
||||||
|
|
||||||
|
export const setPostJoke = (customJoke: CustomJoke) => {
|
||||||
|
return {
|
||||||
|
type: ActionType.POST_CUSTOM_JOKE,
|
||||||
|
payload: customJoke
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const setCustomJokesList = (jokesList: CustomJoke[]) => {
|
||||||
|
return {
|
||||||
|
type: ActionType.FETCH_JOKES,
|
||||||
|
payload: jokesList,
|
||||||
|
};
|
||||||
|
}
|
@ -1,31 +1,20 @@
|
|||||||
import {Category} from "../../model/Category";
|
|
||||||
import {SampleJoke} from "../../model/SampleJoke";
|
import {SampleJoke} from "../../model/SampleJoke";
|
||||||
|
|
||||||
export enum ActionType {
|
export enum ActionType {
|
||||||
FETCH_CATEGORIES = 'FETCH_CATEGORIES',
|
|
||||||
FETCH_JOKES = 'FETCH_JOKES',
|
FETCH_JOKES = 'FETCH_JOKES',
|
||||||
FETCH_JOKES_BY_ID = 'FETCH_JOKES_BY_ID'
|
FETCH_JOKES_BY_ID = 'FETCH_JOKES_BY_ID',
|
||||||
}
|
}
|
||||||
|
|
||||||
type actionFetch = {
|
type actionFetch = {
|
||||||
type: ActionType.FETCH_CATEGORIES;
|
|
||||||
payload: Category[];
|
|
||||||
} | {
|
|
||||||
type: ActionType.FETCH_JOKES;
|
type: ActionType.FETCH_JOKES;
|
||||||
payload: SampleJoke[];
|
payload: SampleJoke[];
|
||||||
} | {
|
}
|
||||||
|
type actionFetchById = {
|
||||||
type: ActionType.FETCH_JOKES_BY_ID;
|
type: ActionType.FETCH_JOKES_BY_ID;
|
||||||
payload: SampleJoke;
|
payload: SampleJoke;
|
||||||
}
|
}
|
||||||
|
export type Action = actionFetch | actionFetchById;
|
||||||
|
|
||||||
export type Action = actionFetch;
|
|
||||||
|
|
||||||
export const setCategoriesList = (categoriesList: Category[]) => {
|
|
||||||
return {
|
|
||||||
type: ActionType.FETCH_CATEGORIES,
|
|
||||||
payload: categoriesList,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
export const setJokesList = (jokesList: SampleJoke[]) => {
|
export const setJokesList = (jokesList: SampleJoke[]) => {
|
||||||
return {
|
return {
|
||||||
type: ActionType.FETCH_JOKES,
|
type: ActionType.FETCH_JOKES,
|
@ -0,0 +1,24 @@
|
|||||||
|
import {CustomJoke} from "../../model/CustomJoke";
|
||||||
|
import {SampleJoke} from "../../model/SampleJoke";
|
||||||
|
import {Action, ActionType} from "../actions/CategoryAction";
|
||||||
|
import {Category} from "../../model/Category";
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
categories: Category[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
categories: []
|
||||||
|
}
|
||||||
|
|
||||||
|
const categoryReducer = (state: State = initialState, action: Action) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case ActionType.FETCH_CATEGORIES:
|
||||||
|
// @ts-ignore
|
||||||
|
return {...state, categories: action.payload};
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default categoryReducer;
|
@ -0,0 +1,29 @@
|
|||||||
|
import {CustomJoke} from "../../model/CustomJoke";
|
||||||
|
import {SampleJoke} from "../../model/SampleJoke";
|
||||||
|
import {Action, ActionType} from "../actions/CustomJoke";
|
||||||
|
import {Category} from "../../model/Category";
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
customJokes: CustomJoke[];
|
||||||
|
customJoke: CustomJoke;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
customJokes: [],
|
||||||
|
customJoke: new CustomJoke('', '', '', '')
|
||||||
|
}
|
||||||
|
|
||||||
|
const customReducer = (state: State = initialState, action: Action) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case ActionType.POST_CUSTOM_JOKE:
|
||||||
|
// @ts-ignore
|
||||||
|
return {...state, customJoke: action.payload};
|
||||||
|
case ActionType.FETCH_JOKES:
|
||||||
|
// @ts-ignore
|
||||||
|
return {...state, customJokes: action.payload};
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default customReducer;
|
@ -0,0 +1,21 @@
|
|||||||
|
import {SampleJoke} from "../../model/SampleJoke";
|
||||||
|
import { setJokeById} from "../actions/SampleAction";
|
||||||
|
|
||||||
|
export const getItem = <TItem>(uri:string, constructor : (json:any) => TItem, setItem: (item: TItem) => 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 Json = await promise.json();
|
||||||
|
const Item: TItem = constructor(Json);
|
||||||
|
dispatch(setItem(Item));
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Error---------', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const getJokeById = (id) => {
|
||||||
|
return getItem<SampleJoke>('https://iut-weather-api.azurewebsites.net/jokes/samples/' + id , (elt) => new SampleJoke(elt["id"], elt["type"], elt["setup"], elt["image"]), (item) => setJokeById(item))
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
import {setPostJoke} from "../actions/CustomJoke";
|
||||||
|
import {SampleJoke} from "../../model/SampleJoke";
|
||||||
|
import {setJokesList} from "../actions/SampleAction";
|
||||||
|
import {getList} from "./GetThunk";
|
||||||
|
|
||||||
|
export const setItem = <TItem>(
|
||||||
|
uri: string,
|
||||||
|
type : string,
|
||||||
|
setup : string,
|
||||||
|
punchline : string
|
||||||
|
) => {
|
||||||
|
return async dispatch => {
|
||||||
|
try {
|
||||||
|
// @ts-ignore
|
||||||
|
const response = await fetch(uri, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json",
|
||||||
|
"Content-Type": 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(
|
||||||
|
{
|
||||||
|
type: type,
|
||||||
|
setup: setup,
|
||||||
|
punchline: punchline
|
||||||
|
}
|
||||||
|
)
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
dispatch(setPostJoke(data));
|
||||||
|
if (response.ok) {
|
||||||
|
console.log('Envoie ok de custom joke')
|
||||||
|
} else {
|
||||||
|
console.log('Erreur lors de la requête POST');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Erreur :', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const postCustomJoke = (joke, downgrade, category) => {
|
||||||
|
return setItem('https://iut-weather-api.azurewebsites.net/jokes', joke, downgrade, category)
|
||||||
|
}
|
Loading…
Reference in new issue