From dd9494c7b99e33ebf6148c373351c05d00e0a1cb Mon Sep 17 00:00:00 2001 From: chakribontha Date: Mon, 30 Sep 2024 02:16:36 +0530 Subject: [PATCH] Fix: Enable Remove button functionality in Add Insurance section --- .../HCX/InsuranceDetailsBuilder.tsx | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Components/HCX/InsuranceDetailsBuilder.tsx b/src/Components/HCX/InsuranceDetailsBuilder.tsx index ee8816fcf4b..834e3a66953 100644 --- a/src/Components/HCX/InsuranceDetailsBuilder.tsx +++ b/src/Components/HCX/InsuranceDetailsBuilder.tsx @@ -44,17 +44,25 @@ export default function InsuranceDetailsBuilder(props: Props) { }; const handleRemove = (index: number) => { - return () => { - field.handleChange( - (props.value || [])?.filter(async (obj, i) => { - if (obj.id && i === index) { - await request(routes.hcx.policies.delete, { - pathParams: { external_id: obj.id }, - }); - } - return i !== index; - }), - ); + return async () => { + const updatedPolicies = [...(props.value || [])]; + const policyToRemove = updatedPolicies[index]; + + if (policyToRemove?.id) { + try { + await request(routes.hcx.policies.delete, { + pathParams: { external_id: policyToRemove.id }, + }); + + updatedPolicies.splice(index, 1); + field.handleChange(updatedPolicies); + } catch (error) { + console.error("Failed to delete the policy", error); + } + } else { + updatedPolicies.splice(index, 1); + field.handleChange(updatedPolicies); + } }; };