Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/message/103: added an error message if user is not connected … #118

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions frontend/src/components/ReservationButton.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Button, message } from "antd";
import { Article } from "../interface/types";
import { useHandleReservationMutation } from "../generated/graphql-types";
import { useContext, useEffect } from "react";
import { UserContext } from "./Layout";

export default function ReservationButton({
articles,
}: {
articles: Article[];
}) {
const userInfo = useContext(UserContext);
const [handleReservation] = useHandleReservationMutation({
onCompleted(data) {
message.success("Réservation effectuée avec succès");
Expand All @@ -22,13 +25,17 @@ export default function ReservationButton({
message.error("Erreur lors de la réservation");
},
});

const onClick = () => {
const startDateString = localStorage.getItem("startDate");
const startDate = startDateString ? new Date(startDateString) : null;
const endDateString = localStorage.getItem("endDate");
const endDate = endDateString ? new Date(endDateString) : null;

if (userInfo.isLoggedIn === false) {
message.error("Veuillez vous connecter pour pouvoir réserver un article");
}

if (!startDate || !endDate) {
message.error("Choisissez une période de location");
return;
Expand All @@ -38,12 +45,11 @@ export default function ReservationButton({
const availableArticles = articles.filter(
(article) => article.availability === true
);
if (availableArticles.length === 0) {
if (availableArticles.length === 0 && userInfo.isLoggedIn === true) {
message.error("Cet article n'est pas disponible");
return;
}


// TO BE DISCUSSED : comment choisir l'article si plusieurs sont dispo ?
const firstAvailableArticleId = availableArticles[0].id;

Expand Down
Loading