|
|
@ -46,19 +46,21 @@ export const getTheme = async () => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const storeFavoriteJoke =async (joke) => {
|
|
|
|
export const storeFavoriteJoke = (joke : CustomJoke) => {
|
|
|
|
try {
|
|
|
|
return async dispatch => {
|
|
|
|
const jsonValue = await AsyncStorage.getItem('@favorite')
|
|
|
|
try {
|
|
|
|
let favoriteJokes = jsonValue != null ? JSON.parse(jsonValue) as CustomJoke[] : [];
|
|
|
|
const favoriteJokes = await AsyncStorage.getItem('favorites');
|
|
|
|
favoriteJokes.push(joke);
|
|
|
|
const favoriteJokesList = favoriteJokes != null ? JSON.parse(favoriteJokes) : [];
|
|
|
|
const updatedJsonValue = JSON.stringify(favoriteJokes);
|
|
|
|
favoriteJokesList.push(joke);
|
|
|
|
await AsyncStorage.setItem('@favorite', updatedJsonValue);
|
|
|
|
await AsyncStorage.setItem('favorites', JSON.stringify(favoriteJokesList));
|
|
|
|
const length = favoriteJokes.length;
|
|
|
|
const favorites = favoriteJokesList.map(joke => new SampleJoke(joke["type"], joke["setup"], joke["punchline"], joke["image"], joke["id"]))
|
|
|
|
console.log( "Leght" + length);
|
|
|
|
dispatch(setFavoriteJoke(favorites));
|
|
|
|
console.log("favorite stored");
|
|
|
|
const length = favoriteJokes.length;
|
|
|
|
}
|
|
|
|
console.log("Leght" + length);
|
|
|
|
catch (e) {
|
|
|
|
console.log("favorite stored");
|
|
|
|
console.log(e);
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
console.log(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -69,7 +71,8 @@ export const getFavorite = () => {
|
|
|
|
let favoriteJokesList = favoriteJokes != null ? JSON.parse(favoriteJokes) : [];
|
|
|
|
let favoriteJokesList = favoriteJokes != null ? JSON.parse(favoriteJokes) : [];
|
|
|
|
favoriteJokesList = favoriteJokesList.filter(joke => joke.id !== undefined);
|
|
|
|
favoriteJokesList = favoriteJokesList.filter(joke => joke.id !== undefined);
|
|
|
|
await AsyncStorage.setItem('favorites', JSON.stringify(favoriteJokesList));
|
|
|
|
await AsyncStorage.setItem('favorites', JSON.stringify(favoriteJokesList));
|
|
|
|
dispatch(setFavoriteJoke(favoriteJokesList));
|
|
|
|
const favorites = favoriteJokesList.map(joke => new SampleJoke(joke["type"], joke["setup"], joke["punchline"], joke["image"], joke["id"]))
|
|
|
|
|
|
|
|
dispatch(setFavoriteJoke(favorites));
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -77,19 +80,11 @@ export const getFavorite = () => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const removeFavoriteJoke = (joke) => {
|
|
|
|
export const removeFavoriteJoke = (joke) => {
|
|
|
|
return async (dispatch) => {
|
|
|
|
return async dispatch => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const jsonValue = await AsyncStorage.getItem('@favorite')
|
|
|
|
await AsyncStorage.clear()
|
|
|
|
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) {
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
console.log("An error occurred", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|