Commit 96d528eb authored by Wietse Kuipers's avatar Wietse Kuipers
Browse files

Merge branch 'welcome-saga-refactor' into 'master'

Refactor welcome actions to use saga

See merge request !65
parents 03b68717 cede2b16
import { apiUrl } from '../url'; export const REFRESH = 'WELCOME_REFRESH';
import * as types from './actionTypes'; export const SUCCESS = 'WELCOME_SUCCESS';
export const FAILURE = 'WELCOME_FAILURE';
export function welcome(eventList) { export function refresh() {
return { return {
type: types.WELCOME, type: REFRESH,
eventList,
}; };
} }
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 };
}
...@@ -5,9 +5,8 @@ import { connect } from 'react-redux'; ...@@ -5,9 +5,8 @@ import { connect } from 'react-redux';
import Moment from 'moment'; import Moment from 'moment';
import 'moment/locale/nl'; import 'moment/locale/nl';
import EventDetailCard from './EventDetailCard'; import EventDetailCard from './EventDetailCard';
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';
...@@ -63,24 +62,12 @@ const mapDispatchToPropsFooter = dispatch => ({ ...@@ -63,24 +62,12 @@ const mapDispatchToPropsFooter = dispatch => ({
const FooterComponent = connect(() => ({}), mapDispatchToPropsFooter)(Footer); const FooterComponent = connect(() => ({}), mapDispatchToPropsFooter)(Footer);
class Welcome extends Component { class Welcome extends Component {
constructor(props) {
super(props);
this.state = {
refreshing: false,
};
}
handleRefresh = () => { handleRefresh = () => {
this.setState({ refreshing: true }); this.props.refresh();
this.props.retrieveShortlist(this.props.token, 5)
.then(() => this.setState({ refreshing: false }));
}; };
render() { render() {
if (!this.props.hasLoaded) { if (this.props.eventList.length === 0) {
this.props.retrieveShortlist(this.props.token, 5);
return <LoadingScreen />;
} else if (this.props.eventList.length === 0) {
return ( return (
<View> <View>
<Text> <Text>
...@@ -101,7 +88,7 @@ class Welcome extends Component { ...@@ -101,7 +88,7 @@ class Welcome extends Component {
keyExtractor={event => event.pk} keyExtractor={event => event.pk}
stickySectionHeadersEnabled stickySectionHeadersEnabled
onRefresh={this.handleRefresh} onRefresh={this.handleRefresh}
refreshing={this.state.refreshing} refreshing={this.props.loading}
ListFooterComponent={FooterComponent} ListFooterComponent={FooterComponent}
/> />
</View> </View>
...@@ -121,19 +108,17 @@ Welcome.propTypes = { ...@@ -121,19 +108,17 @@ Welcome.propTypes = {
registered: PropTypes.bool, registered: PropTypes.bool,
pizza: PropTypes.bool, pizza: PropTypes.bool,
})).isRequired, })).isRequired,
token: PropTypes.string.isRequired, refresh: PropTypes.func.isRequired,
hasLoaded: PropTypes.bool.isRequired, loading: PropTypes.bool.isRequired,
retrieveShortlist: PropTypes.func.isRequired,
}; };
const mapStateToProps = state => ({ const mapStateToProps = state => ({
eventList: state.welcome.eventList, eventList: state.welcome.eventList,
token: state.session.token, loading: state.welcome.loading,
hasLoaded: state.welcome.hasLoaded,
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
retrieveShortlist: (token, amount) => dispatch(retrieveShortlist(token, amount)), refresh: () => dispatch(welcomeActions.refresh()),
}); });
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: [],
hasLoaded: false, loading: true,
}; };
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, loading: false,
}; };
case welcomeActions.FAILURE:
return { ...state, loading: false };
case welcomeActions.REFRESH:
return { ...state, loading: true };
default: default:
return state; return state;
} }
......
...@@ -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, select, takeEvery } from 'redux-saga/effects';
import { apiRequest, tokenSelector } from '../url';
import * as welcomeActions from '../actions/welcome';
import * as loginActions from '../actions/login';
const welcome = function* welcome() {
const token = yield select(tokenSelector);
const data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
const params = {
limit: 5,
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([loginActions.SUCCESS, welcomeActions.REFRESH], welcome);
};
export default welcomeSaga;
...@@ -6,6 +6,7 @@ if (__DEV__) { // eslint-disable-line no-undef ...@@ -6,6 +6,7 @@ if (__DEV__) { // eslint-disable-line no-undef
export const url = server; export const url = server;
export const apiUrl = `${server}/api/v1`; export const apiUrl = `${server}/api/v1`;
export const pizzaUrl = 'https://pizza.thalia.nu'; export const pizzaUrl = 'https://pizza.thalia.nu';
export const tokenSelector = state => state.session.token;
export const apiRequest = (route, fetchOpts, params) => { export const apiRequest = (route, fetchOpts, params) => {
let query = ''; let query = '';
......
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