From 1eb484af5f87353956b9ec1845ff34900cdbb159 Mon Sep 17 00:00:00 2001 From: Derrick Yang <57463206+derrick-yyang@users.noreply.github.com> Date: Wed, 30 Aug 2023 08:32:40 -0400 Subject: [PATCH] Derrick catherine/send data on request refund (#295) * fix: add empty dep array so useEffect is only called once * Added functionality to request refund so it calls a dummy endpoint Co-authored-by: Catherine Xie * feat: Added functionality to request refund so it calls a dummy endpoint Co-authored-by: Catherine Xie * feat: Added loading for requestRefund button * fix: lint * fix: margin adjustment * chore: lint --------- Co-authored-by: Catherine Xie Co-authored-by: Brayden Royston Co-authored-by: BraydenRoyston --- backend/typescript/rest/camperRoutes.ts | 1 + .../services/implementations/camperService.ts | 2 +- frontend/src/APIClients/CamperAPIClient.ts | 25 +++++++++++ .../CamperRefundFooter.tsx | 43 ++++++++++++++++++- .../CamperRefundInfoCard.tsx | 2 +- .../pages/CamperRefundCancellation/index.tsx | 10 ++++- 6 files changed, 77 insertions(+), 6 deletions(-) diff --git a/backend/typescript/rest/camperRoutes.ts b/backend/typescript/rest/camperRoutes.ts index ffb26d49..8bd9fd56 100644 --- a/backend/typescript/rest/camperRoutes.ts +++ b/backend/typescript/rest/camperRoutes.ts @@ -15,6 +15,7 @@ import { sendResponseByMimeType } from "../utilities/responseUtil"; import { CamperDTO, CreateWaitlistedCamperDTO, + RefundDTO, WaitlistedCamperDTO, } from "../types"; import { createWaitlistedCampersDtoValidator } from "../middlewares/validators/waitlistedCampersValidators"; diff --git a/backend/typescript/services/implementations/camperService.ts b/backend/typescript/services/implementations/camperService.ts index fb8885aa..05a8961c 100644 --- a/backend/typescript/services/implementations/camperService.ts +++ b/backend/typescript/services/implementations/camperService.ts @@ -943,7 +943,7 @@ class CamperService implements ICamperService { async cancelRegistration( chargeId: string, camperIds: string[], - ): Promise { + ): Promise { try { const campersWithChargeId: Array = await MgCamper.find({ chargeId, diff --git a/frontend/src/APIClients/CamperAPIClient.ts b/frontend/src/APIClients/CamperAPIClient.ts index 9c34f1df..c0773ce2 100644 --- a/frontend/src/APIClients/CamperAPIClient.ts +++ b/frontend/src/APIClients/CamperAPIClient.ts @@ -154,6 +154,30 @@ const getRefundInfo = async (refundCode: string): Promise => { } }; +const sendSelectedRefundInfo = async ( + selectedRefunds: Array, +): Promise => { + try { + const { chargeId } = selectedRefunds[0].instances[0]; + const camperIds: Array = selectedRefunds.map((refund) => { + return refund.instances[0].id; + }); + + const body = { chargeId, camperIds }; + const { data } = await baseAPIClient.patch( + `/campers/cancel-registration`, + body, + { + headers: { Authorization: getBearerToken() }, + }, + ); + + return data; + } catch (error) { + return error as RefundDTO[]; + } +}; + const getRefundDiscountInfo = async (chargeId: string): Promise => { try { const { data } = await baseAPIClient.get( @@ -194,5 +218,6 @@ export default { waitlistCampers, confirmPayment, getRefundInfo, + sendSelectedRefundInfo, getRefundDiscountInfo, }; diff --git a/frontend/src/components/pages/CamperRefundCancellation/CamperRefundFooter.tsx b/frontend/src/components/pages/CamperRefundCancellation/CamperRefundFooter.tsx index 9a8d9ac5..ce4b7e85 100644 --- a/frontend/src/components/pages/CamperRefundCancellation/CamperRefundFooter.tsx +++ b/frontend/src/components/pages/CamperRefundCancellation/CamperRefundFooter.tsx @@ -1,13 +1,50 @@ -import React from "react"; -import { Flex, Button } from "@chakra-ui/react"; +import React, { useState } from "react"; +import { Flex, Button, useToast } from "@chakra-ui/react"; +import { RefundDTO } from "../../../types/CamperTypes"; +import CamperAPIClient from "../../../APIClients/CamperAPIClient"; type CamperRefundFooterProps = { + refunds: Array; + checkedRefunds: Array; + refundCode: string; + setRefunds: React.Dispatch>; isDisabled: boolean; }; const CamperRefundFooter = ({ + refunds, + checkedRefunds, + refundCode, + setRefunds, isDisabled, }: CamperRefundFooterProps): React.ReactElement => { + const [isLoading, setIsLoading] = useState(false); + const toast = useToast(); + const sendData = async (code: string) => { + const selectedRefunds: Array = []; + checkedRefunds.forEach((checked, i) => { + if (checked) { + selectedRefunds.push(refunds[i]); + } + }); + try { + setIsLoading(true); + const response = await CamperAPIClient.sendSelectedRefundInfo( + selectedRefunds, + ); + setRefunds(refunds); // TODO: change this to response once the endpoint is implemented + } catch { + toast({ + description: `Unable to process selected refunds.`, + status: "error", + duration: 3000, + variant: "subtle", + }); + } finally { + setIsLoading(false); + } + } + return ( sendData(refundCode)} + isLoading={isLoading} disabled={isDisabled} py="12px" px="25px" diff --git a/frontend/src/components/pages/CamperRefundCancellation/CamperRefundInfoCard.tsx b/frontend/src/components/pages/CamperRefundCancellation/CamperRefundInfoCard.tsx index 12e2b17e..0fe87c02 100644 --- a/frontend/src/components/pages/CamperRefundCancellation/CamperRefundInfoCard.tsx +++ b/frontend/src/components/pages/CamperRefundCancellation/CamperRefundInfoCard.tsx @@ -146,7 +146,7 @@ const CamperRefundInfoCard = ({ handleCheckboxChange(camperNum - 1)} + onChange={() => handleCheckboxChange(camperNum)} colorScheme="green" size="lg" > diff --git a/frontend/src/components/pages/CamperRefundCancellation/index.tsx b/frontend/src/components/pages/CamperRefundCancellation/index.tsx index 497862ed..f676f061 100644 --- a/frontend/src/components/pages/CamperRefundCancellation/index.tsx +++ b/frontend/src/components/pages/CamperRefundCancellation/index.tsx @@ -123,7 +123,7 @@ const CamperRefundCancellation = (): React.ReactElement => { } return ( <> - + FON icon { )} - + ); };