Commit c2897200 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Merge branch '44-update-profile-in-sidebar' into 'master'

Resolve "Name is not updated in menu"

Closes #44

See merge request !134
parents 1b0a9ba6 0da41d8d
export const LOGIN = 'LOGIN_LOGIN';
export const SUCCESS = 'LOGIN_SUCCESS';
export const LOGOUT = 'LOGIN_LOGOUT';
export const PROFILE = 'LOGIN_PROFILE';
export const PROFILE_SUCCESS = 'LOGIN_PROFILE_SUCCESS';
export function success(username, token, displayName, photo) {
return { type: SUCCESS, payload: { username, token, displayName, photo } };
export function success(username, token) {
return { type: SUCCESS, payload: { username, token } };
}
export function login(user, pass) {
......@@ -13,3 +15,11 @@ export function login(user, pass) {
export function logout() {
return { type: LOGOUT };
}
export function profile(token) {
return { type: PROFILE, payload: { token } };
}
export function profileSuccess(displayName, photo) {
return { type: PROFILE_SUCCESS, payload: { displayName, photo } };
}
......@@ -61,7 +61,9 @@ class Main extends Component {
const photo = values[PHOTOKEY];
if (username !== null && token !== null) {
store.dispatch(loginActions.success(username, token, displayName, photo, ''));
store.dispatch(loginActions.success(username, token));
store.dispatch(loginActions.profileSuccess(displayName, photo));
store.dispatch(loginActions.profile(token));
store.dispatch(register());
}
});
......
import { defaultProfileImage } from '../url';
import * as loginActions from '../actions/login';
const initialState = {
token: '',
username: '',
displayName: '',
photo: '',
photo: defaultProfileImage,
};
export default function session(state = initialState, action = {}) {
......@@ -14,6 +16,10 @@ export default function session(state = initialState, action = {}) {
...state,
username: action.payload.username,
token: action.payload.token,
};
case loginActions.PROFILE_SUCCESS:
return {
...state,
displayName: action.payload.displayName,
photo: action.payload.photo,
};
......
......@@ -2,7 +2,7 @@ import { call, takeEvery, put } from 'redux-saga/effects';
import { AsyncStorage } from 'react-native';
import Snackbar from 'react-native-snackbar';
import { apiRequest, url } from '../url';
import { apiRequest } from '../url';
import * as loginActions from '../actions/login';
import * as pushNotificationsActions from '../actions/pushNotifications';
......@@ -11,14 +11,12 @@ const TOKENKEY = '@MyStore:token';
const DISPLAYNAMEKEY = '@MyStore:displayName';
const PHOTOKEY = '@MyStore:photo';
const defaultAvatar = `${url}/static/members/images/default-avatar.jpg`;
const login = function* login(action) {
const { user, pass } = action.payload;
Snackbar.show({ title: 'Logging in', duration: Snackbar.LENGTH_INDEFINITE });
let data = {
const data = {
method: 'POST',
headers: {
Accept: 'application/json',
......@@ -30,28 +28,15 @@ const login = function* login(action) {
}),
};
try {
let response = yield call(apiRequest, 'token-auth', data);
const response = yield call(apiRequest, 'token-auth', data);
const { token } = response;
data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
response = yield call(apiRequest, 'members/me', data);
const displayName = response.display_name;
const avatar = response.photo === null ? defaultAvatar : response.photo;
yield call(AsyncStorage.multiSet, [
[USERNAMEKEY, user],
[TOKENKEY, token],
[DISPLAYNAMEKEY, displayName],
[PHOTOKEY, avatar],
]);
yield put(loginActions.success(
user, token, displayName, avatar,
));
yield put(loginActions.success(user, token));
yield put(loginActions.profile(token));
yield put(pushNotificationsActions.register());
Snackbar.dismiss();
Snackbar.show({ title: 'Login successful' });
......@@ -67,9 +52,35 @@ const logout = function* logout() {
Snackbar.show({ title: 'Logout successful' });
};
const profile = function* profile(action) {
const { token } = action.payload;
const data = {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${token}`,
},
};
try {
const userProfile = yield call(apiRequest, 'members/me', data);
yield call(AsyncStorage.multiSet, [
[DISPLAYNAMEKEY, userProfile.display_name],
[PHOTOKEY, userProfile.avatar.medium],
]);
yield put(loginActions.profileSuccess(userProfile.display_name, userProfile.avatar.medium));
} catch (error) {
// Swallow error
}
};
const loginSaga = function* loginSaga() {
yield takeEvery(loginActions.LOGIN, login);
yield takeEvery(loginActions.LOGOUT, logout);
yield takeEvery(loginActions.PROFILE, profile);
};
export default loginSaga;
Supports Markdown
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