diff --git a/app/src/features/api.tsx b/app/src/features/api.tsx index c310ef3e..e6a84af7 100644 --- a/app/src/features/api.tsx +++ b/app/src/features/api.tsx @@ -1,7 +1,7 @@ -import { collection, Timestamp, addDoc, getDocs, query, where, runTransaction, doc, orderBy, limit } from "firebase/firestore"; +import { collection, Timestamp, getDoc, getDocs, query, where, runTransaction, doc, orderBy, limit } from "firebase/firestore"; import { FirebaseDatabase } from "../firebase"; -const TransactionsCollectionName = "transactions" +const TransactionsCollectionName = "transactions-new" const DishCollectionName = "dishes" const UserCollectionName = "users" const QRCollectionName = "qr-codes" @@ -56,21 +56,23 @@ const DishAPI = { const out = await runTransaction(FirebaseDatabase, async (transaction) => { const qrDoc = await transaction.get(qrRef) if (!qrDoc.exists()) { - throw "QR code not registered"; + return null; } - const dish = qrDoc.data().dish - - const docData = { - dish: dish, - user: user, - timestamp: Timestamp.now(), + const dishRef = doc(FirebaseDatabase, "dishes", qrDoc.data().dishID); + const dishSnap = await getDoc(dishRef); + + if (dishSnap.exists()) { + const dish = dishSnap.data(); + const docData = { + dish: dish, + user: user, + timestamp: Timestamp.now(), + } + const docRef = doc(collection(FirebaseDatabase, TransactionsCollectionName)); + transaction.set(docRef, docData); + return docRef.id; } - - const docRef = doc(collection(FirebaseDatabase, TransactionsCollectionName)); - - await transaction.set(docRef, docData); - return docRef.id; }); return out; }, diff --git a/app/src/routes/borrow.tsx b/app/src/routes/borrow.tsx index 71f28632..5c113a10 100644 --- a/app/src/routes/borrow.tsx +++ b/app/src/routes/borrow.tsx @@ -3,13 +3,10 @@ import { useNavigate } from "react-router-dom"; import Scanner from "../widgets/scanner"; import Modal from "react-bootstrap/Modal"; import Button from "react-bootstrap/Button"; -import { faCoffee } from "@fortawesome/free-solid-svg-icons"; +import { faCoffee, faExclamation } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import DishAPI from "../features/api"; import { FirebaseContext } from "../firebase"; -import Login from "./login"; -import { GoogleAuth, FirebaseAuth } from "../firebase"; -import { signInWithPopup } from "firebase/auth"; const Confirm = ({ show, onSubmit, onCancel, id }) => { return ( @@ -21,8 +18,8 @@ const Confirm = ({ show, onSubmit, onCancel, id }) => { > -

ID : {id}

+

ID: {id}