Skip to content

Commit cd48758

Browse files
author
woi
committed
update store to fetch user data
1 parent dc709b0 commit cd48758

File tree

5 files changed

+67
-68
lines changed

5 files changed

+67
-68
lines changed

App/Services/UserService.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { create } from 'apisauce'
2+
import { Config } from 'App/Config'
3+
4+
/**
5+
* This is an example of a service that connects to a 3rd party API.
6+
*
7+
* Feel free to remove this example from your application.
8+
*/
9+
10+
const userApiClient = create({
11+
/**
12+
* Import the config from the App/Config/index.js file
13+
*/
14+
baseURL: Config.API_URL,
15+
headers: {
16+
Accept: 'application/json',
17+
'Content-Type': 'application/json',
18+
},
19+
timeout: 3000,
20+
})
21+
22+
function fetchUser() {
23+
// Simulate an error 50% of the time just for testing purposes
24+
// if (Math.random() > 0.5) {
25+
// return new Promise(function(resolve, reject) {
26+
// resolve(null)
27+
// })
28+
// }
29+
let number = Math.floor(Math.random() / 0.1) + 1
30+
console.log(number)
31+
32+
return userApiClient.get(number.toString()).then((response) => {
33+
console.log(response.data)
34+
if (response.ok) {
35+
return response.data
36+
}
37+
38+
return null
39+
})
40+
}
41+
42+
export const userService = {
43+
fetchUser,
44+
}

App/Services/WeatherService.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

App/Stores/Example/InitialState.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Map } from 'immutable'
44
* The initial values for the redux state.
55
*/
66
export const INITIAL_STATE = Map({
7-
temperature: null,
8-
temperatureErrorMessage: null,
9-
temperatureIsLoading: false,
7+
user: null,
8+
userIsLoading: false,
9+
userErrorMessage: null,
1010
})

App/Stores/Example/Reducers.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ import { INITIAL_STATE } from './InitialState'
88
import { createReducer } from 'reduxsauce'
99
import { ExampleTypes } from './Actions'
1010

11-
export const fetchTemperatureLoading = (state) =>
11+
export const fetchUserLoading = (state) =>
1212
state.merge({
13-
temperatureIsLoading: true,
14-
temperatureErrorMessage: '',
13+
userIsLoading: true,
14+
userErrorMessage: '',
1515
})
1616

17-
export const fetchTemperatureSuccess = (state, { temperature }) =>
17+
export const fetchUserSuccess = (state, { user }) =>
1818
state.merge({
19-
temperature: temperature,
20-
temperatureIsLoading: false,
21-
temperatureErrorMessage: null,
19+
user: user,
20+
userIsLoading: false,
21+
userErrorMessage: null,
2222
})
2323

24-
export const fetchTemperatureFailure = (state, { errorMessage }) =>
24+
export const fetchUserFailure = (state, { errorMessage }) =>
2525
state.merge({
26-
temperature: null,
27-
temperatureIsLoading: false,
28-
temperatureErrorMessage: errorMessage,
26+
user: null,
27+
userIsLoading: false,
28+
userErrorMessage: errorMessage,
2929
})
3030

3131
/**
3232
* @see https://github.com/infinitered/reduxsauce#createreducer
3333
*/
3434
export const reducer = createReducer(INITIAL_STATE, {
35-
[ExampleTypes.FETCH_TEMPERATURE_LOADING]: fetchTemperatureLoading,
36-
[ExampleTypes.FETCH_TEMPERATURE_SUCCESS]: fetchTemperatureSuccess,
37-
[ExampleTypes.FETCH_TEMPERATURE_FAILURE]: fetchTemperatureFailure,
35+
[ExampleTypes.FETCH_USER_LOADING]: fetchUserLoading,
36+
[ExampleTypes.FETCH_USER_SUCCESS]: fetchUserSuccess,
37+
[ExampleTypes.FETCH_USER_FAILURE]: fetchUserFailure,
3838
})

0 commit comments

Comments
 (0)