Skip to content

Commit 9aa96a5

Browse files
author
woi
committed
update Saga to fetch user data
1 parent cd48758 commit 9aa96a5

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

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
}

0 commit comments

Comments
 (0)