Commit cede2b16 authored by Wietse Kuipers's avatar Wietse Kuipers
Browse files

Refactored to remove local state/react warning

parent 72ab549c
export const WELCOME = 'WELCOME_WELCOME'; export const REFRESH = 'WELCOME_REFRESH';
export const SUCCESS = 'WELCOME_SUCCESS'; export const SUCCESS = 'WELCOME_SUCCESS';
export const FAILURE = 'WELCOME_FAILURE'; export const FAILURE = 'WELCOME_FAILURE';
export function welcome(amount, token) { export function refresh() {
return { return {
type: WELCOME, type: REFRESH,
payload: { amount, token },
}; };
} }
......
...@@ -5,7 +5,6 @@ import { connect } from 'react-redux'; ...@@ -5,7 +5,6 @@ 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 * as welcomeActions from '../actions/welcome'; import * as welcomeActions from '../actions/welcome';
import { navigate } from '../actions/navigation'; import { navigate } from '../actions/navigation';
...@@ -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(welcomeActions.welcome(amount, token)), refresh: () => dispatch(welcomeActions.refresh()),
}); });
export default connect(mapStateToProps, mapDispatchToProps)(Welcome); export default connect(mapStateToProps, mapDispatchToProps)(Welcome);
...@@ -2,7 +2,7 @@ import * as welcomeActions from '../actions/welcome'; ...@@ -2,7 +2,7 @@ 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 = {}) {
...@@ -10,8 +10,12 @@ export default function welcome(state = initialState, action = {}) { ...@@ -10,8 +10,12 @@ export default function welcome(state = initialState, action = {}) {
case welcomeActions.SUCCESS: case welcomeActions.SUCCESS:
return { return {
eventList: action.payload.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;
} }
......
import { call, put, takeEvery } from 'redux-saga/effects'; import { call, put, select, takeEvery } from 'redux-saga/effects';
import { apiRequest } from '../url'; import { apiRequest, tokenSelector } from '../url';
import * as welcomeActions from '../actions/welcome'; import * as welcomeActions from '../actions/welcome';
import * as loginActions from '../actions/login';
const welcome = function* welcome(action) { const welcome = function* welcome() {
const { token, amount } = action.payload; const token = yield select(tokenSelector);
const data = { const data = {
method: 'GET', method: 'GET',
headers: { headers: {
...@@ -16,7 +16,7 @@ const welcome = function* welcome(action) { ...@@ -16,7 +16,7 @@ const welcome = function* welcome(action) {
}; };
const params = { const params = {
limit: amount, limit: 5,
ordering: 'start', ordering: 'start',
}; };
...@@ -29,7 +29,7 @@ const welcome = function* welcome(action) { ...@@ -29,7 +29,7 @@ const welcome = function* welcome(action) {
}; };
const welcomeSaga = function* eventSaga() { const welcomeSaga = function* eventSaga() {
yield takeEvery(welcomeActions.WELCOME, welcome); yield takeEvery([loginActions.SUCCESS, welcomeActions.REFRESH], welcome);
}; };
export default welcomeSaga; 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