From 7655c9faba821913afa2a93f2565b91b538a5321 Mon Sep 17 00:00:00 2001 From: savaughn Date: Sun, 5 May 2019 23:41:39 -0500 Subject: [PATCH] added click to remove from favorites list --- package.json | 1 + src/components/FavoritesList/favoritesList.js | 26 ++++++++++++++-- src/state/ActionTypes.js | 5 ++- .../sagas/favoritesSaga/favoritesSaga.js | 31 +++++++++++++++++-- src/state/sagas/historySaga/historySaga.js | 2 +- src/state/sagas/triviaSaga/triviaSaga.js | 2 +- 6 files changed, 58 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 2d8531c..3470b2c 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "android": "react-native run-android" }, "dependencies": { + "lodash": "^4.17.11", "native-base": "^2.12.1", "react": "16.8.3", "react-native": "0.59.5", diff --git a/src/components/FavoritesList/favoritesList.js b/src/components/FavoritesList/favoritesList.js index 2a32392..24d3e61 100644 --- a/src/components/FavoritesList/favoritesList.js +++ b/src/components/FavoritesList/favoritesList.js @@ -1,11 +1,27 @@ import React, { Component } from 'react'; import { FlatList, TouchableOpacity, Text, View } from 'react-native'; import { refreshScreen } from '../../navigator/navigateTo'; +import { REMOVE_FROM_FAVORITES } from '../../state/ActionTypes'; +import {connect} from "react-redux"; -class Header extends Component { + +class FavoritesList extends Component { keyExtractor = (item) => item.id.toString(); + renderItem = ({item}) => ( + { + this.props.dispatch({ + type: REMOVE_FROM_FAVORITES, + payload: {item, favArray: this.props.favorites}, + }); + } } + > + {`${item.text}`} + + ); + render() { return ( @@ -14,7 +30,7 @@ class Header extends Component { {`${item.text}`}} + renderItem={ this.renderItem } onRefresh={() => refreshScreen()} refreshing={this.props.refreshing} /> @@ -28,7 +44,11 @@ class Header extends Component { } } -export default Header; +const mapDispatchToProps = dispatch => ({ + dispatch, +}); + +export default connect(mapDispatchToProps)(FavoritesList); diff --git a/src/state/ActionTypes.js b/src/state/ActionTypes.js index c88e39c..ce8e382 100755 --- a/src/state/ActionTypes.js +++ b/src/state/ActionTypes.js @@ -16,4 +16,7 @@ export const GET_RANDOM_TRIVIA_FAILURE = 'get_random_trivia_failure'; // Favorites export const SAVE_TO_FAVORITES = 'save_to_favorites'; export const SAVE_TO_FAVORITES_SUCCESS = 'save_to_favorites_success'; -export const SAVE_TO_FAVORITES_FAILURE = 'save_to_favorites_failure'; \ No newline at end of file +export const SAVE_TO_FAVORITES_FAILURE = 'save_to_favorites_failure'; +export const REMOVE_FROM_FAVORITES = 'remove_from_favorites'; +export const REMOVE_FROM_FAVORITES_SUCCESS = 'remove_from_favorites_success'; +export const REMOVE_FROM_FAVORITES_FAILURE = 'remove_from_favorites_failure'; \ No newline at end of file diff --git a/src/state/sagas/favoritesSaga/favoritesSaga.js b/src/state/sagas/favoritesSaga/favoritesSaga.js index 1192d2e..f06135b 100644 --- a/src/state/sagas/favoritesSaga/favoritesSaga.js +++ b/src/state/sagas/favoritesSaga/favoritesSaga.js @@ -1,12 +1,37 @@ import React from 'react'; import { call, takeLatest, put } from 'redux-saga/effects'; -import { SAVE_TO_FAVORITES, SAVE_TO_FAVORITES_SUCCESS, SAVE_TO_FAVORITES_FAILURE } from '../../ActionTypes'; +import _ from 'lodash'; +import { + SAVE_TO_FAVORITES, + SAVE_TO_FAVORITES_SUCCESS, + SAVE_TO_FAVORITES_FAILURE, + REMOVE_FROM_FAVORITES, + REMOVE_FROM_FAVORITES_SUCCESS, + REMOVE_FROM_FAVORITES_FAILURE, +} from '../../ActionTypes'; function* sagaWatcher() { - yield takeLatest(SAVE_TO_FAVORITES, saga); + yield takeLatest(SAVE_TO_FAVORITES, saveItem); + yield takeLatest(REMOVE_FROM_FAVORITES, deleteItem) } -function* saga({ payload }) { +function* deleteItem({ payload }) { + try { + const favArray = yield call(removeItemFromArray, payload); + yield put({ type: REMOVE_FROM_FAVORITES_SUCCESS, favArray}); + } catch (err) { + yield put({ type: REMOVE_FROM_FAVORITES_FAILURE }); + } +} + +function removeItemFromArray(payload) { + console.log('del', payload); + _.remove(payload.favArray, function(itemInArray) { + return itemInArray === payload.item; + }); +} + +function* saveItem({ payload }) { console.log('saga', payload); try { const favArray = yield call(saveItemToArray, payload); diff --git a/src/state/sagas/historySaga/historySaga.js b/src/state/sagas/historySaga/historySaga.js index b9af031..aff1903 100644 --- a/src/state/sagas/historySaga/historySaga.js +++ b/src/state/sagas/historySaga/historySaga.js @@ -29,7 +29,7 @@ function getHistoryFact() { unfilteredEventArray.map((item) => { data.filteredEvents.push({ type: 'history', - id: index, + id: `h${index}`, year: item.year, text: item.text, }); diff --git a/src/state/sagas/triviaSaga/triviaSaga.js b/src/state/sagas/triviaSaga/triviaSaga.js index 7cce999..c3e231d 100644 --- a/src/state/sagas/triviaSaga/triviaSaga.js +++ b/src/state/sagas/triviaSaga/triviaSaga.js @@ -26,7 +26,7 @@ function getRandomTrivia() { results.map((item) => { filteredTrivia.push({ type: 'trivia', - id: index, + id: `t${index}`, category: item.category, question: item.question, correctAnswer: item.correct_answer,