From fb6831d44f6ee3314cc4fb96d021dbba7b9fee89 Mon Sep 17 00:00:00 2001 From: Ebube Date: Mon, 21 Aug 2023 23:28:32 +0100 Subject: [PATCH] An Optimized version --- src/components/AddJob/AddJob.jsx | 70 +++++++++++++++++--------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/src/components/AddJob/AddJob.jsx b/src/components/AddJob/AddJob.jsx index ee80b26..81d29fa 100644 --- a/src/components/AddJob/AddJob.jsx +++ b/src/components/AddJob/AddJob.jsx @@ -90,14 +90,15 @@ function AddJob({ popUpHandler, categories }) { }); }; - const getWalletDetail = (currency) => { - let { result_list } = walletDetails; - return result_list?.filter((item) => item.country === currency); + const getWalletDetail = (country) => { + const walletChecker = walletDetails.result_list.find( + (item) => item.country === country + ); + return walletChecker ? walletChecker.amount : 0; }; - // FUNCTION TO HANDLE ADD JOB FORM - const handleAddJob = (values, helpers) => { - let reqData = { + const handleAddJob = async (values, helpers) => { + const reqData = { country: values?.country, price: Number(values.price) * 100, title: values?.title, @@ -107,30 +108,34 @@ function AddJob({ popUpHandler, categories }) { category: values.category?.join("@"), }; - let walletChecker = getWalletDetail(reqData.country); - if (reqData.price > walletChecker[0]?.amount) { + 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, @@ -140,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(() => {