Skip to content

Commit

Permalink
Feature added to accept follow request
Browse files Browse the repository at this point in the history
  • Loading branch information
Palakkgoyal committed Aug 10, 2023
1 parent 9fa5720 commit a385f57
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions src/components/Notification/index.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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";
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);

Expand Down Expand Up @@ -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")
Expand All @@ -84,7 +82,7 @@ function Notifications() {
batch
.commit()
.then(() => {
enqueueSnackbar("Friend Request Declined", {
enqueueSnackbar(`Friend Request ${accept ? "Accepted" : "Declined"}!`, {
variant: "success",
});
})
Expand All @@ -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 (
<>
<SideBar />
Expand All @@ -104,12 +127,6 @@ function Notifications() {
</div>
) : (
<div className="notification-container">
<ShareModal
openShareModal={openShareModal}
setOpenShareModal={setOpenShareModal}
currentPostLink={currentPostLink}
postText={postText}
/>
<Box>
<div
className="profile__favourites"
Expand Down Expand Up @@ -148,13 +165,21 @@ function Notifications() {
{name ? name : ""}.
</Link>
<div style={{ marginTop: "10px" }}>
<button className="accept-btn notif-btn">
<button
className="accept-btn notif-btn"
onClick={() => {
handleAcceptRequest(
notification.recipient,
notification.sender,
);
}}
>
Accept
</button>
<button
className="decline-btn notif-btn"
onClick={() =>
handleDeclineRequest(
handleRemoveNotifiction(
notification.recipient,
notification.sender,
)
Expand Down

0 comments on commit a385f57

Please sign in to comment.