Skip to content

Commit

Permalink
Redux integration & faker + json server
Browse files Browse the repository at this point in the history
  • Loading branch information
Praneet Prakash Rohida authored and Praneet Prakash Rohida committed Aug 31, 2017
1 parent 938e1fe commit 49a66eb
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 20 deletions.
Binary file modified .DS_Store
Binary file not shown.
7 changes: 5 additions & 2 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import HomeScreen from "./screens/home";
import ProfileScreen from "./screens/profile";
import { Root } from "native-base";

import { Provider } from "react-redux";
import store from "./store";

const AppNavigator = StackNavigator(
{
Login: { screen: LoginScreen },
Expand Down Expand Up @@ -45,9 +48,9 @@ export default class App extends Component {
return <Expo.AppLoading />;
} else
return (
<Root>
<Provider store={store}>
<AppNavigator />
</Root>
</Provider>
);
}
}
6 changes: 6 additions & 0 deletions actions/loginActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function setUsername(username) {
return function(dispatch) {
console.log("In set username reducer");
dispatch({ type: "SET_USERNAME", payload: { username: username } });
};
}
20 changes: 20 additions & 0 deletions actions/tweetsActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from "axios";
import { connect } from "react-redux";

export function fetchTweets() {
return function(dispatch) {
dispatch({ type: "FETCH_TWEETS" });
axios
// .get("https://jsonplaceholder.typicode.com/posts")
.get("http://10.0.1.75:3000/tweets")
.then(response => {
dispatch({
type: "FETCH_TWEETS_FULFILLED",
payload: response.data
});
})
.catch(err => {
dispatch({ type: "FETCH_TWEETS_REJECTED", payload: err });
});
};
}
2 changes: 1 addition & 1 deletion generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function() {
name: faker.name.findName(),
username: faker.internet.userName(),
avatar: faker.image.avatar(),
cover: faker.image.abstract(),
cover: faker.image.image(),
bio: faker.lorem.sentence(),
location: faker.address.city(),
following: faker.random.number(1000),
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@expo/vector-icons": "^5.2.0",
"axios": "^0.16.2",
"color": "^2.0.0",
"expo": "^20.0.0",
"faker": "^4.1.0",
Expand All @@ -30,6 +31,10 @@
"react-native": "^0.47.0",
"react-native-svg": "^5.4.1",
"react-navigation": "^1.0.0-beta.11",
"redux": "^3.7.2"
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-promise-middleware": "^4.4.1",
"redux-thunk": "^2.2.0"
}
}
9 changes: 9 additions & 0 deletions reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { combineReducers } from "redux";

import tweets from "./tweetsReducer";
import login from "./loginReducer";

export default combineReducers({
tweets,
login
});
17 changes: 17 additions & 0 deletions reducers/loginReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default function reducer(
state = {
username: ""
},
action
) {
switch (action.type) {
case "SET_USERNAME": {
console.log("In set username reducer");
return { ...state, username: action.payload.username };
break;
}
default: {
return state;
}
}
}
33 changes: 33 additions & 0 deletions reducers/tweetsReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default function reducer(
state = {
tweets: [],
fetching: false,
fetched: false,
error: null
},
action
) {
switch (action.type) {
case "FETCH_TWEETS": {
return { ...state, fetching: true };
break;
}
case "FETCH_TWEETS_REJECTED": {
return { ...state, fetching: false, error: action.payload };
break;
}
case "FETCH_TWEETS_FULFILLED": {
return {
...state,
fetched: true,
fetching: false,
tweets: action.payload,
error: null
};
break;
}
default: {
return state;
}
}
}
25 changes: 15 additions & 10 deletions screens/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
Grid,
Icon
} from "native-base";
import { connect } from "react-redux";
import { fetchTweets } from "../actions/tweetsActions";

const styles = StyleSheet.create({
topMargin: {
Expand Down Expand Up @@ -57,23 +59,25 @@ const styles = StyleSheet.create({
}
});

@connect(store => {
return {
tweets: store.tweets.tweets,
fetchingTweets: store.tweets.fetching,
fetchedTweets: store.tweets.fetched,
errorTweets: store.tweets.error,
username: store.login.username
};
})
export default class HomeScreen extends Component {
constructor(props) {
super(props);
}

componentWillMount() {
fetch("https://http://localhost:3000/tweets")
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
})
.catch(error => {
console.log(error);
});
this.props.dispatch(fetchTweets());
}

tweets = [
tweetsTemp = [
{
id: 1,
user: {
Expand Down Expand Up @@ -178,6 +182,7 @@ export default class HomeScreen extends Component {
}

render() {
console.log(this.props);
return (
<Container style={styles.topMargin}>
<Header noShadow style={{ backgroundColor: "white" }}>
Expand All @@ -187,7 +192,7 @@ export default class HomeScreen extends Component {
</Header>
<Content style={styles.content}>
<FlatList
data={this.tweets}
data={this.props.tweets}
keyExtractor={this._keyExtractor}
renderItem={({ item }) =>
<View style={styles.tweet}>
Expand Down
10 changes: 9 additions & 1 deletion screens/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
Footer,
FooterTab
} from "native-base";
import { setUsername } from "../actions/loginActions";
import { connect } from "react-redux";

const styles = StyleSheet.create({
topMargin: {
Expand All @@ -51,6 +53,9 @@ const styles = StyleSheet.create({
}
});

@connect(store => {
return {};
})
export default class LoginScreen extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -85,7 +90,10 @@ export default class LoginScreen extends Component {
<Form>
<Item stackedLabel last>
<Label>Phone number, email address, or username</Label>
<Input />
<Input
onChangeText={username =>
this.props.dispatch(setUsername(username))}
/>
</Item>
<Item stackedLabel last>
<Label>Password</Label>
Expand Down
22 changes: 17 additions & 5 deletions screens/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
Button
} from "native-base";

import { connect } from "react-redux";
import { fetchTweets } from "../actions/tweetsActions";

const styles = StyleSheet.create({
header: {
paddingTop: 5,
Expand Down Expand Up @@ -112,14 +115,23 @@ const styles = StyleSheet.create({
}
});

@connect(store => {
return {
tweets: store.tweets.tweets,
fetchingTweets: store.tweets.fetching,
fetchedTweets: store.tweets.fetched,
errorTweets: store.tweets.error,
username: store.login.username
};
})
export default class ProfileScreen extends Component {
constructor(props) {
super(props);
this.user = this.props.navigation.state.params;
console.log(this.user.name);
}

tweets = [
tweetsTemp = [
{
id: 1,
user: {
Expand Down Expand Up @@ -297,7 +309,7 @@ export default class ProfileScreen extends Component {
</View>
<View style={{ backgroundColor: "white", marginTop: 8 }}>
<FlatList
data={this.tweets}
data={this.props.tweets}
keyExtractor={this._keyExtractor}
renderItem={({ item }) =>
<View style={styles.tweet}>
Expand All @@ -307,7 +319,7 @@ export default class ProfileScreen extends Component {
activeOpacity={0.75}
> */}
<View style={{ flex: 1, flexDirection: "row" }}>
<Thumbnail source={{ uri: item.user.avatar }} />
<Thumbnail source={{ uri: this.user.avatar }} />
<View
style={{
flexDirection: "column",
Expand All @@ -321,13 +333,13 @@ export default class ProfileScreen extends Component {
fontSize: 20
}}
>
{item.user.name}
{this.user.name}
</Text>

<Text
style={{ paddingLeft: 15, color: "#aaa", fontSize: 16 }}
>
{"@" + item.user.username}
{"@" + this.user.username}
</Text>
</View>
</View>
Expand Down
11 changes: 11 additions & 0 deletions store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { applyMiddleware, createStore } from "redux";

import logger from "redux-logger";
import thunk from "redux-thunk";
import promise from "redux-promise-middleware";

import reducer from "./reducers";

const middleware = applyMiddleware(promise(), thunk, logger);

export default createStore(reducer, middleware);

0 comments on commit 49a66eb

Please sign in to comment.