Commit 330edce9 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg Committed by Gijs Hendriksen
Browse files

Refactor profile actions to use saga

parent fceda41c
...@@ -5,6 +5,3 @@ export const CALENDARRETREIVED = 'CALENDARRETREIVED'; ...@@ -5,6 +5,3 @@ export const CALENDARRETREIVED = 'CALENDARRETREIVED';
export const CALENDARERROR = 'CALENDARERROR'; export const CALENDARERROR = 'CALENDARERROR';
export const RESETLOGINSTATE = 'RESETLOGINSTATE'; export const RESETLOGINSTATE = 'RESETLOGINSTATE';
export const WELCOME = 'WELCOME'; export const WELCOME = 'WELCOME';
export const LOADPROFILESTART = 'LOADPROFILESTART';
export const LOADPROFILESUCCESS = 'LOADPROFILESUCCESS';
export const LOADPROFILEFAILURE = 'LOADPROFILEFAILURE';
import * as types from './actionTypes'; export const PROFILE = 'PROFILE_PROFILE';
import { navigate } from './navigation'; export const FETCHING = 'PROFILE_FETCHING';
export const SUCCESS = 'PROFILE_SUCCESS';
export const FAILURE = 'PROFILE_FAILURE';
import { apiUrl } from '../url'; export function profile(token, member = 'me') {
export function start() {
return { return {
type: types.LOADPROFILESTART, type: PROFILE,
payload: { token, member },
}; };
} }
export function success(profile) { export function fetching() {
return { return {
type: types.LOADPROFILESUCCESS, type: FETCHING,
profile,
}; };
} }
export function fail() { export function success(profileData) {
return { return {
type: types.LOADPROFILEFAILURE, type: SUCCESS,
payload: { profileData },
}; };
} }
export function loadProfile(token, member = 'me') { export function failure() {
return (dispatch) => { return {
dispatch(start()); type: FAILURE,
dispatch(navigate('profile'));
const data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
return fetch(`${apiUrl}/members/${member}/`, data)
.then(
response => response.json(),
)
.then(
response => dispatch(success(response)),
)
.catch(
() => dispatch(fail()),
);
}; };
} }
...@@ -7,7 +7,7 @@ import { connect } from 'react-redux'; ...@@ -7,7 +7,7 @@ import { connect } from 'react-redux';
import { url } from '../url'; import { url } from '../url';
import styles from './style/memberView'; import styles from './style/memberView';
import SquareView from './SquareView'; import SquareView from './SquareView';
import { loadProfile } from '../actions/profile'; import * as profileActions from '../actions/profile';
const regex = new RegExp(`^(${url}/media/public)/(avatars/[^\\.]+)\\.(jpg|jpeg|png|gif)`); const regex = new RegExp(`^(${url}/media/public)/(avatars/[^\\.]+)\\.(jpg|jpeg|png|gif)`);
...@@ -57,7 +57,7 @@ const mapStateToProps = state => ({ ...@@ -57,7 +57,7 @@ const mapStateToProps = state => ({
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
loadProfile: (token, pk) => dispatch(loadProfile(token, pk)), loadProfile: (token, pk) => dispatch(profileActions.profile(token, pk)),
}); });
export default connect(mapStateToProps, mapDispatchToProps)(MemberView); export default connect(mapStateToProps, mapDispatchToProps)(MemberView);
...@@ -9,7 +9,7 @@ import styles from './style/sidebar'; ...@@ -9,7 +9,7 @@ import styles from './style/sidebar';
import * as navigationActions from '../actions/navigation'; import * as navigationActions from '../actions/navigation';
import * as loginActions from '../actions/login'; import * as loginActions from '../actions/login';
import { loadProfile } from '../actions/profile'; import * as profileActions from '../actions/profile';
const background = require('../img/huygens.jpg'); const background = require('../img/huygens.jpg');
...@@ -106,7 +106,7 @@ const mapStateToProps = state => ({ ...@@ -106,7 +106,7 @@ const mapStateToProps = state => ({
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
navigate: (scene, newSection = false) => dispatch(navigationActions.navigate(scene, newSection)), navigate: (scene, newSection = false) => dispatch(navigationActions.navigate(scene, newSection)),
logout: () => dispatch(loginActions.logout()), logout: () => dispatch(loginActions.logout()),
loadProfile: token => dispatch(loadProfile(token)), loadProfile: token => dispatch(profileActions.profile(token)),
}); });
export default connect(mapStateToProps, mapDispatchToProps)(Sidebar); export default connect(mapStateToProps, mapDispatchToProps)(Sidebar);
import * as types from '../actions/actionTypes'; import * as profileActions from '../actions/profile';
const initialState = { const initialState = {
profile: { profile: {
...@@ -19,19 +19,19 @@ const initialState = { ...@@ -19,19 +19,19 @@ const initialState = {
export default function profile(state = initialState, action = {}) { export default function profile(state = initialState, action = {}) {
switch (action.type) { switch (action.type) {
case types.LOADPROFILESTART: case profileActions.FETCHING:
return { return {
...state, ...state,
hasLoaded: false, hasLoaded: false,
}; };
case types.LOADPROFILESUCCESS: case profileActions.SUCCESS:
return { return {
...state, ...state,
profile: action.profile, profile: action.payload.profileData,
success: true, success: true,
hasLoaded: true, hasLoaded: true,
}; };
case types.LOADPROFILEFAILURE: case profileActions.FAILURE:
return { return {
...state, ...state,
success: false, success: false,
......
...@@ -2,11 +2,13 @@ import { all, fork } from 'redux-saga/effects'; ...@@ -2,11 +2,13 @@ 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';
const sagas = function* sagas() { const sagas = function* sagas() {
yield all([ yield all([
fork(loginSaga), fork(loginSaga),
fork(eventSaga), fork(eventSaga),
fork(profileSaga),
]); ]);
}; };
......
import { call, put, takeEvery } from 'redux-saga/effects';
import { apiRequest } from '../url';
import * as profileActions from '../actions/profile';
import * as navActions from '../actions/navigation';
const profile = function* profile(action) {
const { token, member } = action.payload;
yield put(profileActions.fetching());
yield put(navActions.navigate('profile'));
const data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
try {
const profileData = yield call(apiRequest, `members/${member}`, data);
yield put(profileActions.success(profileData));
} catch (error) {
yield put(profileActions.failure());
}
};
const profileSaga = function* eventSaga() {
yield takeEvery(profileActions.PROFILE, profile);
};
export default profileSaga;
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