You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Tp_ReactNative/JokesApp/redux/store.ts

89 lines
2.6 KiB

import { configureStore } from '@reduxjs/toolkit';
import categorieReducer from './reducers/categoryReducer';
import sampleReducer from './reducers/sampleJokeReducer';
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";
const reducer = {
categorieReducer: categorieReducer,
sampleReducer: sampleReducer,
customReducer: customReducer
};
// @ts-ignore
const store = configureStore({
// @ts-ignore
reducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false
})
});
export const storeTheme = async (theme) => {
try {
const jsonValue = JSON.stringify(theme)
await AsyncStorage.setItem('@theme', jsonValue)
console.log("theme stored")
} catch (e) {
console.log(e);
}
}
export const getTheme = async () => {
try {
const jsonValue = await AsyncStorage.getItem('@theme')
return jsonValue != null ? JSON.parse(jsonValue) as Theme : null;
} catch(e) {
console.log(e);
}
}
export const storeFavoriteJoke =async (joke) => {
try {
const jsonValue = await AsyncStorage.getItem('@favorite')
let favoriteJokes = jsonValue != null ? JSON.parse(jsonValue) as CustomJoke[] : [];
favoriteJokes.push(joke);
const updatedJsonValue = JSON.stringify(favoriteJokes);
await AsyncStorage.setItem('@favorite', updatedJsonValue);
const length = favoriteJokes.length;
console.log( "Leght" + length);
console.log("favorite stored");
}
catch (e) {
console.log(e);
}
}
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 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);
}
const updatedJsonValue = JSON.stringify(favoriteJokes);
await AsyncStorage.setItem('@favorite', updatedJsonValue);
console.log("favorite removed");
}
catch (e) {
console.log(e);
}
}
export default store;