diff --git a/app/restraunt/AddMenu/page.js b/app/restraunt/AddMenu/page.js index 671714e..ba516d4 100644 --- a/app/restraunt/AddMenu/page.js +++ b/app/restraunt/AddMenu/page.js @@ -36,7 +36,7 @@ const Page = () => { ); return; } - await addRestaurantMenu(user, name, price, description, image[0]).then(() => { + await addRestaurantMenu(user, name, price, description, image[0],user).then(() => { router.push("/restraunt/home"); } ); diff --git a/app/restraunt/home/page.js b/app/restraunt/home/page.js index 38dc75e..f9c6346 100644 --- a/app/restraunt/home/page.js +++ b/app/restraunt/home/page.js @@ -4,6 +4,8 @@ import { useTable, useGlobalFilter } from "react-table"; import Image from "next/image"; import { MoreHorizontal } from "lucide-react"; import { useUserAuth } from "@/services/utils"; +import { db } from "@/app/firebase/config"; +import { deleteDoc, doc } from "firebase/firestore"; import { getMenuInformation, getRestaurantInformationByUser, @@ -39,17 +41,14 @@ import Link from "next/link"; export default function SettingsRestaurant() { const { user } = useUserAuth(); - const [restaurantData, setRestaurantData] = useState([]); const [menuData, setMenuData] = useState(null); const route = useRouter(); const [selectedProduct, setSelectedProduct] = useState(null); const [globalFilter, setGlobalFilter] = useState(""); useEffect(() => { - console.log("user: ", user); async function gettingRestaurantMenu() { const data = await getMenuInformation(user); - console.log("data: ", data); if (data.length <= 0) { } setMenuData(data); @@ -72,13 +71,14 @@ export default function SettingsRestaurant() { setSelectedProduct(product); }; - const handleProductDelete = (productId) => { - setMenuData(menuData.filter((product) => product.id !== productId)); + const handleProductDelete = async (productId) => { + await deleteDoc(doc(db, "restaurant_menu", productId)).then(() => { + setMenuData(menuData.filter((product) => product.id !== productId)); + }); }; const columns = React.useMemo( () => [ - /* { Header: "Image", accessor: "imageUrl", @@ -87,25 +87,25 @@ export default function SettingsRestaurant() { alt={`Product image - ${original.name}`} className="aspect-square rounded-md object-cover" height="64" - src={"url(/assets/images/restCover.jpg)"} - width="64" - /> - ), - }, - */ - { - Header: 'Image', - accessor: 'imageUrl', - Cell: ({ row: { original } }) => ( - {`Product ), }, + + // { + // Header: 'Image', + // accessor: 'imageUrl', + // Cell: ({ row: { original } }) => ( + // {`Product + // ), + // }, { Header: "Name", accessor: "name", @@ -145,13 +145,6 @@ export default function SettingsRestaurant() { }, className: "hidden md:table-cell", }, - - // { - // Header: "Total Sales", - // accessor: "totalSales", - // Cell: () => "0", - // className: "hidden md:table-cell", - // }, { Header: "Actions", accessor: "actions", diff --git a/app/restraunt/setting/page.js b/app/restraunt/setting/page.js index 070f567..04e03d6 100644 --- a/app/restraunt/setting/page.js +++ b/app/restraunt/setting/page.js @@ -1,7 +1,6 @@ "use client"; import { useState, useEffect } from "react"; import { auth } from "@/app/firebase/config"; -import { deleteUser } from "firebase/auth"; import { useUserAuth } from "@/services/utils"; import { useRouter } from "next/navigation"; import { getRestaurantInformationByUser } from "@/services/GetRequest/getRequest"; @@ -33,9 +32,7 @@ import { Dialog, DialogContent, DialogFooter, - DialogClose, } from "@/components/ui/dialog"; -import { useActionData } from "react-router-dom"; export default function Component() { const { user } = useUserAuth(); @@ -44,15 +41,9 @@ export default function Component() { const [address, setAddress] = useState(""); const [id, setId] = useState(''); const [email, setEmail] = useState(''); - const [showCurrentPassword, setShowCurrentPassword] = useState(false); - const [showNewPassword, setShowNewPassword] = useState(false); - const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [showPasswordResetModal, setShowPasswordResetModal] = useState(false); const [currentUser, setCurrentUser] = useState(null); const route = useRouter(); - const handlePasswordReset = () => { - setShowPasswordResetModal(true); - }; useEffect(() => { async function gettingUserInformation() { const data = await getRestaurantInformationByUser(user); @@ -61,7 +52,6 @@ export default function Component() { if (user) { setCurrentUser(auth.currentUser); gettingUserInformation(); - console.log(auth.currentUser); } if (user == false) { route.push("/"); @@ -71,7 +61,6 @@ export default function Component() { useEffect(() => { if (userData) { userData.map((information)=>{ - console.log("information: ",typeof information.id) setName(information.name); setId(information.id); setAddress(information.address); @@ -86,7 +75,7 @@ export default function Component() { async function deleteCurrentUser() { - await deleteRestaurantUser(currentUser,id).then(()=>alert("Your account has been deleted successfully!")) + await deleteRestaurantUser(currentUser,id,user); } async function passwordChange(){ diff --git a/app/sait-staff/RDashboard/Edit.js b/app/sait-staff/RDashboard/Edit.js index 5bd59ed..c7b5bf9 100644 --- a/app/sait-staff/RDashboard/Edit.js +++ b/app/sait-staff/RDashboard/Edit.js @@ -71,10 +71,9 @@ const Edit = ({ restaurants, selectedRestaurant, setRestaurants, setIsEditing, g setEmail(e.target.value)} - className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" + // onChange={(e) => setEmail(e.target.value)} + className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none sm:text-sm cursor-not-allowed bg-black/20" />
diff --git a/app/sait-staff/SDashboard/Edit.js b/app/sait-staff/SDashboard/Edit.js index 3380999..b7cc568 100644 --- a/app/sait-staff/SDashboard/Edit.js +++ b/app/sait-staff/SDashboard/Edit.js @@ -99,8 +99,8 @@ const Edit = ({ selectedStudent, setIsEditing }) => { id="email" type="email" value={email} - onChange={(e) => setEmail(e.target.value)} - className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" + // onChange={(e) => setEmail(e.target.value)} + className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none sm:text-sm cursor-not-allowed bg-black/20" />
diff --git a/app/sait-staff/overviewDash/overview.js b/app/sait-staff/overviewDash/overview.js index bef58ad..1af8904 100644 --- a/app/sait-staff/overviewDash/overview.js +++ b/app/sait-staff/overviewDash/overview.js @@ -5,7 +5,7 @@ import { getAllStudentsInformation, getRestaurantInformation } from "@/services/ import { FaUsers, FaChartLine } from "react-icons/fa"; import { AiOutlineShop, AiOutlineTeam } from "react-icons/ai"; -import {useSpring, animated} from "react-spring"; +import { useSpring, animated } from "react-spring"; function NumberChange({n}){ const {number} = useSpring({ diff --git a/merge from master b/merge from master new file mode 100644 index 0000000..e636e0a --- /dev/null +++ b/merge from master @@ -0,0 +1,6 @@ +Merge remote-tracking branch 'origin/master' into hunar +# Please enter a commit message to explain why this merge is necessary, +# especially if it merges an updated upstream into a topic branch. +# +# Lines starting with '#' will be ignored, and an empty message aborts +# the commit. diff --git a/services/GetRequest/getRequest.js b/services/GetRequest/getRequest.js index 0010f52..02bac4d 100644 --- a/services/GetRequest/getRequest.js +++ b/services/GetRequest/getRequest.js @@ -65,7 +65,6 @@ export async function getRestaurantInformationByUser(user) { return false; } } - export async function getMenuInformation(userId) { try { const q = query( @@ -79,7 +78,6 @@ export async function getMenuInformation(userId) { }); return userItems; } catch (error) { - console.error("Error getting user information: ", error); return []; } } diff --git a/services/PostRequest/postRequest.js b/services/PostRequest/postRequest.js index eb93e91..bc4fb31 100644 --- a/services/PostRequest/postRequest.js +++ b/services/PostRequest/postRequest.js @@ -9,7 +9,14 @@ import { deleteDoc, updateDoc, } from "firebase/firestore"; -import { ref, uploadBytesResumable, getDownloadURL } from "firebase/storage"; +import { + ref, + uploadBytesResumable, + getDownloadURL, + getStorage, + listAll, + deleteObject, +} from "firebase/storage"; import { deleteUser } from "firebase/auth"; export async function addStudentInformation(userId, userInformation, email) { @@ -76,8 +83,8 @@ export async function addRestaurantInformation( ); } -export async function addRestaurantMenu(user, name, price, description, image) { - const storageRef = ref(storage, `menu/${image.name}`); +export async function addRestaurantMenu(user, name, price, description, image,userId) { + const storageRef = ref(storage, `menu/${userId}/${image.name}`); const uploadTask = uploadBytesResumable(storageRef, image); uploadTask.on( "state_changed", @@ -107,7 +114,7 @@ export async function addRestaurantMenu(user, name, price, description, image) { name, price, description, - status: 'Available', + status: "Available", imageUrl: downloadURL, createdAt: new Date(), }); @@ -181,14 +188,16 @@ export async function deleteRestaurantData(id) { console.error("Error updating document: ", error); } } -export async function updateRestaurantData(id,name,address) { +export async function updateRestaurantData(id, name, address) { try { const docRef = doc(db, "restaurants", id); await updateDoc(docRef, { - name:name, - address:address + name: name, + address: address, }); - console.log(`document has been updated where name: ${name}, id: ${id} and address is ${address}`) + console.log( + `document has been updated where name: ${name}, id: ${id} and address is ${address}` + ); } catch (error) { console.error("Error updating document: ", error); } @@ -236,18 +245,65 @@ export async function existingRestaurantData(email) { console.error("Error updating document: ", error); } } -export async function deleteRestaurantUser(currentUser, id){ +// to delete restarant data from database, storage and athentication +export async function deleteRestaurantUser(currentUser, id, userId) { + const accountId = userId; if (currentUser) { - console.log("currentUser: ", currentUser); try { - console.log(id) - await deleteDoc(doc(db,"restaurants",id)) - .then(async()=>{ + // Step 1: Re-authenticate user if necessary + try { await deleteUser(currentUser); - console.log("User delete successfully"); - }) + } catch (error) { + if (error.code === 'auth/requires-recent-login') { + const credential = EmailAuthProvider.credential( + currentUser.email, + prompt("Please enter your password to re-authenticate.") + ); + await reauthenticateWithCredential(currentUser, credential); + await deleteUser(currentUser); + } else { + throw error; + } + } + + // Step 2: Delete user document from Firestore + await deleteDoc(doc(db, "restaurants", id)); + + // Step 3: Delete user's data from restaurant_menu database + const userCollectionRef = collection(db, "restaurant_menu"); + const q = query(userCollectionRef, where("userId", "==", accountId)); + const querySnapshot = await getDocs(q); + + querySnapshot.forEach(async (docSnapshot) => { + await deleteDoc(docSnapshot.ref); + }); + + // Step 4: Delete user's storage files + const storage = getStorage(); + const folderRef = ref(storage, `menu/${accountId}`); + + const deleteFolder = async (folderRef) => { + const res = await listAll(folderRef); + for (const itemRef of res.items) { + await deleteObject(itemRef); + } + + for (const subfolderRef of res.prefixes) { + await deleteFolder(subfolderRef); + } + }; + + await deleteFolder(folderRef); + alert("Your account has been deleted successfully!"); + } catch (error) { - console.error("error while deleting user: ", error); + if (error.code === 'auth/requires-recent-login') { + alert("To delete your account, please log out first and then proceed with the account deletion."); + } else { + console.error('Error while deleting user:', error); + } } + } else { + console.log("No current user found"); } -} \ No newline at end of file +}