diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..b58b603
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,5 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
diff --git a/.idea/Tp_ReactNative.iml b/.idea/Tp_ReactNative.iml
new file mode 100644
index 0000000..24643cc
--- /dev/null
+++ b/.idea/Tp_ReactNative.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..e88c539
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/JokesApp/components/DetailJoke.tsx b/JokesApp/components/DetailJoke.tsx
index a6fb0e3..0e23271 100644
--- a/JokesApp/components/DetailJoke.tsx
+++ b/JokesApp/components/DetailJoke.tsx
@@ -4,7 +4,7 @@ import React, {useState} from "react";
import {SampleJoke} from "../model/SampleJoke";
import {Joke} from "../model/Joke";
import {CustomJoke} from "../model/CustomJoke";
-import {storeFavoriteJoke} from "../redux/store";
+import {storeFavoriteJoke, useAppDispatch, useAppSelector} from "../redux/store";
type DetailJokeProps = {
item: CustomJoke;
@@ -16,6 +16,9 @@ export function DetailJoke(props: DetailJokeProps) {
const [isActivated, setIsActivated] = useState(false);
const [isActivated2, setIsActivated2] = useState(false);
+ const favoriteJokes = useAppSelector(state => state.customReducer.favoriteJokes) as [CustomJoke, SampleJoke];
+ const dispatch = useAppDispatch();
+
function toggleActivation() {
setIsActivated(!isActivated);
if (isActivated) {
@@ -23,9 +26,12 @@ export function DetailJoke(props: DetailJokeProps) {
//removeFavoriteJoke();
}
else {
+ if (props.item === undefined) {
+ console.log("Joke undefined");
+ return;
+ }
console.log("Joke ajoutée aux favoris");
- storeFavoriteJoke(props.item)
- }
+ storeFavoriteJoke(props.item); }
}
diff --git a/JokesApp/navigation/Navigation.tsx b/JokesApp/navigation/Navigation.tsx
index 85df55f..1dcd4c0 100644
--- a/JokesApp/navigation/Navigation.tsx
+++ b/JokesApp/navigation/Navigation.tsx
@@ -19,8 +19,6 @@ import {ListFavoriteJokeScreen} from "../screens/ListFavoriteJokeScreen";
export function Navigation(){
-
-
const BottomTabNavigator = createBottomTabNavigator();
const [themes, setThemes] = useState(DefaultTheme);
diff --git a/JokesApp/redux/actions/customAction.ts b/JokesApp/redux/actions/customAction.ts
index afc68b4..dbd8323 100644
--- a/JokesApp/redux/actions/customAction.ts
+++ b/JokesApp/redux/actions/customAction.ts
@@ -6,6 +6,7 @@ export enum CustomActionType {
FETCH_CUSTOMS_JOKE = 'FETCH_CUSTOMS_JOKE',
FETCH_CUSTOMS_JOKE_BY_ID = 'FETCH_CUSTOMS_JOKE_BY_ID',
DELETE_CUSTOM_JOKE = 'DELETE_CUSTOM_JOKE',
+ FETCH_FAVORITE_JOKE = 'FETCH_FAVORITE_JOKE'
}
export interface CustomAction {
@@ -23,6 +24,8 @@ export interface CustomsAction {
payload: CustomJoke[];
}
+
+
export type Action = CustomAction;
@@ -55,6 +58,12 @@ export const setDeleteCustomJoke = (deleteCustomJoke: CustomJoke): postCustomAct
}
}
+export const setFavoriteJoke = (favoriteJokes: CustomJoke[]): CustomsAction => {
+ return {
+ type: CustomActionType.FETCH_FAVORITE_JOKE,
+ payload: favoriteJokes
+ }
+}
export const postJoke = (type : string, setup : string, punchline : string) => {
diff --git a/JokesApp/redux/reducers/customJokeReducer.ts b/JokesApp/redux/reducers/customJokeReducer.ts
index 1d09c84..91d979b 100644
--- a/JokesApp/redux/reducers/customJokeReducer.ts
+++ b/JokesApp/redux/reducers/customJokeReducer.ts
@@ -8,6 +8,7 @@ interface state {
customJokes: CustomJoke[];
completCustomJoke: CustomJoke;
deleteCustomJoke: CustomJoke;
+ favoriteJokes: CustomJoke[];
}
// initial state for sampleJokes
@@ -16,6 +17,7 @@ const initialState: state = {
customJokes: [],
completCustomJoke: {} as CustomJoke,
deleteCustomJoke: {} as CustomJoke,
+ favoriteJokes: []
}
// app reducer for sampleJokes
@@ -42,6 +44,11 @@ export default appReducer = (state = initialState, action: Action) => {
...state,
deleteCustomJoke: action.payload,
}
+ case CustomActionType.FETCH_FAVORITE_JOKE:
+ return {
+ ...state,
+ favoriteJokes: action.payload,
+ }
default:
return state;
diff --git a/JokesApp/redux/store.ts b/JokesApp/redux/store.ts
index 79cd16b..f87513d 100644
--- a/JokesApp/redux/store.ts
+++ b/JokesApp/redux/store.ts
@@ -5,6 +5,10 @@ import customReducer from "./reducers/customJokeReducer";
import AsyncStorage from '@react-native-async-storage/async-storage';
import {Theme} from "@react-navigation/native";
import {CustomJoke} from "../model/CustomJoke";
+import {setFavoriteJoke} from "./actions/customAction";
+import {TypedUseSelectorHook, useDispatch, useSelector} from "react-redux";
+import {SampleJoke} from "../model/SampleJoke";
+import {Joke} from "../model/Joke";
const reducer = {
categorieReducer: categorieReducer,
@@ -13,7 +17,6 @@ const reducer = {
};
-// @ts-ignore
const store = configureStore({
// @ts-ignore
reducer,
@@ -59,30 +62,43 @@ export const storeFavoriteJoke =async (joke) => {
}
}
-export const getFavorite = async () => {
- try {
- const jsonValue = await AsyncStorage.getItem('@favorite')
- return jsonValue != null ? JSON.parse(jsonValue) as CustomJoke[] : null;
- } catch(e) {
- console.log(e);
+export const getFavorite = () => {
+ return async (dispatch) => {
+ try {
+ const favoriteJokes = await AsyncStorage.getItem('favorites');
+ let favoriteJokesList = favoriteJokes != null ? JSON.parse(favoriteJokes) : [];
+ favoriteJokesList = favoriteJokesList.filter(joke => joke.id !== undefined);
+ await AsyncStorage.setItem('favorites', JSON.stringify(favoriteJokesList));
+ dispatch(setFavoriteJoke(favoriteJokesList));
+ } catch (e) {
+ console.log(e);
+ }
}
}
-export const removeFavoriteJoke = async (joke) => {
- try {
- const jsonValue = await AsyncStorage.getItem('@favorite')
- let favoriteJokes = jsonValue != null ? JSON.parse(jsonValue) as CustomJoke[] : [];
- const index = favoriteJokes.indexOf(joke);
- if (index > -1) {
- favoriteJokes.splice(index, 1);
+export const removeFavoriteJoke = (joke) => {
+ return async (dispatch) => {
+ try {
+ const jsonValue = await AsyncStorage.getItem('@favorite')
+ let favoriteJokes = jsonValue != null ? JSON.parse(jsonValue) : [];
+ const index = favoriteJokes.indexOf(joke);
+ if (index > -1) {
+ favoriteJokes.splice(index, 1);
+ }
+ const updatedJsonValue = JSON.stringify(favoriteJokes);
+ AsyncStorage.setItem('@favorite', updatedJsonValue);
+ console.log("favorite removed");
+ } catch (e) {
+ console.log(e);
}
- const updatedJsonValue = JSON.stringify(favoriteJokes);
- await AsyncStorage.setItem('@favorite', updatedJsonValue);
- console.log("favorite removed");
- }
- catch (e) {
- console.log(e);
}
}
+export type AppDispatch = typeof store.dispatch;
+
+export type AppStore = ReturnType;
+
+export const useAppDispatch = () => useDispatch();
+export const useAppSelector: TypedUseSelectorHook = useSelector;
+
export default store;
diff --git a/JokesApp/screens/ListFavoriteJokeScreen.tsx b/JokesApp/screens/ListFavoriteJokeScreen.tsx
index 1465c91..c25039b 100644
--- a/JokesApp/screens/ListFavoriteJokeScreen.tsx
+++ b/JokesApp/screens/ListFavoriteJokeScreen.tsx
@@ -2,40 +2,53 @@ import {useTheme} from "@react-navigation/native";
import {darksalmonColor, indigo, whiteColor} from "../Theme";
import React, {useEffect, useState} from "react";
import {getFavorite} from "../redux/store";
-import {FlatList, SafeAreaView, StyleSheet, TouchableHighlight} from "react-native";
+import {FlatList, SafeAreaView, StyleSheet, TouchableHighlight, View, Text} from "react-native";
import {JokeListItems} from "../components/ListeJokeComponent";
import {CustomJoke} from "../model/CustomJoke";
+import {SampleJoke} from "../model/SampleJoke";
+import {useAppSelector, useAppDispatch} from "../redux/store";
+import {useDispatch} from "react-redux";
+export function ListFavoriteJokeScreen({route, navigation}){
-export function ListFavoriteJokeScreen({route, navigation}) {
- const [favoriteJokes, setFavoriteJokes] = useState([]);
+ const favoriteJokes = useAppSelector((state) => state.customReducer.favoriteJokes);
+ const dispatch = useDispatch();
useEffect(() => {
- const fetchFav = async () => {
- const jokes = await getFavorite();
- if (jokes) {
- setFavoriteJokes(jokes);
- } else {
- // Gérer le cas où aucun favori n'est retourné
- setFavoriteJokes([]);
- }
+
+
+ const getFavoriteJokes = async () => {
+ // @ts-ignore
+ await dispatch(getFavorite());
}
- fetchFav();
+ getFavoriteJokes();
}, []);
-
+ let fav = true;
+ if (favoriteJokes.length === 0) {
+ fav = false;
+ }
+ else {
+ fav = true;
+ }
+ console.log(fav);
+ console.log(favoriteJokes.length);
const styles = themeSettings();
- console.log("Ceci est ma taille"+favoriteJokes.length);
return (
- (
- navigation.navigate("JokeDetail", {"joke" : item.id})}>
-
-
- )}
- keyExtractor={(item: CustomJoke) => item.id}
- />
+ { fav ? (
+ (
+ navigation.navigate("JokeDetail", {"joke" : item.id})}>
+
+
+ )}
+ keyExtractor={(item) => item.id.toString()}
+ />
+ ) : (
+ Aucun favoris
+ )}
+
);
}