forked from thecodingmachine/react-native-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the redux/redux-saga example to include error handling and b…
…etter naming
- Loading branch information
Matthieu Napoli
committed
Aug 13, 2018
1 parent
b700f91
commit 739d3cd
Showing
10 changed files
with
97 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import { put } from 'redux-saga/effects' | ||
import { put, call } from 'redux-saga/effects' | ||
import ExampleActions from 'App/Stores/Example/Actions' | ||
import { WeatherService } from 'App/Service/WeatherService' | ||
|
||
/** | ||
* A saga can contain multiple functions. | ||
* | ||
* This example saga contains only one to fetch the weather temperature. | ||
*/ | ||
export function* fetchWeatherTemperature() { | ||
// Fetch the temperature from an API (in this example we hardcode 24 degrees) | ||
const temperature = 24 | ||
export function* fetchTemperature() { | ||
// Fetch the temperature from an API | ||
const temperature = yield call(WeatherService.fetchTemperature) | ||
|
||
// Dispatch a redux action using `put()` | ||
// @see https://redux-saga.js.org/docs/basics/DispatchingActions.html | ||
yield put(ExampleActions.updateWeatherTemperature(temperature)) | ||
if (temperature) { | ||
yield put(ExampleActions.fetchTemperatureSuccess(temperature)) | ||
} else { | ||
yield put( | ||
ExampleActions.fetchTemperatureFailure('There was an error while fetching the temperature.') | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { takeLatest } from 'redux-saga/effects' | ||
import { ExampleTypes } from 'App/Stores/Example/Actions' | ||
import { fetchWeatherTemperature } from './ExampleSaga' | ||
import { fetchTemperature } from './ExampleSaga' | ||
|
||
export default function* root() { | ||
yield [ | ||
/** | ||
* @see https://redux-saga.js.org/docs/basics/UsingSagaHelpers.html | ||
*/ | ||
// Call `fetchWeatherTemperature()` when a `REQUEST_WEATHER_TEMPERATURE` action is triggered | ||
takeLatest(ExampleTypes.REQUEST_WEATHER_TEMPERATURE, fetchWeatherTemperature), | ||
// Call `fetchTemperature()` when a `FETCH_TEMPERATURE` action is triggered | ||
takeLatest(ExampleTypes.FETCH_TEMPERATURE, fetchTemperature), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This directory contains services that are meant to connect the application to other APIs, for example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { create } from 'apisauce' | ||
|
||
const weatherApiClient = create({ | ||
baseURL: 'https://query.yahooapis.com/v1/public/', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
timeout: 3000, | ||
}) | ||
|
||
function fetchTemperature() { | ||
const locationQuery = escape( | ||
"select item.condition.temp from weather.forecast where woeid in (select woeid from geo.places where text='Lyon, Rhone-Alpes, FR') and u='c'" | ||
) | ||
|
||
return weatherApiClient.get('yql?q=' + locationQuery + '&format=json').then((response) => { | ||
if (response.ok) { | ||
return response.data.query.results.channel.item.condition.temp | ||
} | ||
|
||
return null | ||
}) | ||
} | ||
|
||
export const WeatherService = { | ||
fetchTemperature, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters