Skip to content

Commit 3ebefca

Browse files
author
Matthieu Napoli
committed
Add a "loading" example
1 parent ebc3252 commit 3ebefca

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

App/Containers/HomeScreen.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ class HomeScreen extends React.Component {
1616
}
1717

1818
render() {
19+
let temperature = this.props.temperatureIsLoading ? '...' : this.props.temperature
20+
if (temperature === null) {
21+
temperature = '??'
22+
}
23+
1924
return (
2025
<View style={styles.container}>
2126
<Text style={styles.welcome}>Welcome to React Native!</Text>
2227
<Text style={styles.instructions}>To get started, edit App.js</Text>
2328
<Text style={styles.instructions}>{instructions}</Text>
24-
<Text style={styles.instructions}>
25-
The weather temperature is: {this.props.temperature}
26-
</Text>
29+
<Text style={styles.instructions}>The weather temperature is: {temperature}</Text>
2730
<Text style={styles.instructions}>{this.props.isHot ? "It's pretty hot!" : ''}</Text>
2831
<Text style={styles.instructions}>{this.props.temperatureErrorMessage}</Text>
2932
<Button onPress={this.props.fetchTemperature} title="Refresh" />
@@ -57,6 +60,7 @@ HomeScreen.propsTypes = {
5760
const mapStateToProps = (state) => ({
5861
temperature: state.example.get('temperature'),
5962
temperatureErrorMessage: state.example.get('temperatureErrorMessage'),
63+
temperatureIsLoading: state.example.get('temperatureIsLoading'),
6064
isHot: isHot(state),
6165
})
6266

App/Sagas/ExampleSaga.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import { WeatherService } from 'App/Service/WeatherService'
88
* This example saga contains only one to fetch the weather temperature.
99
*/
1010
export function* fetchTemperature() {
11+
// Dispatch a redux action using `put()`
12+
// @see https://redux-saga.js.org/docs/basics/DispatchingActions.html
13+
yield put(ExampleActions.fetchTemperatureLoading())
14+
1115
// Fetch the temperature from an API
1216
const temperature = yield call(WeatherService.fetchTemperature)
1317

14-
// Dispatch a redux action using `put()`
15-
// @see https://redux-saga.js.org/docs/basics/DispatchingActions.html
1618
if (temperature) {
1719
yield put(ExampleActions.fetchTemperatureSuccess(temperature))
1820
} else {

App/Stores/Example/Actions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { createActions } from 'reduxsauce'
2121
const { Types, Creators } = createActions({
2222
// Fetch the current weather temperature
2323
fetchTemperature: null,
24+
// The operation has started and is loading
25+
fetchTemperatureLoading: null,
2426
// The temperature was successfully fetched
2527
fetchTemperatureSuccess: ['temperature'],
2628
// An error occurred

App/Stores/Example/InitialState.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ import { Map } from 'immutable'
66
export const INITIAL_STATE = Map({
77
temperature: null,
88
temperatureErrorMessage: null,
9+
temperatureIsLoading: false,
910
})

App/Stores/Example/Reducers.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1+
/**
2+
* Reducers specify how the application's state changes in response to actions sent to the store.
3+
*
4+
* @see https://redux.js.org/basics/reducers
5+
*/
6+
17
import { INITIAL_STATE } from './InitialState'
28
import { createReducer } from 'reduxsauce'
39
import { ExampleTypes } from './Actions'
410

5-
/**
6-
* Example of a reducer that updates the `temperature` property.
7-
*/
8-
export const updateTemperature = (state, { temperature }) =>
11+
export const fetchTemperatureLoading = (state) =>
12+
state.merge({
13+
temperatureIsLoading: true,
14+
temperatureErrorMessage: '',
15+
})
16+
17+
export const fetchTemperatureSuccess = (state, { temperature }) =>
918
state.merge({
1019
temperature: temperature,
20+
temperatureIsLoading: false,
1121
temperatureErrorMessage: null,
1222
})
1323

14-
/**
15-
* Example of a reducer that updates the `temperatureErrorMessage` property.
16-
*/
17-
export const showErrorMessage = (state, { errorMessage }) =>
24+
export const fetchTemperatureFailure = (state, { errorMessage }) =>
1825
state.merge({
19-
temperature: '??',
26+
temperature: null,
27+
temperatureIsLoading: false,
2028
temperatureErrorMessage: errorMessage,
2129
})
2230

2331
/**
2432
* @see https://github.com/infinitered/reduxsauce#createreducer
2533
*/
2634
export const reducer = createReducer(INITIAL_STATE, {
27-
[ExampleTypes.FETCH_TEMPERATURE_SUCCESS]: updateTemperature,
28-
[ExampleTypes.FETCH_TEMPERATURE_FAILURE]: showErrorMessage,
35+
[ExampleTypes.FETCH_TEMPERATURE_LOADING]: fetchTemperatureLoading,
36+
[ExampleTypes.FETCH_TEMPERATURE_SUCCESS]: fetchTemperatureSuccess,
37+
[ExampleTypes.FETCH_TEMPERATURE_FAILURE]: fetchTemperatureFailure,
2938
})

0 commit comments

Comments
 (0)