diff --git a/JokesApp/components/DetailJoke.tsx b/JokesApp/components/DetailJoke.tsx index f630c56..8a4ac17 100644 --- a/JokesApp/components/DetailJoke.tsx +++ b/JokesApp/components/DetailJoke.tsx @@ -3,9 +3,10 @@ import {darksalmonColor, greyColor, indigo, purpleColor, whiteColor} from "../Th import React, {useState} from "react"; import {SampleJoke} from "../model/SampleJoke"; import {Joke} from "../model/Joke"; +import {CustomJoke} from "../model/CustomJoke"; type DetailJokeProps = { - item: SampleJoke; + item: CustomJoke; } diff --git a/JokesApp/model/JokeFactory.ts b/JokesApp/model/JokeFactory.ts index 10ce616..0b15402 100644 --- a/JokesApp/model/JokeFactory.ts +++ b/JokesApp/model/JokeFactory.ts @@ -35,4 +35,11 @@ export class JokeFactory { // array.push(new SampleJoke(joke.type, joke.setup, joke.image,joke.punchline, joke.id)); //}) } + + static createCustomJokeById(jsonArray: string): CustomJoke { + let array = []; + let json = JSON.parse(jsonArray); + return new CustomJoke(json.type, json.setup, json.punchline,json.image, json.id) + } + } \ No newline at end of file diff --git a/JokesApp/navigation/Navigation.tsx b/JokesApp/navigation/Navigation.tsx index cb68ad1..96e3cb8 100644 --- a/JokesApp/navigation/Navigation.tsx +++ b/JokesApp/navigation/Navigation.tsx @@ -32,7 +32,7 @@ export function Navigation(){ tabBarShowLabel: false, tabBarStyle: styles.top, }} > - ( { } } +export const setCustomsJokeById = (completCustomJoke: CustomJoke): postCustomAction => { + return { + type: CustomActionType.FETCH_CUSTOMS_JOKE_BY_ID, + payload: completCustomJoke + } + +} + export const postJoke = (type : string, setup : string, punchline : string) => { @@ -69,13 +78,7 @@ export const postJoke = (type : string, setup : string, punchline : string) => { export const getJokesCustoms = () => { return async dispatch => { try { - const custom = await fetch('https://iut-weather-api.azurewebsites.net/jokes/', { - method: 'GET', - headers: { - Accept: "application/json", - "Content-Type": "application/json", - }, - }); + const custom = await fetch('https://iut-weather-api.azurewebsites.net/jokes/'); const customsJson = await custom.text(); const customJokes = JokeFactory.createCustomJokes(customsJson); dispatch(setCustomsJoke(customJokes)); @@ -83,4 +86,18 @@ export const getJokesCustoms = () => { console.log('Error---------', error); } } -} \ No newline at end of file +} + +export const getJokesCustomsById = (id : number) => { + return async dispatch => { + try { + const custom = await fetch('https://iut-weather-api.azurewebsites.net/jokes/' + id); + const customsJson = await custom.text(); + const customJokes = JokeFactory.createCustomJokeById(customsJson); + console.log('customJokes', customJokes); + dispatch(setCustomsJokeById(customJokes)); + } catch (error) { + console.log('Error---------', error); + } + } +} diff --git a/JokesApp/redux/reducers/customJokeReducer.ts b/JokesApp/redux/reducers/customJokeReducer.ts index 2aed40f..d7d3bae 100644 --- a/JokesApp/redux/reducers/customJokeReducer.ts +++ b/JokesApp/redux/reducers/customJokeReducer.ts @@ -6,12 +6,14 @@ import {CustomActionType} from "../actions/customAction"; interface state { postJoke: CustomJoke; customJokes: CustomJoke[]; + completCustomJoke: CustomJoke; } // initial state for sampleJokes const initialState: state = { postJoke: {} as CustomJoke, customJokes: [], + completCustomJoke: {} as CustomJoke, } // app reducer for sampleJokes @@ -28,6 +30,11 @@ export default appReducer = (state = initialState, action: Action) => { ...state, customJokes: action.payload, } + case CustomActionType.FETCH_CUSTOMS_JOKE_BY_ID: + return { + ...state, + completCustomJoke: action.payload, + } default: return state; diff --git a/JokesApp/redux/reducers/sampleJokeReducer.ts b/JokesApp/redux/reducers/sampleJokeReducer.ts index cda9faf..2822066 100644 --- a/JokesApp/redux/reducers/sampleJokeReducer.ts +++ b/JokesApp/redux/reducers/sampleJokeReducer.ts @@ -7,8 +7,6 @@ interface state { sampleJoke: SampleJoke[]; recentJokes: SampleJoke[]; completJoke: SampleJoke; - postJoke: CustomJoke; - customJokes: CustomJoke[]; } // initial state for sampleJokes @@ -16,8 +14,6 @@ const initialState: state = { completJoke: {} as SampleJoke, sampleJoke: [], recentJokes: [], - postJoke: {} as CustomJoke, - customJokes: [], } // app reducer for sampleJokes diff --git a/JokesApp/screens/JokeDetailScreen.tsx b/JokesApp/screens/JokeDetailScreen.tsx index a120c2f..812c391 100644 --- a/JokesApp/screens/JokeDetailScreen.tsx +++ b/JokesApp/screens/JokeDetailScreen.tsx @@ -7,21 +7,26 @@ import {Joke} from "../model/Joke"; import {useDispatch, useSelector} from "react-redux"; import {getCompletJokes, setCompletJokes, setSample} from "../redux/actions/sampleAction"; import {validatePathConfig} from "@react-navigation/native"; +import {getJokesCustomsById} from "../redux/actions/customAction"; //svjh export default function JokeDetailScreen({route}) { const jokeId = route.params.joke; - console.log(jokeId); - const DataGen = useSelector((state: any) => state.sampleReducer.completJoke); + const state = route.params.state; + console.log(state); + + // Déterminer quelle donnée utiliser en fonction de l'état de `state` + const DataGen = state ? useSelector((state: any) => state.customReducer.completCustomJoke) : useSelector((state: any) => state.sampleReducer.completJoke); + // const DataGen = useSelector((state: any) => state.sampleReducer.completJoke); const dispatch = useDispatch(); useEffect(() => { const getDetails = async () => { // @ts-ignore - await dispatch(getCompletJokes(jokeId)) + { state ? await dispatch(getJokesCustomsById(jokeId)) : await dispatch(getCompletJokes(jokeId));} + }; getDetails(); }, [dispatch]); - console.log(DataGen); return ( diff --git a/JokesApp/screens/ListJokeScreen.tsx b/JokesApp/screens/ListJokeScreen.tsx index b7f2d59..fb6d87b 100644 --- a/JokesApp/screens/ListJokeScreen.tsx +++ b/JokesApp/screens/ListJokeScreen.tsx @@ -39,7 +39,7 @@ export function ListJokeScreen({route, navigation}) { ( - navigation.navigate("JokeDetail", {"joke" : item.id})}> + navigation.navigate("JokeDetail", {"joke" : item.id, "state" : isActivated2})}> )}