Blame view

redux/actions/vendorActions.js 5.4 KB
jay committed
1
import axios from "axios";
jay committed
2 3 4
import { getSession } from "next-auth/react";
import qs from "qs";
import {
jaymehta committed
5 6 7
  GET_ALL_VENDORS_FAIL,
  GET_ALL_VENDORS_REQUEST,
  GET_ALL_VENDORS_SUCCESS,
jay committed
8 9 10
  GET_LOGGED_IN_VENDOR_FAIL,
  GET_LOGGED_IN_VENDOR_REQUEST,
  GET_LOGGED_IN_VENDOR_SUCCESS,
jaymehta committed
11 12 13
  GET_VENDOR_DETAILS_FAIL,
  GET_VENDOR_DETAILS_REQUEST,
  GET_VENDOR_DETAILS_SUCCESS,
jay committed
14 15 16 17
  UPDATE_VENDOR_DETAILS_FAIL,
  UPDATE_VENDOR_DETAILS_REQUEST,
  UPDATE_VENDOR_DETAILS_SUCCESS
} from "../constants/vendorConstants";
jay committed
18 19 20 21 22 23 24 25 26 27

export const finishVendorOtpVerification = async verificationData => {
  const config = {
    headers: {
      "Content-Type": "application/json"
    }
  };

  return await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendor/finish-otp-verification`, verificationData, config);
};
jay committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

export const pincodeSearchByFilter = async code => {
  const config = {
    headers: {
      "Content-Type": "application/json"
    }
  };

  const query = {
    filters: {
      name: {
        $contains: code
      }
    },
    populate: ["masterCity"]
  };
  const queryString = qs.stringify(query, {
    encodeValuesOnly: true
  });

  return await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/master-pincodes?${queryString}`, config);
};

export const updateVendorBusinessDetails =
  ({ businessDetails, vendorId }) =>
  async dispatch => {
    const session = await getSession();

    if (!session) {
      throw new Error("You are not authenticated. Please log in.");
    }

    try {
      dispatch({
        type: UPDATE_VENDOR_DETAILS_REQUEST
      });

      const config = {
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${session.jwt}`
        }
      };

      const response = await axios.put(
        `${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/${vendorId}`,
        {
jaymehta committed
75
          data: businessDetails
jay committed
76 77 78 79 80 81
        },
        config
      );
      dispatch({
        type: UPDATE_VENDOR_DETAILS_SUCCESS
      });
jaymehta committed
82
      return response.data;
jay committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    } catch (error) {
      dispatch({
        type: UPDATE_VENDOR_DETAILS_FAIL
      });
    }
  };

export const getLoggedInVendor = () => async dispatch => {
  const session = await getSession();
  console.log("session", session);

  if (!session) {
    throw new Error("You are not authenticated. Please log in.");
  }

  try {
    dispatch({
      type: GET_LOGGED_IN_VENDOR_REQUEST
    });

    const config = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${session.jwt}`
      }
    };

    const query = {
      filters: {
        user: {
jaymehta committed
113
          id: { $eq: session.id }
jay committed
114 115 116 117 118 119 120 121
        }
      }
    };

    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });

jaymehta committed
122 123
    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/?${queryString}`, config);
    console.log("response", response.data.data[0]);
jay committed
124
    dispatch({
jaymehta committed
125 126
      type: GET_LOGGED_IN_VENDOR_SUCCESS,
      payload: response.data.data[0]
jay committed
127
    });
jaymehta committed
128
    return response.data.data[0];
jay committed
129 130 131 132 133 134
  } catch (error) {
    dispatch({
      type: GET_LOGGED_IN_VENDOR_FAIL
    });
  }
};
jaymehta committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

export const getVendorDetails = () => async dispatch => {
  const session = await getSession();
  try {
    dispatch({
      type: GET_VENDOR_DETAILS_REQUEST
    });

    console.log("here 1", session);
    const config = {
      headers: {
        "Content-Type": "Application/json",
        Authorization: `Bearer ${session.jwt}`
      }
    };
    console.log("here 2");
    if (!session) {
      return;
    }
    const query = {
      filters: {
        user: {
          id: {
            $eq: session.id
          }
        }
      },
      populate: ["user"]
    };
    console.log("here 3", query);

    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });
    console.log("session", session, session.id);
    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/?${queryString}`, config);
    console.log("response", response);
    dispatch({
      type: GET_VENDOR_DETAILS_SUCCESS,
      payload: response.data.data[0]
    });

    return response.data.data[0];
  } catch (error) {
    dispatch({
      type: GET_VENDOR_DETAILS_FAIL,
      payload: error.response.data
    });
  }
};
jaymehta committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

export const getAllVendors = () => async dispatch => {
  const session = await getSession();
  try {
    dispatch({
      type: GET_ALL_VENDORS_REQUEST
    });

    // console.log("here 1", session);
    const config = {
      headers: {
        "Content-Type": "Application/json",
        Authorization: `Bearer ${session.jwt}`
      }
    };
    // console.log("here 2");
    if (!session) {
      return;
    }
    const query = {
      // filters: {
      //   user: {
      //     id: {
      //       $eq: session.id
      //     }
      //   }
      // },
jaymehta committed
212 213
      populate: ["user", "experiences"],
      sort: ["createdAt:desc"]
jaymehta committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    };
    // console.log("here 3", query);

    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });
    console.log("session", session, session.id);
    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/?${queryString}`, config);
    console.log("response", response);
    dispatch({
      type: GET_ALL_VENDORS_SUCCESS,
      payload: response.data.data
    });

    return response.data.data[0];
  } catch (error) {
    dispatch({
      type: GET_ALL_VENDORS_FAIL,
      payload: error.response.data
    });
  }
};