Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

registerSaga.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { all, takeLatest, call } from "@redux-saga/core/effects";
  2. import { attemptRegister } from "../../request/registerRequest";
  3. import { REGISTER_USER_FETCH } from "../actions/register/registerActionConstants";
  4. function* fetchRegisterUser({ payload }) {
  5. try {
  6. const requestData = {
  7. email: payload.values.mail.toString(),
  8. password: payload.values.password.toString(),
  9. roles: [
  10. {
  11. role: "User",
  12. },
  13. ],
  14. company: {
  15. name: payload.values.nameOfFirm.toString(),
  16. PIB: payload.values.PIB.toString(),
  17. contacts: {
  18. telephone: payload.values.phoneNumber.toString(),
  19. location: payload.values.location.toString(),
  20. web: payload.values.website.toString(),
  21. },
  22. },
  23. };
  24. yield call(attemptRegister, requestData);
  25. if (payload.handleResponseSuccess) {
  26. yield call(payload.handleResponseSuccess);
  27. }
  28. } catch (e) {
  29. let type;
  30. if (
  31. e.response?.data?.toString() === "User with email already exists" ||
  32. e.response?.data?.toString() === '"email" must be a valid email'
  33. ) {
  34. type = "mail";
  35. } else if (
  36. e.response?.data?.toString() === "Company with PIB already exists"
  37. ) {
  38. type = "PIB";
  39. }
  40. const error = {
  41. error: e,
  42. type,
  43. };
  44. if (payload.handleResponseError) {
  45. yield call(payload.handleResponseError, error);
  46. }
  47. }
  48. }
  49. export default function* registerSaga() {
  50. yield all([takeLatest(REGISTER_USER_FETCH, fetchRegisterUser)]);
  51. }