favs et utilisation du async storage

pull/5/head
Corentin RICHARD 2 years ago
parent 3c08ebeeb4
commit 0e763163c8

@ -5,9 +5,9 @@ import {Classe} from "./Classe";
export class Card { export class Card {
constructor(id: number,name :string, img : string, imgGold : string,fav : boolean = true){//,set : CardSet,type : Type,clas : Classe,rarity : string,cost : number,attack : number, health : number, desc : string,flavor : string,artist : string,collectible : boolean,elite : boolean,race : string, cropImg :string ) { constructor(id: string,name :string, img : string, imgGold : string,fav : boolean = true){//,set : CardSet,type : Type,clas : Classe,rarity : string,cost : number,attack : number, health : number, desc : string,flavor : string,artist : string,collectible : boolean,elite : boolean,race : string, cropImg :string ) {
this._id=id this.id=id
this._name=name this.name=name
//this._set=set //this._set=set
// this._type=type // this._type=type
// this._class=clas // this._class=clas
@ -19,28 +19,19 @@ export class Card {
// this._flavor = flavor // this._flavor = flavor
// this._artist = artist // this._artist = artist
// this._collectible = collectible // this._collectible = collectible
this._img = img this.img = img
this._imgGold = imgGold this.imgGold = imgGold
// this._cropImg = cropImg // this._cropImg = cropImg
this._fav = fav this.fav = fav
} }
// ID // // ID //
private _id : number; public id;
get id(): number {
return this._id
}
// NAME // // NAME //
private _name : string; public name;
get name(): string {
return this._name;
}
set name(value: string) {
this._name = value;
}
// private _set : CardSet; // private _set : CardSet;
@ -132,22 +123,8 @@ export class Card {
// this._collectible = value; // this._collectible = value;
// } // }
private _img : string public img;
public imgGold;
get img(): string {
return this._img;
}
set img(value: string) {
this._img = value ;
}
private _imgGold : string
get imgGold(): string {
return this._imgGold;
}
set imgGold(value: string) {
this._imgGold = value ;
}
// private _cropImg : string // private _cropImg : string
// get cropImg(): string { // get cropImg(): string {
@ -157,11 +134,5 @@ export class Card {
// this._cropImg = value; // this._cropImg = value;
// } // }
private _fav : boolean public fav;
get fav(): boolean{
return this._fav;
}
set fav(value : boolean){
this._fav = value;
}
} }

@ -0,0 +1,9 @@
import { Card } from '../../models/Card';
import {SET_FAVS} from '../constants';
export const setList = (list: []) => {
return {
type: SET_FAVS,
payload: list
};
}

@ -1,15 +0,0 @@
import {ADD_FAVORITE_DATA, FETCH_DATA} from '../constants';
export const setList = (List: String[]) => {
return {
type: FETCH_DATA,
payload: List,
};
}
// export const setFavList = (List: String[]) => {
// return {
// type: ADD_FAVORITE_DATA,
// payload: List,
// };
// }

@ -6,3 +6,5 @@ export const ADD_FAVORITE_DATA = "ADD_FAVORITE_DATA"
export const DISPLAY_ALL_CARD = "DISPLAY_ALL_CARD" export const DISPLAY_ALL_CARD = "DISPLAY_ALL_CARD"
export const SET_FAVS = "SET_FAVS"

@ -1,6 +1,8 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import { Card } from '../../models/Card' import { Card } from '../../models/Card'
import { CardProps } from '../../props/favprops' import { CardProps } from '../../props/favprops'
import {FETCH_DATA, ADD_FAVORITE_DATA} from '../constants' import {FETCH_DATA, ADD_FAVORITE_DATA, SET_FAVS} from '../constants'
import StorageHeart from '../../service/AsyncStorage'
const initialState = { const initialState = {
cards: [], cards: [],
@ -12,26 +14,35 @@ const initialState = {
export default appReducer = (state = initialState, action) => { export default appReducer = (state = initialState, action) => {
switch (action.type) { switch (action.type) {
case ADD_FAVORITE_DATA: case ADD_FAVORITE_DATA:
const a : CardProps = action.payload const a : CardProps = action.payload
if(a.route.bool ==false){ if(a.route.bool ==false){
const t : Card[] = state.favoriteCards if(state.favoriteCards == undefined){
const tab = [a.route.card]
if(t.every((elem) => elem != a.route.card)){ StorageHeart.setItem("favoriteList",tab)
return {...state, favoriteCards : tab};
}
if( Array.from(state.favoriteCards).every((elem) => elem != a.route.card)){
//@ts-ignore //@ts-ignore
const tab = state.favoriteCards.concat([a.route.card]) const tab = state.favoriteCards.concat([a.route.card])
console.log(state.favoriteCards) console.log(state.favoriteCards)
StorageHeart.setItem("favoriteList",tab)
return {...state, favoriteCards : tab}; return {...state, favoriteCards : tab};
} }
return {...state} return {...state}
} }
else{ else{
const tab = state.favoriteCards.filter((item) => item!= a.route.card) const tab = state.favoriteCards.filter((item) => item!= a.route.card)
StorageHeart.setItem("favoriteList",tab)
return {...state, favoriteCards : tab }; return {...state, favoriteCards : tab };
} }
case FETCH_DATA: case FETCH_DATA:
// @ts-ignore
return {...state, cards: action.payload}; return {...state, cards: action.payload};
case SET_FAVS:
//@ts-ignore
console.log("Set_favs : ",action.payload)
return {...state, favoriteCards: action.payload}
default: default:
return state; return state;
} }

@ -4,11 +4,53 @@ import { useNavigation } from '@react-navigation/native';
import { NavigationContainer } from '@react-navigation/native'; import { NavigationContainer } from '@react-navigation/native';
import StackNavigation from '../navigation/StackNavigation' import StackNavigation from '../navigation/StackNavigation'
import { Colors } from 'react-native/Libraries/NewAppScreen'; import { Colors } from 'react-native/Libraries/NewAppScreen';
import { useDispatch } from 'react-redux';
import { useEffect } from 'react';
import { getAllCards } from "../redux/actions/actionSelection"
import StorageHeart from '../service/AsyncStorage';
import { setFavList } from '../redux/actions/action_setFavList';
import { setList } from '../redux/actions/action_setFavs';
import { Card } from '../models/Card';
//import { setList } from '../redux/actions/action_setList';
//import Button from 'react-bootstrap/Button';
// @ts-ignore //(ta gueule pour l'erreur sur navigation)
// @ts-ignore //
export default function HomeScreen({navigation}) { export default function HomeScreen({navigation}) {
const dispatch = useDispatch();
useEffect(() => {
console.log("USEEFFECT")
const loadFavCards = async () => {
//@ts-ignore
//await dispatch(getAllCards());
const list = await StorageHeart.getItem("favoriteList")
console.log("async favs : ",list)
//@ts-ignore
dispatch(setList(list))
};
loadFavCards();
}, [dispatch]);
useEffect(() => {
console.log("USEEFFECT")
const loadCards = async () => {
//@ts-ignore
await dispatch(getAllCards());
};
loadCards();
}, [dispatch]);
return ( return (
<View style={styles.container}> <View style={styles.container}>
<View style={styles.centered}> <View style={styles.centered}>

@ -28,7 +28,7 @@ export default function ListScreen({navigation}){
//@ts-ignore //@ts-ignore
var nList : Card[] = useSelector(state => state.appReducer.favoriteCards); var nList : Card[] = useSelector(state => state.appReducer.favoriteCards);
console.log(nList) console.log(" favs : ",nList)
const [searchValue, setSearchValue] = useState(''); const [searchValue, setSearchValue] = useState('');
@ -53,7 +53,7 @@ export default function ListScreen({navigation}){
}} /> }} />
</TouchableHighlight> </TouchableHighlight>
} }
keyExtractor={(item: Card) => item.id.toString()} keyExtractor={(item: Card) => item.id}
/> />
</View> </View>

@ -34,14 +34,14 @@ export default function ListScreen({navigation}){
// Let's define a hook that will be used to update the rendered state after the return will be called // Let's define a hook that will be used to update the rendered state after the return will be called
// You cannot perform side-effects outside of a useEffect hook // You cannot perform side-effects outside of a useEffect hook
useEffect(() => { // useEffect(() => {
console.log("USEEFFECT") // console.log("USEEFFECT")
const loadCards = async () => { // const loadCards = async () => {
//@ts-ignore // //@ts-ignore
await dispatch(getAllCards()); // await dispatch(getAllCards());
}; // };
loadCards(); // loadCards();
}, [dispatch]); // }, [dispatch]);
//* Search : //* Search :

@ -7,6 +7,7 @@ export default class StorageHeart {
static async getItem(key: string): Promise<any> { static async getItem(key: string): Promise<any> {
try { try {
const value = await AsyncStorage.getItem(key); const value = await AsyncStorage.getItem(key);
console.log("load")
if (value !== null) { if (value !== null) {
return JSON.parse(value); return JSON.parse(value);
} }
@ -19,24 +20,9 @@ export default class StorageHeart {
static async setItem(key: string, value: any): Promise<void> { static async setItem(key: string, value: any): Promise<void> {
try { try {
await AsyncStorage.setItem(key, JSON.stringify(value)); await AsyncStorage.setItem(key, JSON.stringify(value));
console.log("save")
} catch (e) { } catch (e) {
console.error(`AsyncStorage setItem error: ${e}`); console.error(`AsyncStorage setItem error: ${e}`);
} }
} }
static async removeItem(key: string): Promise<void> {
try {
await AsyncStorage.removeItem(key);
} catch (e) {
console.error(`AsyncStorage removeItem error: ${e}`);
}
}
static async clear(): Promise<void> {
try {
await AsyncStorage.clear();
} catch (e) {
console.error(`AsyncStorage clear error: ${e}`);
}
}
} }

Loading…
Cancel
Save