-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from WildCodeSchool/56/delete-article
56/delete article
- Loading branch information
Showing
11 changed files
with
362 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Card, Divider } from "antd"; | ||
import { ReservationData } from "../interface/types"; | ||
import ValidateReservationButton from "./ValidateReservationButton"; | ||
import CancelReservationButton from "./CancelReservationButton"; | ||
import DeleteArticleReservationButton from "./DeleteArticleReservationButton"; | ||
|
||
export const ArticleReservationCard = ({ | ||
reservationData, | ||
}: { | ||
reservationData: ReservationData; | ||
}) => { | ||
const articles = reservationData.reservation.articles; | ||
|
||
return ( | ||
<> | ||
{reservationData.reservation.status === "pending" ? ( | ||
<Card title={`Détails de votre réservation`} style={{ width: 500 }}> | ||
{articles.map((article) => ( | ||
<Card style={{ margin: 20 }} key={article.product?.id}> | ||
<div | ||
style={{ | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "space-between", | ||
}} | ||
> | ||
<div style={{ display: "flex", alignItems: "center" }}> | ||
<img | ||
src={article.product?.imgUrl} | ||
alt="product img" | ||
style={{ height: 100, marginRight: 20 }} | ||
/> | ||
<div> | ||
<p style={{ margin: 0, fontWeight: "bold" }}> | ||
{article.product?.name} | ||
</p> | ||
<p style={{ margin: 0 }}>{article.product?.price}€</p> | ||
</div> | ||
</div> | ||
<DeleteArticleReservationButton | ||
articleId={article.id} | ||
reservationData={reservationData} | ||
/> | ||
</div> | ||
</Card> | ||
))} | ||
<div style={{ display: "flex", justifyContent: "space-around" }}> | ||
{reservationData.reservation.status === "pending" && ( | ||
<ValidateReservationButton | ||
reservation={reservationData.reservation} | ||
/> | ||
)} | ||
{reservationData.reservation.status === "pending" && ( | ||
<CancelReservationButton | ||
reservation={reservationData.reservation} | ||
/> | ||
)} | ||
</div> | ||
</Card> | ||
) : ( | ||
<h1>Aucune réservation en cours.</h1> | ||
)} | ||
|
||
<Divider dashed /> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useMutation } from "@apollo/client"; | ||
import { Button, message, Popconfirm } from "antd"; | ||
import { CANCEL_RESERVATION } from "../graphql/mutations"; | ||
import { Reservation } from "../interface/types"; | ||
import { | ||
GetCurrentReservationByUserIdDocument, | ||
GetReservationsByUserIdDocument, | ||
} from "../generated/graphql-types"; | ||
|
||
function CancelReservationButton({ reservation }: Reservation) { | ||
const [cancelReservation] = useMutation(CANCEL_RESERVATION, { | ||
onCompleted: () => { | ||
message.success("La réservation a bien été annulée."); | ||
}, | ||
onError: () => { | ||
message.error("Une erreur est survenue lors de l'annulation."); | ||
}, | ||
refetchQueries: [ | ||
GetReservationsByUserIdDocument, | ||
GetCurrentReservationByUserIdDocument, | ||
], | ||
}); | ||
|
||
return ( | ||
<> | ||
<Popconfirm | ||
title="Annuler cette réservation ?" | ||
description="La réservation sera définitivement annulée." | ||
okText="Oui" | ||
cancelText="Non" | ||
onConfirm={() => | ||
cancelReservation({ | ||
variables: { | ||
reservationId: reservation.id.toString(), | ||
}, | ||
}) | ||
} | ||
> | ||
<Button danger>Annuler la réservation</Button> | ||
</Popconfirm> | ||
</> | ||
); | ||
} | ||
|
||
export default CancelReservationButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { ReservationData } from "../interface/types"; | ||
import { ReservationCard } from "./ReservationCard"; | ||
import { ArticleReservationCard } from "./ArticleReservationCard"; | ||
|
||
export const CurrentReservation = ({ reservationData }: { reservationData: ReservationData }) => { | ||
return ( | ||
<ReservationCard reservationData={reservationData} /> | ||
); | ||
export const CurrentReservation = ({ | ||
reservationData, | ||
}: { | ||
reservationData: ReservationData; | ||
}) => { | ||
return <ArticleReservationCard reservationData={reservationData} />; | ||
}; | ||
|
||
export default CurrentReservation; |
78 changes: 78 additions & 0 deletions
78
frontend/src/components/DeleteArticleReservationButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { useMutation } from "@apollo/client"; | ||
import { | ||
GetAllArticlesDocument, | ||
GetCurrentReservationByUserIdDocument, | ||
GetReservationsByUserIdDocument, | ||
} from "../generated/graphql-types"; | ||
import { Button, message, Popconfirm } from "antd"; | ||
import { CANCEL_RESERVATION, DELETE_ARTICLE_FROM_RESERVATION } from "../graphql/mutations"; | ||
import { DeleteOutlined } from "@ant-design/icons"; | ||
import { ReservationData } from "../interface/types"; | ||
import { useEffect } from "react"; | ||
|
||
function DeleteArticleReservationButton({ | ||
reservationData, | ||
articleId, | ||
}: { | ||
reservationData: ReservationData; | ||
articleId: number; | ||
}) { | ||
const articlesNumber = reservationData.reservation.articles.length; | ||
useEffect(()=> { | ||
console.log(articlesNumber) | ||
console.log(reservationData.reservation.articles) | ||
}, [reservationData]) | ||
const [cancelReservation] = useMutation(CANCEL_RESERVATION); | ||
const [deleteArticleFromReservation] = useMutation( | ||
DELETE_ARTICLE_FROM_RESERVATION, | ||
{ | ||
onCompleted: () => { | ||
const remainingArticles = reservationData.reservation.articles.filter( | ||
(article) => article.id !== articleId | ||
).length; | ||
|
||
if (remainingArticles === 0) { | ||
cancelReservation({ | ||
variables: { | ||
reservationId: reservationData.reservation.id.toString(), | ||
}, | ||
refetchQueries: [ | ||
GetReservationsByUserIdDocument, | ||
GetCurrentReservationByUserIdDocument, | ||
], | ||
}); | ||
} | ||
|
||
message.success("L'article a bien été supprimé de la réservation."); | ||
}, | ||
onError: () => { | ||
message.error( | ||
"Une erreur est survenue lors de la suppression de l'article." | ||
); | ||
}, | ||
refetchQueries: [ | ||
GetReservationsByUserIdDocument, | ||
GetCurrentReservationByUserIdDocument, | ||
GetAllArticlesDocument, | ||
], | ||
} | ||
); | ||
|
||
return ( | ||
<Popconfirm | ||
title="Supprimer cet article ? " | ||
description="Cet article sera définitivement supprimé de la réservation." | ||
okText="Oui" | ||
cancelText="Non" | ||
onConfirm={() => | ||
deleteArticleFromReservation({ | ||
variables: { id: articleId.toString() }, | ||
}) | ||
} | ||
> | ||
<Button title="Supprimer l'article" danger icon={<DeleteOutlined />} /> | ||
</Popconfirm> | ||
); | ||
} | ||
|
||
export default DeleteArticleReservationButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.