From a385f571afc4f871efa02a8f62414073d84a4d0b Mon Sep 17 00:00:00 2001 From: Palak Date: Thu, 10 Aug 2023 14:22:33 +0530 Subject: [PATCH] Feature added to accept follow request --- src/components/Notification/index.jsx | 53 ++++++++++++++++++++------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/components/Notification/index.jsx b/src/components/Notification/index.jsx index 3f3ff2a4a..bf100541a 100644 --- a/src/components/Notification/index.jsx +++ b/src/components/Notification/index.jsx @@ -1,8 +1,9 @@ import "./index.css"; -import { Loader, ShareModal } from "../../reusableComponents"; +import { Loader } from "../../reusableComponents"; import React, { useEffect, useState } from "react"; import { auth, db } from "../../lib/firebase"; +import firebase from "firebase/compat/app"; import { Box } from "@mui/material"; import { Link } from "react-router-dom"; @@ -10,9 +11,6 @@ import { SideBar } from "../index"; import { useSnackbar } from "notistack"; function Notifications() { - const [openShareModal, setOpenShareModal] = useState(false); - const [currentPostLink, setCurrentPostLink] = useState(""); - const [postText, setPostText] = useState(""); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); @@ -62,7 +60,7 @@ function Notifications() { return () => unsubscribe(); }, []); - function handleDeclineRequest(currentUserUid, targetUserUid) { + async function handleRemoveNotifiction(currentUserUid, targetUserUid, accept) { const batch = db.batch(); const friendRequestRef = db .collection("users") @@ -84,7 +82,7 @@ function Notifications() { batch .commit() .then(() => { - enqueueSnackbar("Friend Request Declined", { + enqueueSnackbar(`Friend Request ${accept ? "Accepted" : "Declined"}!`, { variant: "success", }); }) @@ -95,6 +93,31 @@ function Notifications() { }); } + async function handleAcceptRequest(currentUserUid, targetUserUid) { + const batch = db.batch(); + + const currentUserRef = db.collection("users").doc(currentUserUid); + const targetUserRef = db.collection("users").doc(targetUserUid); + + batch.update(currentUserRef, { + Friends: firebase.firestore.FieldValue.arrayUnion(targetUserUid), + }); + + batch.update(targetUserRef, { + Friends: firebase.firestore.FieldValue.arrayUnion(currentUserUid), + }); + + await handleRemoveNotifiction(currentUserUid, targetUserUid, true); + await batch + .commit() + .catch((error) => { + enqueueSnackbar(`Error Occurred: ${error}`, { + variant: "error", + }); + }); + } + + return ( <> @@ -104,12 +127,6 @@ function Notifications() { ) : (
-
-