diff --git a/src/components/AddJob/AddJob.jsx b/src/components/AddJob/AddJob.jsx index 904f8ed..81d29fa 100644 --- a/src/components/AddJob/AddJob.jsx +++ b/src/components/AddJob/AddJob.jsx @@ -1,6 +1,6 @@ import { Field, Form, Formik } from "formik"; import React, { useEffect, useState } from "react"; -import { useDispatch } from "react-redux"; +import { useDispatch, useSelector } from "react-redux"; import * as Yup from "yup"; import usersService from "../../services/UsersService"; import { tableReload } from "../../store/TableReloads"; @@ -43,7 +43,7 @@ const validationSchema = Yup.object().shape({ function AddJob({ popUpHandler, categories }) { const ApiCall = new usersService(); - + const { walletDetails } = useSelector((state) => state.walletDetails); let dispatch = useDispatch(); let [currency, setCurrency] = useState({ @@ -90,9 +90,15 @@ function AddJob({ popUpHandler, categories }) { }); }; - // FUNCTION TO HANDLE ADD JOB FORM - const handleAddJob = (values, helpers) => { - let reqData = { + const getWalletDetail = (country) => { + const walletChecker = walletDetails.result_list.find( + (item) => item.country === country + ); + return walletChecker ? walletChecker.amount : 0; + }; + + const handleAddJob = async (values, helpers) => { + const reqData = { country: values?.country, price: Number(values.price) * 100, title: values?.title, @@ -102,20 +108,34 @@ function AddJob({ popUpHandler, categories }) { category: values.category?.join("@"), }; + const walletAmount = getWalletDetail(reqData.country); + if (reqData.price > walletAmount) { + setRequestStatus({ + loading: false, + status: false, + message: "Insufficient Balance", + }); + + setTimeout(() => { + setRequestStatus({ loading: false, status: false, message: "" }); + }, 1500); + return; + } + setRequestStatus({ loading: true, status: false, message: "" }); - ApiCall.jobManagerCreateJob(reqData) - .then((res) => { - if (res.data.internal_return < 1) { - setRequestStatus({ - loading: false, - status: false, - message: "Could not complete your request at the moment", - }); - setTimeout(() => { - popUpHandler(); - }, 1500); - return; - } + + try { + const res = await ApiCall.jobManagerCreateJob(reqData); + if (res.data.internal_return < 1) { + setRequestStatus({ + loading: false, + status: false, + message: "Could not complete your request at the moment", + }); + setTimeout(() => { + popUpHandler(); + }, 1500); + } else { setRequestStatus({ loading: false, status: true, @@ -125,19 +145,18 @@ function AddJob({ popUpHandler, categories }) { dispatch(tableReload({ type: "JOBTABLE" })); popUpHandler(); }, 1000); - }) - .catch((err) => { - setRequestStatus({ - loading: false, - status: false, - message: "Opps! something went wrong. Try Again", - }); - }) - .finally(() => { - setTimeout(() => { - setRequestStatus({ loading: false, status: false, message: "" }); - }, 5000); + } + } catch (err) { + setRequestStatus({ + loading: false, + status: false, + message: "Oops! Something went wrong. Try Again", }); + } finally { + setTimeout(() => { + setRequestStatus({ loading: false, status: false, message: "" }); + }, 5000); + } }; useEffect(() => { @@ -164,7 +183,11 @@ function AddJob({ popUpHandler, categories }) { className="input-label text-[#181c32] dark:text-white text-[13.975px] leading-[20.9625px] font-semibold flex item-center gap-1" > Currency - {props.errors.country && props.touched.country && {props.errors.country}} + {props.errors.country && props.touched.country && ( + + {props.errors.country} + + )}