You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 lines
1.4 KiB

import { effects } from "redux-saga";
import { setSelfUserToken } from "../actions/auth/reducer.actions";
import {
isSendingUserRequest,
setUserRequestError,
setUserRequestSuccess,
clearUserRequestError,
clearUserRequestSuccess,
setSelfUser
} from "../actions/user/reducer.actions";
import { getSelfUser } from "../api/user.api";
function* getSelfUserCall() {
yield effects.put(isSendingUserRequest(true));
try {
const wasSuccessful = yield effects.call(getSelfUser);
yield effects.put(setUserRequestSuccess(wasSuccessful));
yield effects.put(clearUserRequestError());
// Check if the user exists, if yes set the user, otherwise force logout
if (wasSuccessful.results && wasSuccessful.results.length) {
yield effects.put(setSelfUser(wasSuccessful.results[0]));
} else {
yield effects.put(setSelfUserToken(""));
yield effects.put(setSelfUser({}));
}
return wasSuccessful;
} catch (exception) {
yield effects.put(setUserRequestError(exception));
return false;
} finally {
yield effects.put(isSendingUserRequest(false));
}
}
export function* getSelfUserFlow(request) {
yield effects.put(clearUserRequestSuccess());
yield effects.put(clearUserRequestError());
const wasSuccessful = yield effects.call(getSelfUserCall);
if (!wasSuccessful) {
yield effects.put(setSelfUserToken(""));
yield effects.put(setSelfUser({}));
}
}