From 1230c6a2f8cb2bc1dbd2e66618262786e193507a Mon Sep 17 00:00:00 2001 From: ValeuDoamne Date: Sat, 25 May 2024 02:49:13 +0300 Subject: [PATCH] Begin adding accommodations to webapp --- src/app/add_accommodation/page.tsx | 173 +++++++++++++++++++ src/app/layout.tsx | 2 +- src/app/my_accommodations/page.tsx | 82 +++++++++ src/components/AsyncConfirmationModal.tsx | 44 +++++ src/components/ConfirmationModal.tsx | 44 +++++ src/components/Header.tsx | 5 +- src/components/usertype_check/AdminCheck.tsx | 18 ++ src/components/usertype_check/GuestCheck.tsx | 18 ++ src/components/usertype_check/HostCheck.tsx | 18 ++ 9 files changed, 401 insertions(+), 3 deletions(-) create mode 100644 src/app/add_accommodation/page.tsx create mode 100644 src/app/my_accommodations/page.tsx create mode 100644 src/components/AsyncConfirmationModal.tsx create mode 100644 src/components/ConfirmationModal.tsx create mode 100644 src/components/usertype_check/AdminCheck.tsx create mode 100644 src/components/usertype_check/GuestCheck.tsx create mode 100644 src/components/usertype_check/HostCheck.tsx diff --git a/src/app/add_accommodation/page.tsx b/src/app/add_accommodation/page.tsx new file mode 100644 index 0000000..7f8e655 --- /dev/null +++ b/src/app/add_accommodation/page.tsx @@ -0,0 +1,173 @@ +'use client'; + +import { ChangeEvent, FormEvent, useState, useContext } from "react"; +import { addDoc, collection } from 'firebase/firestore'; +import { ref, uploadBytesResumable, getDownloadURL } from 'firebase/storage'; +import { db, storage } from '@/lib/firebase'; +import toast from "react-hot-toast"; +import { UserContext } from '@/lib/context'; +import { createHash } from "crypto"; +import ConfirmationModal from "@/components/AsyncConfirmationModal"; +import HostCheck from "@/components/usertype_check/HostCheck"; + +export default function AddAccommodation() { + const [location, setLocation] = useState(''); + const [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [photos, setPhotos] = useState([]); + const [numberOfRooms, setNumberOfRooms] = useState(1); + const [price, setPrice] = useState(100); + const [showModal, setShowModal] = useState(false); + + const { user } = useContext(UserContext); + + const handlePhotoUpload = (e: ChangeEvent) => { + if (e.target.files) { + setPhotos(Array.from(e.target.files)); + } + }; + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + + // Generate a unique ID and add the location data to Firestore + try { + + let images = []; + + for(let i = 0; i < photos.length; i++) { + const buffer = await photos[i].arrayBuffer(); + const name = photos[i].name; + const hashName = createHash('sha256') + .update(new Uint8Array(buffer)) + .digest('hex'); + const extension = name.split('.').pop(); + const storageUri = `accommodations/${hashName}.${extension}`; + const storageRef = ref(storage, storageUri); + if (buffer !== undefined) { + await uploadBytesResumable(storageRef, buffer); + images.push(await getDownloadURL(storageRef)); + } + } + + await addDoc(collection(db, 'accommodations'), { + uid: user.uid, + title, + location, + description, + numberOfRooms, + price, + photos: images, // set the photos url + }); + + // Reset the form + setTitle(''); + setLocation(''); + setDescription(''); + setPhotos([]); + setNumberOfRooms(1); + setPrice(100); + setShowModal(false); + + toast.success("Successfully created accommodation!") + } catch (e) { + toast.error("Could not create accommodation, contact Admin! Error: "+e); + } + }; + + const handleFormSubmit = async (e: FormEvent) => { + e.preventDefault(); + setShowModal(true); + }; + + + return ( + +
+
+

Add accommodation

+
+
+ + setTitle(e.target.value)} + className="mt-1 p-2 border border-gray-300 rounded w-full" + required + /> +
+
+ + setLocation(e.target.value)} + className="mt-1 p-2 border border-gray-300 rounded w-full" + required + /> +
+
+ +