Skip to content

Commit

Permalink
Fix: Enable Remove button functionality in Add Insurance section
Browse files Browse the repository at this point in the history
  • Loading branch information
chakribontha committed Sep 29, 2024
1 parent 5534762 commit dd9494c
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/Components/HCX/InsuranceDetailsBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
};

Expand Down

0 comments on commit dd9494c

Please sign in to comment.