userProfile.ts 1.38 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1
import { UserProfileService } from 'service/UserProfileService';
2
import { log } from 'utilities/debug';
Maxym Borodenko's avatar
Maxym Borodenko committed
3 4 5 6
import { SubmissionError } from 'redux-form';
import * as _ from 'lodash';
import { showSnackbar } from 'actions/snackbar';
import { RECEIVE_USER_PROFILE } from 'constants/userProfile';
7
import User from 'model/user/User';
Maxym Borodenko's avatar
Maxym Borodenko committed
8 9
import { IReducerAction } from 'model/common.model';

Maxym Borodenko's avatar
Maxym Borodenko committed
10
export const receiveUserProfile = (user: User): IReducerAction => ({
Maxym Borodenko's avatar
Maxym Borodenko committed
11 12 13 14
    type: RECEIVE_USER_PROFILE, payload: user,
});

export const loadUserProfile = () => (dispatch, getState) => {
15
    return UserProfileService.getProfile()
Maxym Borodenko's avatar
Maxym Borodenko committed
16 17 18 19 20 21 22 23 24 25
    // receive the current user profile
        .then((userProfile) => {
            return dispatch(receiveUserProfile(userProfile));
        })
        .catch((error) => {
            log('Error', error);
        });
};

export const changePassword = (newPassword: string, oldPassword: string) => (dispatch, getState) => {
26
    return UserProfileService.changePassword(newPassword, oldPassword)
Maxym Borodenko's avatar
Maxym Borodenko committed
27
        .then(() => {
Oleksii Savran's avatar
I18n  
Oleksii Savran committed
28
            dispatch(showSnackbar('user.common.passChangeSuccess'));
Maxym Borodenko's avatar
Maxym Borodenko committed
29 30 31
        }).catch((error) => {
            const data = _.get(error, 'response.data');
            log('Save error', data.error);
Oleksii Savran's avatar
I18n  
Oleksii Savran committed
32
            dispatch(showSnackbar(data.error || 'user.common.passChangeError'));
Maxym Borodenko's avatar
Maxym Borodenko committed
33 34 35 36
            throw new SubmissionError({ title: 'Password change error', _error: data.error });
        });
};