Commit 72ab549c authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg Committed by Wietse Kuipers
Browse files

Refactor welcome actions to use saga

parent 03b68717
import { apiUrl } from '../url'; export const WELCOME = 'WELCOME_WELCOME';
import * as types from './actionTypes'; export const SUCCESS = 'WELCOME_SUCCESS';
export const FAILURE = 'WELCOME_FAILURE';
export function welcome(eventList) { export function welcome(amount, token) {
return { return {
type: types.WELCOME, type: WELCOME,
eventList, payload: { amount, token },
}; };
} }
export function retrieveShortlist(token, amount) { export function success(eventList) {
return (dispatch) => { return {
const data = { type: SUCCESS,
method: 'GET', payload: { eventList },
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
return fetch(`${apiUrl}/events/?limit=${amount}&ordering=start`, data)
.then(
response => response.json(),
)
.then(
responseJson => dispatch(welcome(responseJson.results)),
)
.catch(
() => dispatch(welcome([])),
);
}; };
} }
export function failure() {
return { type: FAILURE };
}
...@@ -7,7 +7,7 @@ import 'moment/locale/nl'; ...@@ -7,7 +7,7 @@ import 'moment/locale/nl';
import EventDetailCard from './EventDetailCard'; import EventDetailCard from './EventDetailCard';
import LoadingScreen from './LoadingScreen'; import LoadingScreen from './LoadingScreen';
import { retrieveShortlist } from '../actions/welcome'; import * as welcomeActions from '../actions/welcome';
import { navigate } from '../actions/navigation'; import { navigate } from '../actions/navigation';
import styles from './style/welcome'; import styles from './style/welcome';
...@@ -133,7 +133,7 @@ const mapStateToProps = state => ({ ...@@ -133,7 +133,7 @@ const mapStateToProps = state => ({
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
retrieveShortlist: (token, amount) => dispatch(retrieveShortlist(token, amount)), retrieveShortlist: (token, amount) => dispatch(welcomeActions.welcome(amount, token)),
}); });
export default connect(mapStateToProps, mapDispatchToProps)(Welcome); export default connect(mapStateToProps, mapDispatchToProps)(Welcome);
import * as types from '../actions/actionTypes'; import * as welcomeActions from '../actions/welcome';
const initialState = { const initialState = {
eventList: [], eventList: [],
...@@ -7,9 +7,9 @@ const initialState = { ...@@ -7,9 +7,9 @@ const initialState = {
export default function welcome(state = initialState, action = {}) { export default function welcome(state = initialState, action = {}) {
switch (action.type) { switch (action.type) {
case types.WELCOME: case welcomeActions.SUCCESS:
return { return {
eventList: action.eventList, eventList: action.payload.eventList,
hasLoaded: true, hasLoaded: true,
}; };
default: default:
......
...@@ -3,12 +3,14 @@ import { all, fork } from 'redux-saga/effects'; ...@@ -3,12 +3,14 @@ import { all, fork } from 'redux-saga/effects';
import loginSaga from './login'; import loginSaga from './login';
import eventSaga from './event'; import eventSaga from './event';
import profileSaga from './profile'; import profileSaga from './profile';
import welcomeSaga from './welcome';
const sagas = function* sagas() { const sagas = function* sagas() {
yield all([ yield all([
fork(loginSaga), fork(loginSaga),
fork(eventSaga), fork(eventSaga),
fork(profileSaga), fork(profileSaga),
fork(welcomeSaga),
]); ]);
}; };
......
import { call, put, takeEvery } from 'redux-saga/effects';
import { apiRequest } from '../url';
import * as welcomeActions from '../actions/welcome';
const welcome = function* welcome(action) {
const { token, amount } = action.payload;
const data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
const params = {
limit: amount,
ordering: 'start',
};
try {
const response = yield call(apiRequest, 'events', data, params);
yield put(welcomeActions.success(response));
} catch (error) {
yield put(welcomeActions.failure());
}
};
const welcomeSaga = function* eventSaga() {
yield takeEvery(welcomeActions.WELCOME, welcome);
};
export default welcomeSaga;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment