| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import { all, takeLatest, call } from "@redux-saga/core/effects";
- import { attemptRegister } from "../../request/registerRequest";
- import { REGISTER_USER_FETCH } from "../actions/register/registerActionConstants";
-
- function* fetchRegisterUser({ payload }) {
- try {
- const requestData = {
- email: payload.values.mail.toString(),
- password: payload.values.password.toString(),
- roles: [
- {
- role: "User",
- },
- ],
- company: {
- name: payload.values.nameOfFirm.toString(),
- PIB: payload.values.PIB.toString(),
- contacts: {
- telephone: payload.values.phoneNumber.toString(),
- location: payload.values.location.toString(),
- web: payload.values.website.toString(),
- },
- },
- };
- yield call(attemptRegister, requestData);
- if (payload.handleResponseSuccess) {
- yield call(payload.handleResponseSuccess);
- }
- } catch (e) {
- let type;
- if (
- e.response?.data?.toString() === "User with email already exists" ||
- e.response?.data?.toString() === '"email" must be a valid email'
- ) {
- type = "mail";
- } else if (
- e.response?.data?.toString() === "Company with PIB already exists"
- ) {
- type = "PIB";
- }
- const error = {
- error: e,
- type,
- };
- if (payload.handleResponseError) {
- yield call(payload.handleResponseError, error);
- }
- }
- }
-
- export default function* registerSaga() {
- yield all([takeLatest(REGISTER_USER_FETCH, fetchRegisterUser)]);
- }
|