Skip to content

Commit

Permalink
Derrick catherine/send data on request refund (#295)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* feat: Added functionality to request refund so it calls a dummy endpoint

Co-authored-by: Catherine Xie <[email protected]>

* feat: Added loading for requestRefund button

* fix: lint

* fix: margin adjustment

* chore: lint

---------

Co-authored-by: Catherine Xie <[email protected]>
Co-authored-by: Brayden Royston <[email protected]>
Co-authored-by: BraydenRoyston <[email protected]>
  • Loading branch information
4 people authored Aug 30, 2023
1 parent fa85a6c commit 1eb484a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
1 change: 1 addition & 0 deletions backend/typescript/rest/camperRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { sendResponseByMimeType } from "../utilities/responseUtil";
import {
CamperDTO,
CreateWaitlistedCamperDTO,
RefundDTO,
WaitlistedCamperDTO,
} from "../types";
import { createWaitlistedCampersDtoValidator } from "../middlewares/validators/waitlistedCampersValidators";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ class CamperService implements ICamperService {
async cancelRegistration(
chargeId: string,
camperIds: string[],
): Promise<void> {
): Promise<RefundDTO> {
try {
const campersWithChargeId: Array<Camper> = await MgCamper.find({
chargeId,
Expand Down
25 changes: 25 additions & 0 deletions frontend/src/APIClients/CamperAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,30 @@ const getRefundInfo = async (refundCode: string): Promise<RefundDTO[]> => {
}
};

const sendSelectedRefundInfo = async (
selectedRefunds: Array<RefundDTO>,
): Promise<RefundDTO[]> => {
try {
const { chargeId } = selectedRefunds[0].instances[0];
const camperIds: Array<string> = 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<number> => {
try {
const { data } = await baseAPIClient.get(
Expand Down Expand Up @@ -194,5 +218,6 @@ export default {
waitlistCampers,
confirmPayment,
getRefundInfo,
sendSelectedRefundInfo,
getRefundDiscountInfo,
};
Original file line number Diff line number Diff line change
@@ -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<RefundDTO>;
checkedRefunds: Array<boolean>;
refundCode: string;
setRefunds: React.Dispatch<React.SetStateAction<RefundDTO[]>>;
isDisabled: boolean;
};

const CamperRefundFooter = ({
refunds,
checkedRefunds,
refundCode,
setRefunds,
isDisabled,
}: CamperRefundFooterProps): React.ReactElement => {
const [isLoading, setIsLoading] = useState<boolean>(false);
const toast = useToast();
const sendData = async (code: string) => {
const selectedRefunds: Array<RefundDTO> = [];
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 (
<Flex
color="#FFFFFF"
Expand All @@ -30,6 +67,8 @@ const CamperRefundFooter = ({
variant="primary"
background="primary.green.100"
textStyle="buttonSemiBold"
onClick={() => sendData(refundCode)}
isLoading={isLoading}
disabled={isDisabled}
py="12px"
px="25px"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const CamperRefundInfoCard = ({
<Checkbox
isDisabled={!valid}
defaultChecked={valid}
onChange={() => handleCheckboxChange(camperNum - 1)}
onChange={() => handleCheckboxChange(camperNum)}
colorScheme="green"
size="lg"
>
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/components/pages/CamperRefundCancellation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const CamperRefundCancellation = (): React.ReactElement => {
}
return (
<>
<Box pb="7%">
<Box pb="156px">
<Image
src={FONIcon}
alt="FON icon"
Expand Down Expand Up @@ -267,7 +267,13 @@ const CamperRefundCancellation = (): React.ReactElement => {
)}
</Box>
</Box>
<CamperRefundFooter isDisabled={isFooterButtonDisabled()} />
<CamperRefundFooter
refunds={refunds}
checkedRefunds={checkedRefunds}
refundCode={refundCode}
setRefunds={setRefunds}
isDisabled={isFooterButtonDisabled()}
/>
</>
);
};
Expand Down

0 comments on commit 1eb484a

Please sign in to comment.