Skip to content

Commit

Permalink
add history / trivia to favorites list
Browse files Browse the repository at this point in the history
  • Loading branch information
savaughn committed May 6, 2019
1 parent cfba371 commit 90fd515
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 22 deletions.
30 changes: 21 additions & 9 deletions src/screens/historyScreen/historyScreen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import {FlatList, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import { connect } from 'react-redux';
import { GET_HISTORY_FACT } from "../../state/ActionTypes";
import { GET_HISTORY_FACT, SAVE_TO_FAVORITES } from "../../state/ActionTypes";

class HistoryScreen extends Component {
constructor(props) {
Expand All @@ -20,22 +20,33 @@ class HistoryScreen extends Component {
});
};

keyExtractor = (item) => item.index.toString();
saveToFavorite = (item) => {
this.props.dispatch({
type: SAVE_TO_FAVORITES,
payload: {item, favArray: this.props.favorites},
});
};

keyExtractor = (item) => item.id.toString();

renderItem = ({item}) => (
<TouchableOpacity
onPress={ () => this.saveToFavorite(item) }
>
<Text style={ {margin: 10 } }>{`${item.year}: ${item.text}`}</Text>
</TouchableOpacity>
);


render() {
console.log(this.props.event);
return (
<View style={styles.container}>
<TouchableOpacity
onPress={()=> this.getHistory()}
>
<Text style={styles.item}>{this.props.event.currentDate}</Text>
</TouchableOpacity>
<Text style={styles.item}>{this.props.event.currentDate}</Text>
<FlatList
data={ this.props.event.filteredEvents }
keyExtractor={this.keyExtractor}
renderItem={({item}) => <Text style={ {margin: 10 } }>{`${item.year}: ${item.text}\n`}</Text>}
renderItem={ this.renderItem }
onRefresh={()=> this.getHistory()}
refreshing={ this.props.refreshing }
/>
Expand All @@ -44,9 +55,10 @@ class HistoryScreen extends Component {
}
}

const mapStateToProps = ({ randomHistory }) => ({
const mapStateToProps = ({ randomHistory, favorites }) => ({
event: randomHistory.event,
refreshing: randomHistory.refreshing,
favorites: favorites.favorites,
});

const mapDispatchToProps = dispatch => ({
Expand Down
24 changes: 20 additions & 4 deletions src/screens/triviaScreen/triviaScreen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import {FlatList, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import { connect } from 'react-redux';
import { GET_RANDOM_TRIVIA } from '../../state/ActionTypes';
import { GET_RANDOM_TRIVIA, SAVE_TO_FAVORITES } from '../../state/ActionTypes';

class TriviaScreen extends Component {
constructor(props) {
Expand All @@ -20,7 +20,22 @@ class TriviaScreen extends Component {
});
};

keyExtractor = (item) => item.index.toString();
saveToFavorite = (item) => {
this.props.dispatch({
type: SAVE_TO_FAVORITES,
payload: {item, favArray: this.props.favorites},
});
};

keyExtractor = (item) => item.id.toString();

renderItem = ({item}) => (
<TouchableOpacity
onPress={ () => this.saveToFavorite(item) }
>
<Text style={ {margin: 10 } }>{`${item.category}: ${item.question}\nCorrect Answer:${item.correctAnswer}\nMultiple Choice: ${item.multipleChoice}`}</Text>
</TouchableOpacity>
);


render() {
Expand All @@ -29,7 +44,7 @@ class TriviaScreen extends Component {
<FlatList
data={ this.props.trivia }
keyExtractor={this.keyExtractor}
renderItem={({item}) => <Text style={ {margin: 10 } }>{`${item.category}: ${item.question}\nCorrect Answer:${item.correctAnswer}\nMultiple Choice: ${item.multipleChoice}`}</Text>}
renderItem={ this.renderItem }
onRefresh={()=> this.getTrivia()}
refreshing={ this.props.refreshing }
/>
Expand All @@ -38,9 +53,10 @@ class TriviaScreen extends Component {
}
}

const mapStateToProps = ({ randomTrivia }) => ({
const mapStateToProps = ({ randomTrivia, favorites }) => ({
trivia: randomTrivia.trivia,
refreshing: randomTrivia.refreshing,
favorites: favorites.favorites,
});

const mapDispatchToProps = dispatch => ({
Expand Down
32 changes: 27 additions & 5 deletions src/state/sagas/favoritesSaga/favoritesSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,42 @@ function* sagaWatcher() {
}

function* saga({ payload }) {
console.log('fav pay', payload);
console.log('saga', payload);
try {
const favArray = yield call(saveItemToArray, payload);
console.log('done', favArray);
yield put({ type: SAVE_TO_FAVORITES_SUCCESS, favArray });
} catch (err) {
console.log('fail');
yield put({ type: SAVE_TO_FAVORITES_FAILURE });
}
}

function saveItemToArray(payload) {
payload.favArray.push({
id: payload.item.id,
text: `${payload.item.setup}\n${payload.item.punchline}`,
});
console.log('payload', payload);
const type = payload.item.type;
switch(type) {
case 'history': {
payload.favArray.push({
id: payload.item.id,
text: `${payload.item.year}: ${payload.item.text}\n`,
});
break;
}
case 'trivia': {
payload.favArray.push({
id: payload.item.id,
text: `${payload.item.category}: ${payload.item.question}\nCorrect Answer:${payload.item.correctAnswer}\nMultiple Choice: ${payload.item.multipleChoice}`,
});
break;
}
default: {
payload.favArray.push({
id: payload.item.id,
text: `${payload.item.setup}\n${payload.item.punchline}`,
});
}
}
return payload.favArray;
}

Expand Down
4 changes: 2 additions & 2 deletions src/state/sagas/historySaga/historySaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ function getHistoryFact() {
const unfilteredEventArray = json.data.Events;
unfilteredEventArray.map((item) => {
data.filteredEvents.push({
index,
type: 'history',
id: index,
year: item.year,
text: item.text,
});
index++;
});
console.log(data);
return data;
});
}
Expand Down
1 change: 0 additions & 1 deletion src/state/sagas/jokeSaga/jokeSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function* sagaWatcher() {
function* saga() {
try {
const joke = yield call(getRandomJoke);
console.log(joke);
yield put({ type: GET_RANDOM_JOKE_SUCCESS, payload: joke });
} catch (err) {
yield put({ type: GET_RANDOM_JOKE_FAILURE });
Expand Down
3 changes: 2 additions & 1 deletion src/state/sagas/triviaSaga/triviaSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function getRandomTrivia() {
const results = json.results;
results.map((item) => {
filteredTrivia.push({
index,
type: 'trivia',
id: index,
category: item.category,
question: item.question,
correctAnswer: item.correct_answer,
Expand Down

0 comments on commit 90fd515

Please sign in to comment.