Skip to content

Commit

Permalink
Discussion Posts functional mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Sanderson committed Nov 14, 2024
1 parent da7c7f3 commit b2485ff
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 19 deletions.
40 changes: 22 additions & 18 deletions public/scripts/clubhome.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.13.2/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.13.2/firebase-analytics.js";
import { getFirestore, collection, getDoc, getDocs, doc, query, where } from 'https://www.gstatic.com/firebasejs/10.13.2/firebase-firestore.js';
import { getFirestore, collection, orderBy, getDoc, getDocs, doc, query, where } from 'https://www.gstatic.com/firebasejs/10.13.2/firebase-firestore.js';
import { getAuth,
createUserWithEmailAndPassword,
signOut,
Expand Down Expand Up @@ -82,6 +82,7 @@ from "https://www.gstatic.com/firebasejs/10.13.2/firebase-auth.js";




async function getEmailFromUID(uid)
{
const q = query(collection(db, "Users"), where("uid", "==", uid));
Expand All @@ -90,6 +91,20 @@ from "https://www.gstatic.com/firebasejs/10.13.2/firebase-auth.js";
return qsnap.docs[0].data()["email"];
}

if (await isAdmin(userUID) == 1)
{
document.getElementById("manage").href = "overview.html?id=" + docRef.id;
}
else
{
let elem = document.getElementById("manage")
elem.remove()
}

const post = document.getElementById("post");
post.href = "discussionpost.html?id=" + queryParams.get("id");


let docRef = await getClubdocRefFromQParams(queryParams);

document.getElementById("clubName").textContent = "Welcome to the \"" + docRef.data()["BookClubName"] + "\" Book Club"
Expand All @@ -108,29 +123,18 @@ from "https://www.gstatic.com/firebasejs/10.13.2/firebase-auth.js";
parentUL.appendChild(newLI);
}
let docRef2 = doc(db,"BookClubs", queryParams.get("id"));

const discRef = collection(docRef2, "DiscussionPosts");
const dq = query(discRef, orderBy("postDate"));
const dqsnap = await getDocs(dq);

const discRef = await getDocs(collection(docRef2, "DiscussionPosts"));
let elem;

discRef.forEach((doc)=>{
dqsnap.forEach((doc)=>{

elem = createDisPost(doc);
document.getElementById("disc").appendChild(elem);
})





console.log(isAdmin(userUID));

if (await isAdmin(userUID) == 1)
{
document.getElementById("manage").href = "overview.html?id=" + docRef.id;
}
else
{
let elem = document.getElementById("manage")
elem.remove()
}


69 changes: 69 additions & 0 deletions public/scripts/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.13.2/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.13.2/firebase-analytics.js";
import { getFirestore, collection, orderBy, getDoc, addDoc, getDocs, doc, query, where } from 'https://www.gstatic.com/firebasejs/10.13.2/firebase-firestore.js';
import { getAuth,
createUserWithEmailAndPassword,
signOut,
signInWithEmailAndPassword,
onAuthStateChanged
}

from "https://www.gstatic.com/firebasejs/10.13.2/firebase-auth.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyAt-SInlPaL2FzwtXrRltIEiV5l8k5HMjg",
authDomain: "pageturners-a831a.firebaseapp.com",
projectId: "pageturners-a831a",
storageBucket: "pageturners-a831a.appspot.com",
messagingSenderId: "304224952392",
appId: "1:304224952392:web:f33dbc84b481e39a44787d",
measurementId: "G-C6DKQSJ1R8"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const auth = getAuth();
const db = getFirestore(app);

const queryParams = new URLSearchParams(window.location.search);
let userUID = "";
let userEmail = "";
auth.onAuthStateChanged(function(user){
if (user) {
userUID = user.uid;
userEmail = user.email;
}
})

const postform = document.getElementById("reply-form");

postform.addEventListener("submit", async function (e) {

e.preventDefault();

let title = postform["title"].value;
let body = postform["text"].value;

//let docRef2 = doc(db,"BookClubs", queryParams.get("id"));
const discRef = collection(db,"BookClubs",queryParams.get("id"),"DiscussionPosts");

let docref = await addDoc(discRef, {
Author: userEmail,
Body: body,
Title: title,
postDate: new Date()
}).then((e)=>{
window.location.href = "./clubhomepage.html?id=" + queryParams.get("id");
})






})
2 changes: 1 addition & 1 deletion public/webpages/html/clubhomepage.html
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ <h3>Discussion Board</h3>
<h2 id="disctopic">Current Topic: None</h2>
<ul id="disc">
</ul>
<button>Add Your Own Comment</button>
<a id="post"><button>Add Your Own Post</button>
</section>
<script type="module" src="../../scripts/clubhome.js"></script>
</body>
Expand Down
25 changes: 25 additions & 0 deletions public/webpages/html/discussionpost.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Discussion Thread</title>
<link rel="stylesheet" href="../css/loginreg.css">
</head>
<body>
<main>
<section class="discussion-thread">
<h2 id="discussion-title"></h2>
<div id="discussion-container"></div>

<!-- Form to submit a new reply -->
<form id="reply-form">
<input type="text" id="title" placeholder="Title" required>
<textarea id="text" rows="4" placeholder="Write your Post here..." required></textarea>
<button type="submit">Post</button>
</form>
</section>
</main>
<script type="module" src="../../scripts/post.js"></script>
</body>
</html>

0 comments on commit b2485ff

Please sign in to comment.