Commit 51ca3cbd authored by Gijs Hendriksen's avatar Gijs Hendriksen
Browse files

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

Refactor profile actions to use saga

See merge request !63
parents fceda41c 330edce9
......@@ -5,6 +5,3 @@ export const CALENDARRETREIVED = 'CALENDARRETREIVED';
export const CALENDARERROR = 'CALENDARERROR';
export const RESETLOGINSTATE = 'RESETLOGINSTATE';
export const WELCOME = 'WELCOME';
export const LOADPROFILESTART = 'LOADPROFILESTART';
export const LOADPROFILESUCCESS = 'LOADPROFILESUCCESS';
export const LOADPROFILEFAILURE = 'LOADPROFILEFAILURE';
import * as types from './actionTypes';
import { navigate } from './navigation';
export const PROFILE = 'PROFILE_PROFILE';
export const FETCHING = 'PROFILE_FETCHING';
export const SUCCESS = 'PROFILE_SUCCESS';
export const FAILURE = 'PROFILE_FAILURE';
import { apiUrl } from '../url';
export function start() {
export function profile(token, member = 'me') {
return {
type: types.LOADPROFILESTART,
type: PROFILE,
payload: { token, member },
};
}
export function success(profile) {
export function fetching() {
return {
type: types.LOADPROFILESUCCESS,
profile,
type: FETCHING,
};
}
export function fail() {
export function success(profileData) {
return {
type: types.LOADPROFILEFAILURE,
type: SUCCESS,
payload: { profileData },
};
}
export function loadProfile(token, member = 'me') {
return (dispatch) => {
dispatch(start());
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()),
);
export function failure() {
return {
type: FAILURE,
};
}
......@@ -7,7 +7,7 @@ import { connect } from 'react-redux';
import { url } from '../url';
import styles from './style/memberView';
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)`);
......@@ -57,7 +57,7 @@ const mapStateToProps = state => ({
});
const mapDispatchToProps = dispatch => ({
loadProfile: (token, pk) => dispatch(loadProfile(token, pk)),
loadProfile: (token, pk) => dispatch(profileActions.profile(token, pk)),
});
export default connect(mapStateToProps, mapDispatchToProps)(MemberView);
......@@ -9,7 +9,7 @@ import styles from './style/sidebar';
import * as navigationActions from '../actions/navigation';
import * as loginActions from '../actions/login';
import { loadProfile } from '../actions/profile';
import * as profileActions from '../actions/profile';
const background = require('../img/huygens.jpg');
......@@ -106,7 +106,7 @@ const mapStateToProps = state => ({
const mapDispatchToProps = dispatch => ({
navigate: (scene, newSection = false) => dispatch(navigationActions.navigate(scene, newSection)),
logout: () => dispatch(loginActions.logout()),
loadProfile: token => dispatch(loadProfile(token)),
loadProfile: token => dispatch(profileActions.profile(token)),
});
export default connect(mapStateToProps, mapDispatchToProps)(Sidebar);
import * as types from '../actions/actionTypes';
import * as profileActions from '../actions/profile';
const initialState = {
profile: {
......@@ -19,19 +19,19 @@ const initialState = {
export default function profile(state = initialState, action = {}) {
switch (action.type) {
case types.LOADPROFILESTART:
case profileActions.FETCHING:
return {
...state,
hasLoaded: false,
};
case types.LOADPROFILESUCCESS:
case profileActions.SUCCESS:
return {
...state,
profile: action.profile,
profile: action.payload.profileData,
success: true,
hasLoaded: true,
};
case types.LOADPROFILEFAILURE:
case profileActions.FAILURE:
return {
...state,
success: false,
......
......@@ -2,11 +2,13 @@ import { all, fork } from 'redux-saga/effects';
import loginSaga from './login';
import eventSaga from './event';
import profileSaga from './profile';
const sagas = function* sagas() {
yield all([
fork(loginSaga),
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