Skip to content

Commit 9bcb514

Browse files
committed
added fetching functionalit
1 parent f53cec2 commit 9bcb514

File tree

5 files changed

+101
-20
lines changed

5 files changed

+101
-20
lines changed

App.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ import { View, Text, AsyncStorage } from 'react-native'
33
import Nav from './src/Nav'
44
import PropTypes from 'prop-types'
55

6-
import { createStore } from 'redux'
6+
import { createStore, applyMiddleware } from 'redux' // NEW
7+
78
import { Provider } from 'react-redux'
89
import rootReducer from './src/reducers/rootReducer'
9-
// import { persistStore } from 'redux-persist' // new
1010

11-
const store = createStore(rootReducer)
12-
// persistStore(store) // new
11+
import thunk from 'redux-thunk' // NEW
12+
13+
const store = createStore(rootReducer, applyMiddleware(thunk))
1314

1415
class App extends React.Component {
15-
componentDidMount() {
16-
AsyncStorage.clear()
17-
}
1816
render() {
1917
return (
2018
<Provider store={store}>

src/actions/citiesActions.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
11
export const ADD_CITY = 'ADD_CITY'
22
export const ADD_LOCATION = 'ADD_LOCATION'
33

4+
export const FETCHING_CITIES = 'FETCHING_CITIES'
5+
export const FETCHING_CITIES_SUCCESS = 'FETCHING_CITIES_SUCCESS'
6+
export const FETCHING_CITIES_FAILURE = 'FETCHING_CITIES_FAILURE'
7+
8+
import getCities from '../api'
9+
10+
import uuidV4 from 'uuid/v4'
11+
12+
export function getCitiesFromAPI() {
13+
return (dispatch) => {
14+
dispatch(fetchingCities())
15+
fetch('https://www.swapi.co/api/planets/')
16+
.then(data => data.json())
17+
.then(data => {
18+
const { results } = data
19+
results.forEach(result => {
20+
result.country = result.orbital_period;
21+
result.id = uuidV4();
22+
result.locations = [];
23+
})
24+
dispatch(fetchingCitiesSuccess(results))
25+
})
26+
.catch(error => {
27+
dispatch(fetchingCitiesFailure())
28+
})
29+
}
30+
}
31+
32+
export function fetchingCities() {
33+
return {
34+
type: FETCHING_CITIES
35+
}
36+
}
37+
38+
export function fetchingCitiesSuccess(cities) {
39+
return {
40+
type: FETCHING_CITIES_SUCCESS,
41+
cities: cities
42+
}
43+
}
44+
45+
export function fetchingCitiesFailure() {
46+
return {
47+
type: FETCHING_CITIES_FAILURE
48+
}
49+
}
50+
451
export function addCity(city) {
552
return {
653
type: ADD_CITY,

src/api.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import uuidV4 from 'uuid/v4'
2+
13
const cities = [
24
{ name: 'Detroit', locations: [], country: 'USA', id: uuidV4() },
35
{ name: 'Montreal', locations: [], country: 'Canada', id: uuidV4() },

src/reducers/citiesReducer.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
import { ADD_CITY, ADD_LOCATION } from '../actions/citiesActions'
1+
import { ADD_CITY, ADD_LOCATION, FETCHING_CITIES, FETCHING_CITIES_SUCCESS, FETCHING_CITIES_FAILURE } from '../actions/citiesActions'
2+
23
import uuidV4 from 'uuid/v4'
34

45
const initialState = {
5-
cities: []
6+
cities: [],
7+
error: false,
8+
isFetching: false
69
}
710

811
export default function citiesReducer(state = initialState, action) {
912
switch(action.type) {
13+
case FETCHING_CITIES:
14+
return {
15+
...state,
16+
isFetching: true
17+
}
18+
case FETCHING_CITIES_SUCCESS:
19+
return {
20+
...state,
21+
isFetching: false,
22+
cities: action.cities
23+
}
24+
case FETCHING_CITIES_FAILURE:
25+
return {
26+
...state,
27+
isFetching: false,
28+
error: true
29+
}
1030
case ADD_LOCATION:
1131
const city = state.cities.filter(city => {
1232
return city.id === action.city.id

src/tabs/Cities/Cities.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import React from 'react'
22
import {
3-
View, Text, StyleSheet, Image, FlatList
3+
View, Text, StyleSheet, Image, FlatList, ActivityIndicator
44
} from 'react-native'
5-
65
import { ListItem } from 'react-native-elements'
76

87
import { connect } from 'react-redux'
9-
108
import PropTypes from 'prop-types'
11-
9+
import { getCitiesFromAPI } from '../../actions/citiesActions'
1210
const styles = StyleSheet.create({
1311
container: {
1412
flex: 1,
@@ -30,6 +28,9 @@ class Cities extends React.Component {
3028
/>
3129
)
3230
}
31+
componentDidMount() {
32+
this.props.dispatchGetCitiesFromAPI()
33+
}
3334
renderCity = ({ item }) => {
3435
return (
3536
<ListItem
@@ -41,20 +42,33 @@ class Cities extends React.Component {
4142
render() {
4243
return (
4344
<View style={styles.container}>
44-
<FlatList
45-
keyExtractor={(city) => city.id}
46-
data={this.props.cities}
47-
renderItem={this.renderCity}
48-
/>
45+
{
46+
this.props.citiesInfo.isFetching ? (
47+
<ActivityIndicator
48+
animating
49+
/>
50+
) : (
51+
<FlatList
52+
keyExtractor={(city) => city.id}
53+
data={this.props.cities}
54+
renderItem={this.renderCity}
55+
/>
56+
)
57+
}
4958
</View>
5059
)
5160
}
5261
}
5362

5463
const mapStateToProps = (state, nextProps) => {
5564
return {
56-
cities: state.citiesReducer.cities
65+
cities: state.citiesReducer.cities,
66+
citiesInfo: state.citiesReducer
5767
}
5868
}
5969

60-
export default connect(mapStateToProps)(Cities)
70+
const mapDispatchToProps = {
71+
dispatchGetCitiesFromAPI: getCitiesFromAPI
72+
}
73+
74+
export default connect(mapStateToProps, mapDispatchToProps)(Cities)

0 commit comments

Comments
 (0)