Files
freeCodeCamp/client/src/redux/fetch-user-saga.js
Mrugesh Mohapatra fdb17223ec revert : Added displayUsername and username update functionality
removes implemenation from #35699
This reverts commit e154f38118.
2019-12-13 01:07:05 +05:30

51 lines
1.3 KiB
JavaScript

import { call, put, takeEvery } from 'redux-saga/effects';
import {
fetchUserComplete,
fetchUserError,
fetchProfileForUserError,
fetchProfileForUserComplete
} from './';
import { getSessionUser, getUserProfile } from '../utils/ajax';
import { jwt } from './cookieValues';
function* fetchSessionUser() {
if (!jwt) {
yield put(fetchUserComplete({ user: {}, username: '' }));
return;
}
try {
const {
data: { user = {}, result = '', sessionMeta = {} }
} = yield call(getSessionUser);
const appUser = user[result] || {};
yield put(
fetchUserComplete({ user: appUser, username: result, sessionMeta })
);
} catch (e) {
yield put(fetchUserError(e));
}
}
function* fetchOtherUser({ payload: maybeUser = '' }) {
try {
const maybeUserLC = maybeUser.toLowerCase();
const { data } = yield call(getUserProfile, maybeUserLC);
const { entities: { user = {} } = {}, result = '' } = data;
const otherUser = user[result] || {};
yield put(
fetchProfileForUserComplete({ user: otherUser, username: result })
);
} catch (e) {
yield put(fetchProfileForUserError(e));
}
}
export function createFetchUserSaga(types) {
return [
takeEvery(types.fetchUser, fetchSessionUser),
takeEvery(types.fetchProfileForUser, fetchOtherUser)
];
}