{"size": 2416, "ext": "js", "max_stars_repo_path": "src/common/actions/profileAction.js", "max_stars_repo_name": "Popov85/ratos3-frontend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/common/actions/profileAction.js", "max_issues_repo_name": "Popov85/ratos3-frontend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/common/actions/profileAction.js", "max_forks_repo_name": "Popov85/ratos3-frontend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 45.5849056604, "max_line_length": 105, "alphanum_fraction": 0.7404801325}
import {profileAPI} from "../_api/profileAPI";
import {updateUserProfile} from "./userActions";

const UPDATING_USER_PROFILE = "UPDATING_USER_PROFILE";
const UPDATING_USER_PROFILE_FAILURE = "UPDATING_USER_PROFILE_FAILURE";

const UPDATING_USER_PASSWORD = "UPDATING_USER_PASSWORD";
const UPDATING_USER_PASSWORD_FAILURE = "UPDATING_USER_PASSWORD_FAILURE";

const SET_USER_PROFILE_UPDATED = "SET_USER_PROFILE_UPDATED";
const SET_USER_PASSWORD_UPDATED = "SET_USER_PASSWORD_UPDATED";

const CLEAR_USER_PROFILE_FAILURE = "CLEAR_USER_PROFILE_FAILURE";
const CLEAR_USER_PASSWORD_FAILURE = "CLEAR_USER_PASSWORD_FAILURE";

export const updatingUserProfile = isUpdating => ({type: UPDATING_USER_PROFILE, isUpdating});
export const updatingUserProfileFailure = error => ({type: UPDATING_USER_PROFILE_FAILURE, error});

export const updatingUserPassword = isUpdating => ({type: UPDATING_USER_PASSWORD, isUpdating});
export const updatingUserPasswordFailure = error => ({type: UPDATING_USER_PASSWORD_FAILURE, error});

export const setUserProfileUpdated = (message) => ({type: SET_USER_PROFILE_UPDATED, payload: message});
export const setUserPasswordUpdated = (message) => ({type: SET_USER_PASSWORD_UPDATED, payload: message});

export const clearUserProfileFailure = () => ({type: CLEAR_USER_PROFILE_FAILURE});
export const clearUserPasswordFailure = () => ({type: CLEAR_USER_PASSWORD_FAILURE});


export const getProfileUpdated = (profile) => {
    return (dispatch) => {
        dispatch(clearUserProfileFailure());
        dispatch(updatingUserProfile(true));
        profileAPI.updateProfile(profile).then(() => {
            dispatch(updateUserProfile(profile));
            dispatch(setUserProfileUpdated("Successfully updated profile!"));
        }).catch(e => {
            dispatch(updatingUserProfileFailure(new Error("Failed to update profile")));
        }).finally(() => dispatch(updatingUserProfile(false)));
    }
}

export const getPasswordUpdated = (passwords) => {
    return (dispatch) => {
        dispatch(clearUserPasswordFailure());
        dispatch(updatingUserPassword(true));
        profileAPI.updatePassword(passwords).then(() => {
            dispatch(setUserPasswordUpdated("Successfully updated password!"));
        }).catch(e => {
            dispatch(updatingUserPasswordFailure(new Error("Failed to update password")));
        }).finally(() => dispatch(updatingUserPassword(false)));
    }
}
