2018-08-23 16:29:26 +01:00
|
|
|
import { call, put, takeEvery } from 'redux-saga/effects';
|
|
|
|
|
2018-11-07 18:15:27 +00:00
|
|
|
import {
|
|
|
|
fetchUserComplete,
|
|
|
|
fetchUserError,
|
|
|
|
fetchProfileForUserError,
|
|
|
|
fetchProfileForUserComplete
|
|
|
|
} from './';
|
|
|
|
import { getSessionUser, getUserProfile } from '../utils/ajax';
|
2018-10-24 00:24:48 +01:00
|
|
|
import { jwt } from './cookieValues';
|
2018-08-23 16:29:26 +01:00
|
|
|
|
|
|
|
function* fetchSessionUser() {
|
2018-10-24 00:24:48 +01:00
|
|
|
if (!jwt) {
|
|
|
|
yield put(fetchUserComplete({ user: {}, username: '' }));
|
|
|
|
return;
|
|
|
|
}
|
2018-08-23 16:29:26 +01:00
|
|
|
try {
|
|
|
|
const {
|
2018-11-29 14:24:17 +00:00
|
|
|
data: { user = {}, result = '', sessionMeta = {} }
|
2018-08-23 16:29:26 +01:00
|
|
|
} = yield call(getSessionUser);
|
2018-11-07 18:15:27 +00:00
|
|
|
const appUser = user[result] || {};
|
2018-11-29 14:24:17 +00:00
|
|
|
yield put(
|
2019-12-13 00:52:36 +05:30
|
|
|
fetchUserComplete({ user: appUser, username: result, sessionMeta })
|
2018-11-29 14:24:17 +00:00
|
|
|
);
|
2018-08-23 16:29:26 +01:00
|
|
|
} catch (e) {
|
|
|
|
yield put(fetchUserError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 12:58:34 +00:00
|
|
|
function* fetchOtherUser({ payload: maybeUser = '' }) {
|
2018-11-07 18:15:27 +00:00
|
|
|
try {
|
2019-03-01 12:58:34 +00:00
|
|
|
const maybeUserLC = maybeUser.toLowerCase();
|
|
|
|
const { data } = yield call(getUserProfile, maybeUserLC);
|
2018-11-07 18:15:27 +00:00
|
|
|
|
|
|
|
const { entities: { user = {} } = {}, result = '' } = data;
|
|
|
|
const otherUser = user[result] || {};
|
|
|
|
yield put(
|
|
|
|
fetchProfileForUserComplete({ user: otherUser, username: result })
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
yield put(fetchProfileForUserError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 16:29:26 +01:00
|
|
|
export function createFetchUserSaga(types) {
|
2018-11-07 18:15:27 +00:00
|
|
|
return [
|
|
|
|
takeEvery(types.fetchUser, fetchSessionUser),
|
|
|
|
takeEvery(types.fetchProfileForUser, fetchOtherUser)
|
|
|
|
];
|
2018-08-23 16:29:26 +01:00
|
|
|
}
|