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.
194 lines
6.0 KiB
194 lines
6.0 KiB
import { effects } from "redux-saga"; |
|
import { setSelfUserToken } from "../actions/auth/reducer.actions"; |
|
import { |
|
isSendingUserRequest, |
|
setUserRequestError, |
|
setUserRequestSuccess, |
|
clearUserRequestError, |
|
clearUserRequestSuccess, |
|
setSelfUser, |
|
setCompleteRegistrationStep |
|
} from "../actions/user/reducer.actions"; |
|
import { getSelfUserRequest } from "../actions/user/saga.actions"; |
|
import { CLIENT_OR_PROVIDER_STEP } from "../constants/user.constants"; |
|
import { |
|
getSelfUser, |
|
createUserInfo, |
|
updateUserInfo, |
|
createClient, |
|
updateClient, |
|
createProvider, |
|
updateProvider |
|
} from "../api/user.api"; |
|
|
|
function* getSelfUserCall() { |
|
yield effects.put(isSendingUserRequest(true)); |
|
try { |
|
const wasSuccessful = yield effects.call(getSelfUser); |
|
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)); |
|
} |
|
} |
|
|
|
function* createUserInfoCall(postBody) { |
|
yield effects.put(isSendingUserRequest(true)); |
|
const { phone_number } = postBody; |
|
try { |
|
return yield effects.call(createUserInfo, phone_number); |
|
} catch (exception) { |
|
yield effects.put(setUserRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingUserRequest(false)); |
|
} |
|
} |
|
|
|
function* updateUserInfoCall(payload) { |
|
yield effects.put(isSendingUserRequest(true)); |
|
const { username, phone_number } = payload; |
|
try { |
|
return yield effects.call(updateUserInfo, username, phone_number); |
|
} catch (exception) { |
|
yield effects.put(setUserRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingUserRequest(false)); |
|
} |
|
} |
|
|
|
function* createClientCall(postBody) { |
|
yield effects.put(isSendingUserRequest(true)); |
|
const { business_number } = postBody; |
|
try { |
|
return yield effects.call(createClient, business_number); |
|
} catch (exception) { |
|
yield effects.put(setUserRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingUserRequest(false)); |
|
} |
|
} |
|
|
|
function* updateClientCall(payload) { |
|
yield effects.put(isSendingUserRequest(true)); |
|
const { username, business_number } = payload; |
|
try { |
|
return yield effects.call(updateClient, username, business_number); |
|
} catch (exception) { |
|
yield effects.put(setUserRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingUserRequest(false)); |
|
} |
|
} |
|
|
|
function* createProviderCall(postBody) { |
|
yield effects.put(isSendingUserRequest(true)); |
|
const { sin } = postBody; |
|
try { |
|
return yield effects.call(createProvider, sin); |
|
} catch (exception) { |
|
yield effects.put(setUserRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingUserRequest(false)); |
|
} |
|
} |
|
|
|
function* updateProviderCall(payload) { |
|
yield effects.put(isSendingUserRequest(true)); |
|
const { username, sin } = payload; |
|
try { |
|
return yield effects.call(updateProvider, username, sin); |
|
} catch (exception) { |
|
yield effects.put(setUserRequestError(exception)); |
|
return false; |
|
} finally { |
|
yield effects.put(isSendingUserRequest(false)); |
|
} |
|
} |
|
|
|
export function* getSelfUserFlow(request) { |
|
const wasSuccessful = yield effects.call(getSelfUserCall); |
|
if (!wasSuccessful) { |
|
yield effects.put(setSelfUserToken("")); |
|
yield effects.put(setSelfUser({})); |
|
} |
|
} |
|
|
|
export function* createUserInfoFlow(request) { |
|
yield effects.put(clearUserRequestSuccess()); |
|
yield effects.put(clearUserRequestError()); |
|
const wasSuccessful = yield effects.call(createUserInfoCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(clearUserRequestError()); |
|
yield effects.put(setCompleteRegistrationStep(CLIENT_OR_PROVIDER_STEP)); |
|
yield effects.put(getSelfUserRequest()); |
|
} |
|
} |
|
|
|
export function* updateUserInfoFlow(request) { |
|
yield effects.put(clearUserRequestSuccess()); |
|
yield effects.put(clearUserRequestError()); |
|
const wasSuccessful = yield effects.call(updateUserInfoCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(setUserRequestSuccess(wasSuccessful)); |
|
yield effects.put(clearUserRequestError()); |
|
yield effects.put(setCompleteRegistrationStep(CLIENT_OR_PROVIDER_STEP)); |
|
yield effects.put(getSelfUserRequest()); |
|
} |
|
} |
|
|
|
export function* createClientFlow(request) { |
|
yield effects.put(clearUserRequestSuccess()); |
|
yield effects.put(clearUserRequestError()); |
|
const wasSuccessful = yield effects.call(createClientCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(clearUserRequestError()); |
|
yield effects.put(getSelfUserRequest()); |
|
} |
|
} |
|
|
|
export function* updateClientFlow(request) { |
|
yield effects.put(clearUserRequestSuccess()); |
|
yield effects.put(clearUserRequestError()); |
|
const wasSuccessful = yield effects.call(updateClientCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(setUserRequestSuccess(wasSuccessful)); |
|
yield effects.put(clearUserRequestError()); |
|
yield effects.put(getSelfUserRequest()); |
|
} |
|
} |
|
|
|
export function* createProviderFlow(request) { |
|
yield effects.put(clearUserRequestSuccess()); |
|
yield effects.put(clearUserRequestError()); |
|
const wasSuccessful = yield effects.call(createProviderCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(clearUserRequestError()); |
|
yield effects.put(getSelfUserRequest()); |
|
} |
|
} |
|
|
|
export function* updateProviderFlow(request) { |
|
yield effects.put(clearUserRequestSuccess()); |
|
yield effects.put(clearUserRequestError()); |
|
const wasSuccessful = yield effects.call(updateProviderCall, request.data); |
|
if (wasSuccessful) { |
|
yield effects.put(setUserRequestSuccess(wasSuccessful)); |
|
yield effects.put(clearUserRequestError()); |
|
yield effects.put(getSelfUserRequest()); |
|
} |
|
}
|
|
|