1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-01 01:18:10 +02:00
unleash.unleash/frontend/src/store/user/actions.js
2018-04-16 14:27:34 +02:00

43 lines
1.0 KiB
JavaScript

import api from '../../data/user-api';
import { dispatchAndThrow } from '../util';
export const UPDATE_USER = 'UPDATE_USER';
export const START_FETCH_USER = 'START_FETCH_USER';
export const ERROR_FETCH_USER = 'ERROR_FETCH_USER';
const debug = require('debug')('unleash:user-actions');
const updateUser = value => ({
type: UPDATE_USER,
value,
});
function handleError(error) {
debug(error);
}
export function fetchUser() {
debug('Start fetching user');
return dispatch => {
dispatch({ type: START_FETCH_USER });
return api
.fetchUser()
.then(json => dispatch(updateUser(json)))
.catch(dispatchAndThrow(dispatch, ERROR_FETCH_USER));
};
}
export function unsecureLogin(path, user) {
return dispatch => {
dispatch({ type: START_FETCH_USER });
return api
.unsecureLogin(path, user)
.then(json => dispatch(updateUser(json)))
.catch(handleError);
};
}
export function logoutUser() {
return () => api.logoutUser().catch(handleError);
}