33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
import { DaddyClient } from "../../util/daddy";
|
|
import { Config } from "../../config";
|
|
import { all, takeLatest, put, select } from "redux-saga/effects";
|
|
import { FETCH_PROFILE_REQUEST, fetchProfile, FETCH_PROFILE_FAILURE, FETCH_PROFILE_SUCCESS } from "../actions/profile";
|
|
import { SET_CURRENT_USER } from "../actions/auth";
|
|
import { RootState } from "../reducers/root";
|
|
import { User } from "../../types/user";
|
|
|
|
export function* usersRootSaga() {
|
|
yield all([
|
|
takeLatest(SET_CURRENT_USER, onCurrentUserChangeSaga),
|
|
takeLatest(FETCH_PROFILE_REQUEST, fetchProfileSaga),
|
|
]);
|
|
}
|
|
|
|
export function* onCurrentUserChangeSaga() {
|
|
yield put(fetchProfile());
|
|
}
|
|
|
|
export function* fetchProfileSaga() {
|
|
const client = new DaddyClient(Config.graphQLEndpoint);
|
|
|
|
let profile: User;
|
|
try {
|
|
const currentUser: User = yield select((state: RootState) => state.auth.currentUser);
|
|
profile = yield client.fetchUser(currentUser.email).then(result => result.user);
|
|
} catch(err) {
|
|
yield put({ type: FETCH_PROFILE_FAILURE, err });
|
|
return;
|
|
}
|
|
|
|
yield put({type: FETCH_PROFILE_SUCCESS, profile });
|
|
} |