Commit b2187c1a authored by Gijs Hendriksen's avatar Gijs Hendriksen
Browse files

Merge branch 'feature/event-sagas' into 'master'

Refactor event actions to use sagas

See merge request !59
parents 041c2f65 192913c2
......@@ -3,9 +3,6 @@ export const BACK = 'BACK';
export const OPENDRAWER = 'OPENDRAWER';
export const CALENDARRETREIVED = 'CALENDARRETREIVED';
export const CALENDARERROR = 'CALENDARERROR';
export const LOADEVENTSTART = 'LOADEVENTSTART';
export const LOADEVENTSUCCESS = 'LOADEVENTSUCCESS';
export const LOADEVENTFAILURE = 'LOADEVENTFAILURE';
export const RESETLOGINSTATE = 'RESETLOGINSTATE';
export const WELCOME = 'WELCOME';
export const LOADPROFILESTART = 'LOADPROFILESTART';
......
export const EVENT = 'EVENT_EVENT';
export const FETCHING = 'EVENT_FETCHING';
export const SUCCESS = 'EVENT_SUCCESS';
export const FAILURE = 'EVENT_FAILURE';
export function event(pk, token) {
return {
type: EVENT,
payload: { pk, token },
};
}
export function success(eventData, eventRegistrations) {
return {
type: SUCCESS,
payload: { eventData, eventRegistrations },
};
}
export function fetching() {
return {
type: FETCHING,
};
}
export function failure() {
return {
type: FAILURE,
};
}
import * as types from './actionTypes';
import { navigate } from './navigation';
import { apiUrl } from '../url';
export function start() {
return {
type: types.LOADEVENTSTART,
};
}
export function success(data, registrations) {
return {
type: types.LOADEVENTSUCCESS,
data,
registrations,
};
}
export function fail() {
return {
type: types.LOADEVENTFAILURE,
};
}
function loadRegistrations(id, token) {
const data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
return fetch(`${apiUrl}/events/${id}/registrations/?status=registered`, data)
.then(
response => response.json(),
)
.catch(
() => [],
);
}
export function loadEvent(id, token) {
return (dispatch) => {
dispatch(start());
dispatch(navigate('event'));
const data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
return fetch(`${apiUrl}/events/${id}/`, data)
.then(
response => response.json(),
)
.then(
(response) => {
if (response.status > -1) {
loadRegistrations(id, token)
.then(registrations => dispatch(success(response, registrations)));
} else {
dispatch(success(response, []));
}
},
)
.catch(
() => dispatch(fail()),
);
};
}
......@@ -295,10 +295,10 @@ Event.propTypes = {
};
const mapStateToProps = state => ({
data: state.events.data,
registrations: state.events.registrations,
success: state.events.success,
hasLoaded: state.events.hasLoaded,
data: state.event.data,
registrations: state.event.registrations,
success: state.event.success,
hasLoaded: state.event.hasLoaded,
});
export default connect(mapStateToProps, () => ({}))(Event);
......@@ -5,7 +5,7 @@ import { connect } from 'react-redux';
import Moment from 'moment';
import 'moment/locale/nl';
import * as actions from '../actions/events';
import * as eventActions from '../actions/event';
import styles from './style/eventCard';
......@@ -54,7 +54,7 @@ const mapStateToProps = state => ({
});
const mapDispatchToProps = dispatch => ({
loadEvent: (pk, token) => dispatch(actions.loadEvent(pk, token)),
loadEvent: (pk, token) => dispatch(eventActions.event(pk, token)),
});
export default connect(mapStateToProps, mapDispatchToProps)(EventCard);
......@@ -4,7 +4,7 @@ import { View, Text, TouchableOpacity, Linking } from 'react-native';
import { connect } from 'react-redux';
import Moment from 'moment';
import 'moment/locale/nl';
import * as actions from '../actions/events';
import * as actions from '../actions/event';
import { pizzaUrl } from '../url';
import styles from './style/eventDetailCard';
......@@ -87,7 +87,7 @@ const mapStateToProps = state => ({
});
const mapDispatchToProps = dispatch => ({
loadEvent: (pk, token) => dispatch(actions.loadEvent(pk, token)),
loadEvent: (pk, token) => dispatch(actions.event(pk, token)),
});
export default connect(mapStateToProps, mapDispatchToProps)(EventDetailCard);
import * as types from '../actions/actionTypes';
import * as types from '../actions/event';
const initialState = {
data: {
......@@ -20,21 +20,21 @@ const initialState = {
export default function loadEvent(state = initialState, action = {}) {
switch (action.type) {
case types.LOADEVENTSTART: {
case types.FETCHING: {
return {
...state,
hasLoaded: false,
};
}
case types.LOADEVENTSUCCESS:
case types.SUCCESS:
return {
...state,
data: action.data,
registrations: action.registrations,
data: action.payload.eventData,
registrations: action.payload.eventRegistrations,
success: true,
hasLoaded: true,
};
case types.LOADEVENTFAILURE:
case types.FAILURE:
return {
...state,
success: false,
......
import session from './session';
import navigation from './navigation';
import events from './events';
import event from './event';
import calendar from './calendar';
import welcome from './welcome';
import profile from './profile';
......@@ -8,7 +8,7 @@ import profile from './profile';
export {
session,
navigation,
events,
event,
calendar,
welcome,
profile,
......
import { call, put, takeEvery } from 'redux-saga/effects';
import { apiRequest } from '../url';
import * as eventActions from '../actions/event';
import * as navActions from '../actions/navigation';
const event = function* event(action) {
const { pk, token } = action.payload;
yield put(eventActions.fetching());
yield put(navActions.navigate('event'));
const data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
try {
const eventData = yield call(apiRequest, `events/${pk}`, data);
const params = {
status: 'registered',
};
const eventRegistrations = yield call(apiRequest, `events/${pk}/registrations`, data, params);
yield put(eventActions.success(
eventData,
eventRegistrations,
));
} catch (error) {
yield put(eventActions.failure());
}
};
const eventSaga = function* eventSaga() {
yield takeEvery(eventActions.EVENT, event);
};
export default eventSaga;
import { all, fork } from 'redux-saga/effects';
import loginSaga from './login';
import eventSaga from './event';
const sagas = function* sagas() {
yield all([
fork(loginSaga),
fork(eventSaga),
]);
};
......
......@@ -7,4 +7,12 @@ export const url = server;
export const apiUrl = `${server}/api/v1`;
export const pizzaUrl = 'https://pizza.thalia.nu';
export const apiRequest = (route, data) => fetch(`${apiUrl}/${route}/`, data).then(response => response.json());
export const apiRequest = (route, fetchOpts, params) => {
let query = '';
if (params !== null && params === Object(params)) {
query = `?${Object.keys(params)
.map(k => `${encodeURIComponent(k)} = ${encodeURIComponent(params[k])}`)
.join('&')}`;
}
return fetch(`${apiUrl}/${route}/${query}`, fetchOpts).then(response => response.json());
};
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