Skip to content

Commit 4327576

Browse files
authored
Merge pull request #29 from thecodingmachine/fix/change_example_API
Change API used in example screen
2 parents dc709b0 + 26bd452 commit 4327576

File tree

14 files changed

+107
-71
lines changed

14 files changed

+107
-71
lines changed

App/Config/index.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const Config = {
2-
API_URL: 'https://query.yahooapis.com/v1/public/',
2+
API_URL: 'https://jsonplaceholder.typicode.com/users/',
33
}

App/Config/index.production.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const Config = {
2-
API_URL: 'https://query.yahooapis.com/v1/public/',
2+
API_URL: 'https://jsonplaceholder.typicode.com/users/',
33
}

App/Config/index.staging.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const Config = {
2-
API_URL: 'https://query.yahooapis.com/v1/public/',
2+
API_URL: 'https://jsonplaceholder.typicode.com/users/',
33
}

App/Containers/Example/ExampleScreen.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { Platform, Text, View, Button } from 'react-native'
33
import { connect } from 'react-redux'
44
import { PropTypes } from 'prop-types'
55
import ExampleActions from 'App/Stores/Example/Actions'
6-
import { isHot } from 'App/Stores/Example/Selectors'
6+
import { liveInEurope } from 'App/Stores/Example/Selectors'
77
import Style from './ExampleScreenStyle'
88

99
/**
1010
* This is an example of a container component.
1111
*
12-
* This screen displays a little help message and shows the weather temperature.
12+
* This screen displays a little help message and informations about a fake user.
1313
* Feel free to remove it.
1414
*/
1515

@@ -20,43 +20,54 @@ const instructions = Platform.select({
2020

2121
class ExampleScreen extends React.Component {
2222
componentDidMount() {
23-
this.props.fetchTemperature()
23+
this.props.fetchUser()
2424
}
2525

2626
render() {
27-
let temperature = this.props.temperatureIsLoading ? '...' : this.props.temperature
28-
if (temperature === null) {
29-
temperature = '??'
27+
let isLoading = this.props.userIsLoading ? 'Data are loading...' : ''
28+
let user = this.props.user
29+
let error = this.props.userErrorMessage
30+
let result = null
31+
if (user && !error) {
32+
result =
33+
"I'm a fake user, my name is " +
34+
user.name +
35+
'.\n' +
36+
(this.props.liveInEurope ? 'I live in Europe !' : "I don't live in Europe.")
3037
}
3138

3239
return (
3340
<View style={Style.container}>
3441
<Text style={Style.title}>TheCodingMachine boilerplate</Text>
3542
<Text style={Style.text}>To get started, edit App.js</Text>
36-
<Text style={Style.text}>{instructions}</Text>
37-
<Text style={Style.text}>The weather temperature is: {temperature}</Text>
38-
<Text style={Style.text}>{this.props.isHot ? "It's pretty hot!" : ''}</Text>
39-
<Text style={Style.text}>{this.props.temperatureErrorMessage}</Text>
40-
<Button onPress={this.props.fetchTemperature} title="Refresh" />
43+
<Text style={Style.instructions}>{instructions}</Text>
44+
<Text style={Style.loading}>{isLoading}</Text>
45+
{user && !error ? (
46+
<Text style={Style.result}>{result}</Text>
47+
) : (
48+
<Text style={Style.error}>{error}</Text>
49+
)}
50+
<Button onPress={this.props.fetchUser} title="Refresh" />
4151
</View>
4252
)
4353
}
4454
}
4555

4656
ExampleScreen.propsTypes = {
47-
temperature: PropTypes.number,
48-
temperatureErrorMessage: PropTypes.string,
57+
user: PropTypes.number,
58+
userIsLoading: PropTypes.bool,
59+
userErrorMessage: PropTypes.string,
4960
}
5061

5162
const mapStateToProps = (state) => ({
52-
temperature: state.example.get('temperature'),
53-
temperatureErrorMessage: state.example.get('temperatureErrorMessage'),
54-
temperatureIsLoading: state.example.get('temperatureIsLoading'),
55-
isHot: isHot(state),
63+
user: state.example.get('user').toJS(),
64+
userIsLoading: state.example.get('userIsLoading'),
65+
userErrorMessage: state.example.get('userErrorMessage'),
66+
liveInEurope: liveInEurope(state),
5667
})
5768

5869
const mapDispatchToProps = (dispatch) => ({
59-
fetchTemperature: () => dispatch(ExampleActions.fetchTemperature()),
70+
fetchUser: () => dispatch(ExampleActions.fetchUser()),
6071
})
6172

6273
export default connect(

App/Containers/Example/ExampleScreenStyle.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,26 @@ export default StyleSheet.create({
1919
textAlign: 'center',
2020
marginBottom: 5,
2121
},
22+
instructions: {
23+
...Fonts.style.normal,
24+
textAlign: 'center',
25+
marginBottom: 5,
26+
fontStyle: 'italic',
27+
},
28+
loading: {
29+
...Fonts.style.normal,
30+
textAlign: 'center',
31+
marginBottom: 5,
32+
},
33+
result: {
34+
...Fonts.style.normal,
35+
textAlign: 'center',
36+
marginBottom: 5,
37+
},
38+
error: {
39+
...Fonts.style.normal,
40+
textAlign: 'center',
41+
marginBottom: 5,
42+
color: 'red',
43+
},
2244
})

App/Sagas/ExampleSaga.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import { put, call } from 'redux-saga/effects'
22
import ExampleActions from 'App/Stores/Example/Actions'
3-
import { WeatherService } from 'App/Services/WeatherService'
3+
import { userService } from 'App/Services/UserService'
44

55
/**
66
* A saga can contain multiple functions.
77
*
8-
* This example saga contains only one to fetch the weather temperature.
8+
* This example saga contains only one to fetch fake user informations.
99
* Feel free to remove it.
1010
*/
11-
export function* fetchTemperature() {
11+
export function* fetchUser() {
1212
// Dispatch a redux action using `put()`
1313
// @see https://redux-saga.js.org/docs/basics/DispatchingActions.html
14-
yield put(ExampleActions.fetchTemperatureLoading())
14+
yield put(ExampleActions.fetchUserLoading())
1515

16-
// Fetch the temperature from an API
17-
const temperature = yield call(WeatherService.fetchTemperature)
18-
19-
if (temperature) {
20-
yield put(ExampleActions.fetchTemperatureSuccess(temperature))
16+
// Fetch user informations from an API
17+
const user = yield call(userService.fetchUser)
18+
if (user) {
19+
yield put(ExampleActions.fetchUserSuccess(user))
2120
} else {
2221
yield put(
23-
ExampleActions.fetchTemperatureFailure('There was an error while fetching the temperature.')
22+
ExampleActions.fetchUserFailure('There was an error while fetching user informations.')
2423
)
2524
}
2625
}

App/Sagas/StartupSaga.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import NavigationService from 'App/Services/NavigationService'
88
export function* startup() {
99
// Dispatch a redux action using `put()`
1010
// @see https://redux-saga.js.org/docs/basics/DispatchingActions.html
11-
yield put(ExampleActions.fetchTemperature())
11+
yield put(ExampleActions.fetchUser())
1212

1313
// Add more operations you need to do at startup here
1414
// ...

App/Sagas/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { takeLatest } from 'redux-saga/effects'
22
import { ExampleTypes } from 'App/Stores/Example/Actions'
33
import { StartupTypes } from 'App/Stores/Startup/Actions'
4-
import { fetchTemperature } from './ExampleSaga'
4+
import { fetchUser } from './ExampleSaga'
55
import { startup } from './StartupSaga'
66

77
export default function* root() {
@@ -11,7 +11,7 @@ export default function* root() {
1111
*/
1212
// Run the startup saga when the application starts
1313
takeLatest(StartupTypes.STARTUP, startup),
14-
// Call `fetchTemperature()` when a `FETCH_TEMPERATURE` action is triggered
15-
takeLatest(ExampleTypes.FETCH_TEMPERATURE, fetchTemperature),
14+
// Call `fetchUser()` when a `FETCH_USER` action is triggered
15+
takeLatest(ExampleTypes.FETCH_USER, fetchUser),
1616
]
1717
}

App/Services/WeatherService.js renamed to App/Services/UserService.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Config } from 'App/Config'
77
* Feel free to remove this example from your application.
88
*/
99

10-
const weatherApiClient = create({
10+
const userApiClient = create({
1111
/**
1212
* Import the config from the App/Config/index.js file
1313
*/
@@ -19,27 +19,25 @@ const weatherApiClient = create({
1919
timeout: 3000,
2020
})
2121

22-
function fetchTemperature() {
22+
function fetchUser() {
2323
// Simulate an error 50% of the time just for testing purposes
2424
if (Math.random() > 0.5) {
2525
return new Promise(function(resolve, reject) {
2626
resolve(null)
2727
})
2828
}
2929

30-
const locationQuery = escape(
31-
"select item.condition.temp from weather.forecast where woeid in (select woeid from geo.places where text='Lyon, Rhone-Alpes, FR') and u='c'"
32-
)
30+
let number = Math.floor(Math.random() / 0.1) + 1
3331

34-
return weatherApiClient.get('yql?q=' + locationQuery + '&format=json').then((response) => {
32+
return userApiClient.get(number.toString()).then((response) => {
3533
if (response.ok) {
36-
return response.data.query.results.channel.item.condition.temp
34+
return response.data
3735
}
3836

3937
return null
4038
})
4139
}
4240

43-
export const WeatherService = {
44-
fetchTemperature,
41+
export const userService = {
42+
fetchUser,
4543
}

App/Stores/Example/Actions.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import { createActions } from 'reduxsauce'
1919
* @see https://github.com/infinitered/reduxsauce#createactions
2020
*/
2121
const { Types, Creators } = createActions({
22-
// Fetch the current weather temperature
23-
fetchTemperature: null,
22+
// Fetch user informations
23+
fetchUser: null,
2424
// The operation has started and is loading
25-
fetchTemperatureLoading: null,
26-
// The temperature was successfully fetched
27-
fetchTemperatureSuccess: ['temperature'],
25+
fetchUserLoading: null,
26+
// User informations were successfully fetched
27+
fetchUserSuccess: ['user'],
2828
// An error occurred
29-
fetchTemperatureFailure: ['errorMessage'],
29+
fetchUserFailure: ['errorMessage'],
3030
})
3131

3232
export const ExampleTypes = Types

0 commit comments

Comments
 (0)