referralAction.js 1.61 KB
import axios from "axios";
import { REFERRAL_SUBMIT_FAIL, REFERRAL_SUBMIT_REQUEST, REFERRAL_SUBMIT_SUCCESS, CLEAR_ERRORS } from "../constants/referralConstants";

export const postReferralDetails = referralData => async dispatch => {
  try {
    dispatch({
      type: REFERRAL_SUBMIT_REQUEST
    });

    const config = {
      headers: {
        "Content-Type": "application/json"
      }
    };

    const response = await axios.post(
      `${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/referrals`,
      {
        data: {
          fullName: referralData.fullName,
          township: referralData.township,
          mobileNumber: referralData.mobileNo,
          project: referralData.building,
          email: referralData.email,
          wing: referralData.wing,
          flatNumber: referralData.flatNo,
          ownershipType: referralData.ownershipType,
          purchasedYear: referralData.purchasedYear,
          purchasedSource: referralData.purchasedSource,
          referralName: referralData.referralName,
          referralPhoneNo: referralData.referralMobileNo,
          referralEmail: referralData.referralEmail,
          projectReferredTo: referralData.projectReferredTo
        }
      },
      config
    );

    dispatch({
      type: REFERRAL_SUBMIT_SUCCESS,
      payload: response.data
    });
  } catch (error) {
    console.log("Error while submitting referral details: ");
    console.log(error);

    dispatch({
      type: REFERRAL_SUBMIT_FAIL,
      payload: error.response.data
    });
  }
};

// Clear errors
export const clearErrors = () => async dispatch => {
  dispatch({
    type: CLEAR_ERRORS
  });
};