Skip to content

Commit

Permalink
Merge pull request #414 from NIAEFEUP/feature/allow-user-cancel-excha…
Browse files Browse the repository at this point in the history
…nges-they-sent

feat: cancel exchange request
  • Loading branch information
tomaspalma authored Feb 3, 2025
2 parents 9e76a45 + 654ad13 commit 5cb23e5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export type MarketplaceRequest = {
classes?: Array<ClassInfo>,
pending_motive?: DirectExchangePendingMotive,
accepted: boolean,
canceled: boolean
}

export type DirectExchangeRequest = {
Expand Down
13 changes: 12 additions & 1 deletion src/api/services/exchangeRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ const acceptDirectExchangeRequest = async (id: number) => {
});
}

const cancelMarketplaceRequest = async (id: number) => {
return fetch(`${api.BACKEND_URL}/exchange/marketplace/${id}/cancel/`, {
method: "PUT",
credentials: "include",
headers: {
"X-CSRFToken": api.getCSRFToken(),
},
});
}

const exchangeRequestService = {
submitExchangeRequest,
retrieveMarketplaceRequest,
Expand All @@ -128,7 +138,8 @@ const exchangeRequestService = {
adminAcceptExchangeRequest,
adminMarkRequestAsAwaitingInformation,
verifyExchangeRequest,
acceptDirectExchangeRequest
acceptDirectExchangeRequest,
cancelMarketplaceRequest
}

export default exchangeRequestService;
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const CommonCardHeader = ({
useEffect(() => {
if (!showRequestStatus) return;

if ((request as DirectExchangeRequest).canceled) {
if (request.canceled) {
setRequestStatus(StudentRequestCardStatus.CANCELED);
return;
}
Expand Down
38 changes: 23 additions & 15 deletions src/components/exchange/requests/view/cards/MineRequestCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ import SessionContext from "../../../../../contexts/SessionContext";
import { Card, CardContent, CardFooter } from "../../../../ui/card"
import { CommonCardHeader } from "./CommonCardHeader";
import { ListRequestChanges } from "./ListRequestChanges";
import { Button } from "../../../../ui/button";
import useCancelMarketplaceExchange from "../../../../../hooks/exchange/useCancelMarketplaceExchange";
import { MoonLoader } from "react-spinners";
import { StudentRequestCardStatus } from "../../../../../utils/requests";

type Props = {
request: MarketplaceRequest
}

export const MineRequestCard = ({ request }: Props) => {
const { open, setOpen, selectedOptions, setSelectedOptions, setSelectAll, togglePreview } = useContext(ExchangeRequestCommonContext);
const {
open, setOpen, selectedOptions, setSelectedOptions, setSelectAll, togglePreview,
setRequestStatus
} = useContext(ExchangeRequestCommonContext);
const [hovered, setHovered] = useState<boolean>(false);

const { trigger: cancelMarketplaceExchange, isMutating: cancelingMarketplaceExchange } = useCancelMarketplaceExchange(request.id);

const { user } = useContext(SessionContext);

return <Card
Expand Down Expand Up @@ -49,20 +58,19 @@ export const MineRequestCard = ({ request }: Props) => {
)}
</CardContent>
<CardFooter className={open ? "" : "hidden"}>
{/* <div className="flex flex-row justify-between w-full items-center"> */}
{/* <form className="flex flex-row gap-2"> */}
{/* {!request.accepted && request.pending_motive === DirectExchangePendingMotive.USER_DID_NOT_ACCEPT && */}
{/* <Button */}
{/* type="submit" */}
{/* className="success-button hover:bg-white" */}
{/* > */}
{/* Aceitar */}
{/* </Button> */}
{/* } */}
{/* </form> */}
{/* </div> */}

{(!request.canceled || !request.accepted) && <Button
variant="destructive"
onClick={async () => {
await cancelMarketplaceExchange();
setRequestStatus(StudentRequestCardStatus.CANCELED)
}}
>
{cancelingMarketplaceExchange
? <MoonLoader size={20} />
: "Cancelar"
}
</Button>
}
</CardFooter>

</Card>
}
22 changes: 22 additions & 0 deletions src/hooks/exchange/useCancelMarketplaceExchange.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import useSWRMutation from "swr/mutation";
import exchangeRequestService from "../../api/services/exchangeRequestService";

export default (id: number) => {
const submit = async () => {
return await exchangeRequestService.cancelMarketplaceRequest(id);
}

const { error, trigger, isMutating } = useSWRMutation(
`${id}`,
submit
);

return {
error,
trigger,
isMutating
};
};



0 comments on commit 5cb23e5

Please sign in to comment.