2020-04-01 19:20:47 -07:00
|
|
|
import api from '../api';
|
|
|
|
import { importFetchedAccount } from './importer';
|
|
|
|
|
|
|
|
export const ME_FETCH_REQUEST = 'ME_FETCH_REQUEST';
|
|
|
|
export const ME_FETCH_SUCCESS = 'ME_FETCH_SUCCESS';
|
|
|
|
export const ME_FETCH_FAIL = 'ME_FETCH_FAIL';
|
2020-04-11 16:55:07 -07:00
|
|
|
export const ME_FETCH_SKIP = 'ME_FETCH_SKIP';
|
2020-04-01 19:20:47 -07:00
|
|
|
|
2020-04-21 13:53:32 -07:00
|
|
|
export const ME_PATCH_REQUEST = 'ME_PATCH_REQUEST';
|
|
|
|
export const ME_PATCH_FAIL = 'ME_PATCH_FAIL';
|
|
|
|
|
2020-04-17 14:08:07 -07:00
|
|
|
function hasToken(getState) {
|
|
|
|
const accessToken = getState().getIn(['auth', 'user', 'access_token']);
|
|
|
|
return Boolean(accessToken);
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:20:47 -07:00
|
|
|
export function fetchMe() {
|
|
|
|
return (dispatch, getState) => {
|
2020-04-17 14:08:07 -07:00
|
|
|
|
|
|
|
if (!hasToken(getState)) {
|
|
|
|
dispatch({ type: ME_FETCH_SKIP }); return;
|
2020-04-14 13:45:38 -07:00
|
|
|
};
|
2020-04-17 14:08:07 -07:00
|
|
|
|
2020-04-01 19:20:47 -07:00
|
|
|
dispatch(fetchMeRequest());
|
2020-04-17 14:08:07 -07:00
|
|
|
|
2020-04-14 11:44:40 -07:00
|
|
|
api(getState).get('/api/v1/accounts/verify_credentials').then(response => {
|
2020-04-01 19:20:47 -07:00
|
|
|
dispatch(fetchMeSuccess(response.data));
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(fetchMeFail(error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:53:32 -07:00
|
|
|
export function patchMe(params) {
|
|
|
|
return (dispatch, getState) => {
|
2020-04-21 16:00:05 -07:00
|
|
|
dispatch(patchMeRequest());
|
2020-04-21 13:53:32 -07:00
|
|
|
return api(getState)
|
|
|
|
.patch('/api/v1/accounts/update_credentials', params)
|
|
|
|
.then(response => {
|
|
|
|
dispatch(fetchMeSuccess(response.data));
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(patchMeFail(error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:20:47 -07:00
|
|
|
export function fetchMeRequest() {
|
|
|
|
return {
|
|
|
|
type: ME_FETCH_REQUEST,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function fetchMeSuccess(me) {
|
2020-04-21 16:00:05 -07:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch(importFetchedAccount(me));
|
|
|
|
dispatch({
|
|
|
|
type: ME_FETCH_SUCCESS,
|
|
|
|
me,
|
|
|
|
});
|
2020-04-01 19:20:47 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function fetchMeFail(error) {
|
|
|
|
return {
|
|
|
|
type: ME_FETCH_FAIL,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
};
|
2020-04-21 13:53:32 -07:00
|
|
|
|
|
|
|
export function patchMeRequest() {
|
|
|
|
return {
|
|
|
|
type: ME_PATCH_REQUEST,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function patchMeFail(error) {
|
|
|
|
return {
|
|
|
|
type: ME_PATCH_FAIL,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
};
|