Skip to content

Commit

Permalink
Channel changing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bubner committed Aug 12, 2023
1 parent 2b6a64c commit 934d5fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/Firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const storage = getStorage(app);
const auth = getAuth(app);
const db = getDatabase(app);

// Store what channel the user is currently in
export let currentChannel = "main";

// Define structure of user data from Firebase
export interface UserData {
email: string; // Primary key
Expand Down Expand Up @@ -249,7 +252,7 @@ export async function uploadMsg(formVal: string): Promise<void> {

// Add to Firebase with UID, content, and user info
const msgID = push(child(ref(db), "messages")).key;
await set(ref(db, "messages/main/" + msgID), {
await set(ref(db, `messages/${currentChannel}/` + msgID), {
isMsg: true,
isRetracted: false,
id: msgID,
Expand All @@ -264,7 +267,7 @@ export async function uploadMsg(formVal: string): Promise<void> {

export async function uploadFileMsg(url: string, type: string): Promise<void> {
const msgID = push(child(ref(db), "messages")).key;
await set(ref(db, "messages/main/" + msgID), {
await set(ref(db, `messages/${currentChannel}/` + msgID), {
isMsg: false,
isRetracted: false,
id: msgID,
Expand All @@ -278,7 +281,7 @@ export async function uploadFileMsg(url: string, type: string): Promise<void> {
}

export async function updateMsg(id: string, content: object): Promise<void> {
await update(ref(db, "messages/main/" + id), content);
await update(ref(db, `messages/${currentChannel}/` + id), content);
}

export async function deleteMsg(id: string): Promise<void> {
Expand All @@ -290,7 +293,7 @@ export async function deleteMsg(id: string): Promise<void> {
await deleteObject(fileRef).catch((err) => errorHandler(err));
}
// Now we can safely delete the message as we've deleted any other objects related to it
await remove(ref(db, "messages/main/" + id));
await remove(ref(db, `messages/${currentChannel}/` + id));
});
}

Expand All @@ -300,7 +303,7 @@ export async function uploadSysMsg(message: string): Promise<void> {
// Message limits can be ignored, as these messages are administrator controlled.
// Keep uid and email attached to display name to ensure validity and traceback for each system message.
const msgID = push(child(ref(db), "messages")).key;
await set(ref(db, "messages/main/" + msgID), {
await set(ref(db, `messages/${currentChannel}/` + msgID), {
isMsg: true,
isRetracted: false,
id: msgID,
Expand Down
7 changes: 3 additions & 4 deletions src/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author Lachlan Paul, 2023
*/

import {auth, db, getData, MessageData, toCommas} from "../Firebase";
import {auth, db, getData, MessageData, toCommas, currentChannel} from "../Firebase";
import {createRef, useEffect, useRef, useState} from "react";
import {onValue, ref} from "firebase/database";
import Message from "./Message";
Expand Down Expand Up @@ -57,7 +57,7 @@ function Chat() {

// Grand collection function that continually checks the message database for new/changed messages
useEffect(() => {
const unsubscribe = onValue(ref(db, "messages/main/"), (snapshot) => {
const unsubscribe = onValue(ref(db, `messages/${currentChannel}/`), (snapshot) => {
setMessageData(snapshot.val());
});
return () => unsubscribe();
Expand Down Expand Up @@ -153,8 +153,7 @@ function Chat() {
) : (
<>
<p className="top">
Welcome to the Bunyip Bellower! <br/> This is the start of the application's
history.
Welcome to the Bunyip Bellower! <br/> This is the start of the <b>{currentChannel}</b> channel.
</p>
<hr/>
</>
Expand Down

0 comments on commit 934d5fa

Please sign in to comment.