Skip to content

Commit

Permalink
header / favorites list components
Browse files Browse the repository at this point in the history
  • Loading branch information
savaughn committed May 6, 2019
1 parent 7a38830 commit cfba371
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 66 deletions.
34 changes: 34 additions & 0 deletions src/components/FavoritesList/favoritesList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { Component } from 'react';
import { FlatList, TouchableOpacity, Text, View } from 'react-native';
import { refreshScreen } from '../../navigator/navigateTo';

class Header extends Component {

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

render() {
return (
<View style={{ alignItems: 'center', justifyContent: 'center' }}>
{
this.props.favorites &&
<FlatList
data={this.props.favorites}
keyExtractor={this.keyExtractor}
renderItem={({item}) => <Text style={{margin: 10}}>{`${item.text}`}</Text>}
onRefresh={() => refreshScreen()}
refreshing={this.props.refreshing}
/>
}
{
!this.props.favorites.length &&
<Text>Favorites List</Text>
}
</View>
);
}
}

export default Header;



42 changes: 42 additions & 0 deletions src/components/Header/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { Component } from 'react';
import {TouchableOpacity, View, Text, StyleSheet} from 'react-native';
import { navigateTo } from '../../navigator/navigateTo';

class Header extends Component {

render() {
return (
<View style={ styles.header}>
<TouchableOpacity
onPress={()=> navigateTo('jokeScreen')}
>
<Text style={styles.items}>Jokes</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={()=> navigateTo('historyScreen')}
>
<Text style={styles.items}>Today in History</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={()=> navigateTo('triviaScreen')}
>
<Text style={styles.items}>Trivia</Text>
</TouchableOpacity>
</View>
);
}
}

const styles = StyleSheet.create({
header: {
flexDirection: 'row',
justifyContent: 'space-evenly',
},
items: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

export default Header;
19 changes: 19 additions & 0 deletions src/navigator/navigateTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Actions} from 'react-native-router-flux';

export function navigateTo(screen, method = 'push') {
switch(method.toLowerCase()){
case 'replace':
Actions.replace(screen);
break;
case 'reset':
Actions.reset(screen);
break;
default:
Actions.push(screen);
break;
}
}

export function refreshScreen(props) {
Actions.refresh(props)
}
6 changes: 3 additions & 3 deletions src/screens/historyScreen/historyScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class HistoryScreen extends Component {
}

componentDidMount() {
this.props.dispatch({
type: GET_HISTORY_FACT,
});
if (!this.props.event.length) {
this.getHistory();
}
}

getHistory = () => {
Expand Down
59 changes: 8 additions & 51 deletions src/screens/homeScreen/homeScreen.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,30 @@
import React, { Component } from 'react';
import {FlatList, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import { connect } from 'react-redux';
import { Actions } from 'react-native-router-flux';
import Header from '../../components/Header/header';
import FavoritesList from '../../components/FavoritesList/favoritesList';

class HomeScreen extends Component {
constructor(props) {
super(props);
}

goToJokeScreen = () => {
Actions.push('jokeScreen');
};

goToHistoryScreen = () => {
Actions.push('historyScreen');
};

goToTriviaScreen = () => {
Actions.push('triviaScreen');
};

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

render() {
console.log('home', this.props.favorites);
return (
<View style={styles.container}>
<View style={ styles.header}>
<TouchableOpacity
onPress={()=> this.goToJokeScreen()}
>
<Text style={styles.items}>Jokes</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={()=> this.goToHistoryScreen()}
>
<Text style={styles.items}>Today in History</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={()=> this.goToTriviaScreen()}
>
<Text style={styles.items}>Trivia</Text>
</TouchableOpacity>
</View>
{
this.props.favorites &&
<FlatList
data={ this.props.favorites }
keyExtractor={this.keyExtractor}
renderItem={({item}) => <Text style={ {margin: 10 } }>{`${item.text}`}</Text>}
/>
}
<Header />
<FavoritesList
favorites={ this.props.favorites }
refreshing={ this.props.refreshing }
/>
</View>
);
}
}

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

const styles = StyleSheet.create({
Expand All @@ -66,16 +33,6 @@ const styles = StyleSheet.create({
backgroundColor: '#F5FCFF',
marginTop: 50,
},
header: {
flexDirection: 'row',
justifyContent: 'space-evenly',
},
items: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});


export default connect(mapStateToProps)(HomeScreen);
9 changes: 4 additions & 5 deletions src/screens/jokeScreen/jokeScreen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { Component } from 'react';
import {FlatList, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import { connect } from 'react-redux';
import { Actions } from 'react-native-router-flux';
import {GET_RANDOM_JOKE, SAVE_TO_FAVORITES} from "../../state/ActionTypes";


Expand All @@ -11,9 +10,9 @@ class JokeScreen extends Component {
}

componentDidMount() {
this.props.dispatch({
type: GET_RANDOM_JOKE,
});
if (!this.props.jokes.length) {
this.getJoke();
}
}

getJoke = () => {
Expand Down Expand Up @@ -57,7 +56,7 @@ class JokeScreen extends Component {
const mapStateToProps = ({ randomJoke, favorites }) => ({
jokes: randomJoke.jokes,
refreshing: randomJoke.refreshing,
favorites: favorites.favorites,
favorites: favorites.favorites,
});

const mapDispatchToProps = dispatch => ({
Expand Down
6 changes: 3 additions & 3 deletions src/screens/triviaScreen/triviaScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class TriviaScreen extends Component {
}

componentDidMount() {
this.props.dispatch({
type: GET_RANDOM_TRIVIA,
});
if (!this.props.trivia.length) {
this.getTrivia();
}
}

getTrivia = () => {
Expand Down
10 changes: 6 additions & 4 deletions src/state/reducers/favoritesReducer/favoritesReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { SAVE_TO_FAVORITES, SAVE_TO_FAVORITES_SUCCESS, SAVE_TO_FAVORITES_FAILURE
export const INITIAL_STATE = {
favorites: [],
error: false,
refreshing: false,
};

export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case SAVE_TO_FAVORITES:
return {...state};
case SAVE_TO_FAVORITES_SUCCESS:
return { ...state, favorites: action.favArray, error: false };
return {...state, refreshing: true };
case SAVE_TO_FAVORITES_SUCCESS: {
return {...state, favorites: action.favArray, error: false, refreshing: false };
}
case SAVE_TO_FAVORITES_FAILURE:
return { ...state, error: true };
return { ...state, error: true, refreshing: false };
default:
return { ...state };
}
Expand Down

0 comments on commit cfba371

Please sign in to comment.