Adding and removing favs

pull/5/head
Corentin RICHARD 2 years ago
parent 88a6612e77
commit 3c08ebeeb4

@ -1,3 +1,4 @@
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} from '../constants'
@ -13,11 +14,17 @@ export default appReducer = (state = initialState, action) => {
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(t.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)
return {...state, favoriteCards : tab}; return {...state, favoriteCards : tab};
} }
return {...state}
}
else{ else{
const tab = state.favoriteCards.filter((item) => item!= a.route.card) const tab = state.favoriteCards.filter((item) => item!= a.route.card)
return {...state, favoriteCards : tab }; return {...state, favoriteCards : tab };

@ -27,7 +27,7 @@ import Item from '../components/ListItemComponent';
export default function ListScreen({navigation}){ export default function ListScreen({navigation}){
//@ts-ignore //@ts-ignore
let nList = useSelector(state => state.appReducer.favoriteCards); var nList : Card[] = useSelector(state => state.appReducer.favoriteCards);
console.log(nList) console.log(nList)
const [searchValue, setSearchValue] = useState(''); const [searchValue, setSearchValue] = useState('');
@ -51,7 +51,6 @@ export default function ListScreen({navigation}){
card: item, card: item,
bool: true bool: true
}} /> }} />
<Text>"a"</Text>
</TouchableHighlight> </TouchableHighlight>
} }
keyExtractor={(item: Card) => item.id.toString()} keyExtractor={(item: Card) => item.id.toString()}

Loading…
Cancel
Save