favorite update

pull/5/head
Corentin RICHARD 2 years ago
parent 731c69ccc6
commit 88a6612e77

@ -1,17 +1,53 @@
import { View } from 'react-native';
import { Image } from 'react-native';
import { useDispatch } from "react-redux";
import { Card } from "../models/Card";
import { setFavList } from "../redux/actions/action_setFavList";
import { ImageBackground, TouchableHighlight, View } from "react-native";
import React, { useEffect } from "react";
import {CardProps} from "../props/favprops"
import { FontAwesome } from '@expo/vector-icons';
import { StyleSheet} from 'react-native';
export default function Item(props: CardProps){ // a mettre dans components et definir une props pour passer le param
const {route} = props;
const item: Card = route.card;
const bool: boolean = route.bool;
const dispatch = useDispatch()
type ItemProps = {
url : string //Image URL
}
const HandleAddFav = (props : CardProps) => {
console.log("add new card")
dispatch(setFavList(props));
}
return(
export function ListItemComponent(props : ItemProps){
return (
<View>
<Image
source={{uri: props.url}}
style={{flex:1, minHeight:250, minWidth:180}}/>
</View>
)
/* dispatch , */
<View style={styles.item}>
<ImageBackground source={{uri:item.img}} style={{flex:1, minHeight:250, minWidth:180}}>
<TouchableHighlight style={item.fav?styles.favoriteButtonFav:styles.favoriteButtonNonFav} onPress={() => HandleAddFav(props)} >
<FontAwesome name="heart-o" size={50} color="#fff" />
</TouchableHighlight>
</ImageBackground>
</View>
);
}
const styles = StyleSheet.create({
item: {
},
favoriteButtonNonFav: {
position: 'absolute',
top: 10,
right: 10,
backgroundColor: 'red',
borderRadius: 50,
padding: 10,
},
favoriteButtonFav: {
position: 'absolute',
top: 10,
right: 10,
backgroundColor: 'red',
borderRadius: 50,
padding: 10,
},});

@ -0,0 +1,8 @@
import { Card } from "../models/Card";
export interface CardProps{
route : {
card: Card;
bool: boolean;
}
}

@ -25,7 +25,7 @@ import { Card } from "../../models/Card";
// constructor(cardId : String, name : String, manaCost : number, attack : number, health : number, desc : String){
// this.cardId = cardId;
// this.name = name;
// this.manaCost = manaCost;
// this.manaCost = manaCost;ADD_FAVORITE_DATA, FETCH_DATA,
// this.attack = attack;
// this.health = health;
// this.desc = desc;
@ -50,7 +50,7 @@ export const getAllCards = () => {
// 'content-length':'9505',
// 'content-type':'application/json; charset=utf-8',
// 'etag':'W/"74bb-QMT8DIj6saBS1wT4u5WWcEmZAdw"'
// }
// }ADD_FAVORITE_DATA, FETCH_DATA,
//! Actualisation de l'API (16/03) :
headers: {
@ -61,7 +61,7 @@ export const getAllCards = () => {
//'pageSize':'100'
};
//! Actualisation de l'API (16/03) :
const CardsPromise = await fetch('https://us.api.blizzard.com/hearthstone/cards?locale=en_US&access_token=EU1AbKy9Q7sOUjQYC5AFF1O1JPuoAgCNxk', options);
const CardsPromise = await fetch('https://us.api.blizzard.com/hearthstone/cards?locale=en_US&access_token=EUe6p4N9uLm8BbsHyYVZXIa4DDBP2hMR05', options);
//const CardsPromise = await fetch('https://us.api.blizzard.com/hearthstone/cards?locale=en_US&access_token=EURTWhjBC2SRb4Ez42BT1kx8R2NcJc07kL', options);
//console.log("FETCH")
//console.log(CardsPromise)

@ -1,8 +1,10 @@
import { Card } from '../../models/Card';
import { CardProps } from '../../props/favprops';
import {ADD_FAVORITE_DATA, FETCH_DATA} from '../constants';
export const setFavList = (List: String[]) => {
export const setFavList = (props : CardProps) => {
return {
type: ADD_FAVORITE_DATA,
payload: List,
payload: props,
};
}

@ -4,4 +4,5 @@ export const FETCH_DATA = "FETCH_DATA"
export const ADD_FAVORITE_DATA = "ADD_FAVORITE_DATA"
export const DISPLAY_ALL_CARD = "DISPLAY_ALL_CARD"
export const DISPLAY_ALL_CARD = "DISPLAY_ALL_CARD"

@ -1,10 +1,9 @@
import { CardProps } from '../../props/favprops'
import {FETCH_DATA, ADD_FAVORITE_DATA} from '../constants'
const initialState = {
cards: [],
favoriteCards: [],
// cards: ["C_ace", "C_K", "C_Q", "C_J"],
// favoriteCards: [ "C_ace", "C_K"],
favoriteCards: []
}
@ -12,8 +11,17 @@ const initialState = {
export default appReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_FAVORITE_DATA:
// @ts-ignore
return {...state, favoriteCards: state.favoriteCards.push(action.payload)};
const a : CardProps = action.payload
if(a.route.bool ==false){
//@ts-ignore
const tab = state.favoriteCards.concat([a.route.card])
console.log(state.favoriteCards)
return {...state, favoriteCards : tab};
}
else{
const tab = state.favoriteCards.filter((item) => item!= a.route.card)
return {...state, favoriteCards : tab };
}
case FETCH_DATA:
// @ts-ignore
return {...state, cards: action.payload};

@ -1,91 +1,64 @@
import { StyleSheet, Text, View, Button, FlatList } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import React, { useState } from "react";
import {setFavList } from '../redux/actions/action_setFavList';
//redux
import { StyleSheet, Text, View, Button, TouchableHighlight, TextInput, ImageBackground } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from "react";
import { FlatList } from 'react-native-gesture-handler';
import {useDispatch, useSelector} from 'react-redux';
import {useEffect} from 'react';
import { FontAwesome } from '@expo/vector-icons';
import { ThunkAction } from 'redux-thunk';
export const Cardslist = [
{
id: '1',
title: "premier élément",
},
{
id: '2',
title: "second élément",
},
{
id: '3',
title: "élément",
},
{
id: '4',
title: "barman douteux",
},
{
id: '10',
title: "dernier élément",
}
];
//? possiblement à supprimer
import { getAllCards } from "../redux/actions/actionSelection"
import { Card } from '../models/Card';
import { Image } from 'react-native';
import { ImageURISource } from 'react-native';
import Item from '../components/ListItemComponent';
//* Icons
// import { BiSearchAlt } from 'react-icons';
//@ts-ignore
const Item = ({title}) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);
//TODO
// export const getFavList = () => {
// //In order to use await your callback must be asynchronous using async keyword.
// return async dispatch => {
// //Then perform your asynchronous operations.
// try {
// //Have it first fetch data from our starwars url.
// //const nounoursPromise = await fetch('https://iut-weather-api.azurewebsites.net/nounours');
// //Then use the json method to get json data from api/
// //const nounoursListJson = await nounoursPromise.json();
// //const nounoursList: Nounours[] = nounoursListJson.map(elt => new Nounours(elt["name"], elt["age"], elt["nbPoils"], elt["image"]));
// dispatch(setFavList(Array{id,title}));
// } catch (error) {
// console.log('Error---------', error);
// //You can dispatch to another action if you want to display an error message in the application
// //dispatch(fetchDataRejected(error))
// }
// }
// }
//* Components
//@ts-ignore
export default function ListScreen({navigation}){
//@ts-ignore
let nList = useSelector(state => state.appReducer.favoriteCards);
console.log(nList)
const [searchValue, setSearchValue] = useState('');
export default function Main(){
const [count, setCount] = useState(0);
return (
<View style={styles.container}>
<Text>Maman, J4AI UNE LISTE DE FAVORIS ! !</Text>
<StatusBar style="auto" />
<Text>{count}</Text>
<Button onPress={()=> setCount(count+1)} title="+1"/>
<Button onPress={()=> setCount(count+2)} title="+2"/>
<Button onPress={()=> setCount(count+10)} title="+10"/>
<FlatList data={Cardslist}
renderItem={({item}) => <Item title={item.title} />}
keyExtractor={item => item.id}/>
<TextInput
style={styles.textInput}
value={searchValue}
onChangeText={text => setSearchValue(text)}
placeholder="Rechercher une carte..."
/>
<FlatList
numColumns={2}
data={nList}
renderItem={({item}) =>
<TouchableHighlight onPress={() => navigation.navigate("ListFav")}>
<Item route={{
card: item,
bool: true
}} />
<Text>"a"</Text>
</TouchableHighlight>
}
keyExtractor={(item: Card) => item.id.toString()}
/>
</View>
);
}
@ -94,17 +67,18 @@ export default function Main(){
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
backgroundColor: '#ac9585',
alignItems: 'center',
justifyContent: 'center',
justifyContent: 'space-evenly',
},
item: {
textInput: {
padding: 15,
margin: 5,
width:200,
backgroundColor: '#ffffff',
borderRadius : 15,
backgroundColor: '#efefef',
padding: 20,
margin : 10,
},
title: {
fontStyle: "italic",
shadowColor: 'grey',
textAlign:'center'
}
});

@ -1,5 +1,5 @@
import { StyleSheet, Text, View, Button, TouchableHighlight, TextInput, ImageBackground } from 'react-native';
import { StyleSheet, Text, View, Button, TouchableHighlight, TextInput, ImageBackground, LogBox } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from "react";
import { FlatList } from 'react-native-gesture-handler';
@ -18,37 +18,16 @@ import { ImageURISource } from 'react-native';
// import { BiSearchAlt } from 'react-icons';
//* Components
import {ListItemComponent} from '../components/ListItemComponent'
//@ts-ignore
const Item = ({url,item}) => { // a mettre dans components et definir une props pour passer le param
const HandleAddFav = () => {
item.fav = !item.fav
}
return(
<View style={styles.item}>
<ImageBackground source={{uri:url}} style={{flex:1, minHeight:250, minWidth:180}}>
<TouchableHighlight style={item.fav?styles.favoriteButtonFav:styles.favoriteButtonNonFav} onPress={HandleAddFav}>
<FontAwesome name="heart-o" size={50} color="#fff" />
</TouchableHighlight>
</ImageBackground>
</View>
);
}
import { setFavList } from '../redux/actions/action_setFavList';
import Item from '../components/ListItemComponent';
//@ts-ignore
export default function ListScreen({navigation}){
const [count, setCount] = useState(0);
// // Initialize the binding content with the application initial state
//@ts-ignore
const nList = useSelector(state => state.appReducer.cards);
var nList = useSelector(state => state.appReducer.cards);
// Create a const that will hold the react-redux events dispatcher
const dispatch = useDispatch();
@ -65,12 +44,6 @@ export default function ListScreen({navigation}){
}, [dispatch]);
// const req = fetch('https://omgvamp-hearthstone-v1.p.rapidapi.com/cards')
//https://us.api.blizzard.com/hearthstone/cards/678?locale=en_US
//* Search :
const [searchValue, setSearchValue] = useState('');
@ -92,7 +65,10 @@ export default function ListScreen({navigation}){
data={filteredList}
renderItem={({item}) =>
<TouchableHighlight onPress={() => navigation.navigate("ListFav")}>
<Item url={item.img} item={item}/>
<Item route={{
card: item,
bool: false
}} ></Item>
</TouchableHighlight>
}
keyExtractor={(item: Card) => item.id.toString()}
@ -113,38 +89,6 @@ const styles = StyleSheet.create({
justifyContent: 'space-evenly',
},
border: {
flex: 1,
backgroundColor: '#ff0000',
maxHeight : 100,
borderWidth : 15,
borderRadius : 15,
borderColor : '#00ffaa',
alignItems: 'center',
justifyContent: 'center',
},
item: {
},
favoriteButtonNonFav: {
position: 'absolute',
top: 10,
right: 10,
backgroundColor: 'red',
borderRadius: 50,
padding: 10,
},
favoriteButtonFav: {
position: 'absolute',
top: 10,
right: 10,
backgroundColor: 'red',
borderRadius: 50,
padding: 10,
},
title: {
fontStyle: "italic",
},
textInput: {
padding: 15,
margin: 5,
@ -155,3 +99,4 @@ const styles = StyleSheet.create({
textAlign:'center'
}
});

@ -2,7 +2,7 @@ import { Card } from "../models/Card";
import AsyncStorage from "@react-native-async-storage/async-storage";
export default class Storage {
export default class StorageHeart {
static async getItem(key: string): Promise<any> {
try {

Loading…
Cancel
Save