From c58890ee104eba707eff13ec6a04b36ec7db8665 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Mon, 17 Jan 2022 15:23:55 -0800 Subject: [PATCH] adding sub text and skeleton loader --- components/Loading/skeletonLoader.tsx | 11 ++++ components/RoomCard.tsx | 84 +++++++++++++++++++++++++-- models/room.ts | 2 + services/roomService.tsx | 10 ++++ 4 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 components/Loading/skeletonLoader.tsx diff --git a/components/Loading/skeletonLoader.tsx b/components/Loading/skeletonLoader.tsx new file mode 100644 index 0000000..ba49209 --- /dev/null +++ b/components/Loading/skeletonLoader.tsx @@ -0,0 +1,11 @@ +export default function SkeletonLoader() { + return ( +
+
+
+
+
+
+
+ ); +} diff --git a/components/RoomCard.tsx b/components/RoomCard.tsx index 5bab219..1778da5 100644 --- a/components/RoomCard.tsx +++ b/components/RoomCard.tsx @@ -9,13 +9,21 @@ import { Avatar, Divider, Tooltip } from "antd"; import { UserOutlined, AntDesignOutlined } from "@ant-design/icons"; import { useTeamDashboardContext } from "../contexts/teamDashboardContext"; import { User } from "../models/user"; +import RoomService from "../services/roomService"; +import toast from "react-hot-toast"; +import { useState } from "react"; +import SkeletonLoader from "./Loading/skeletonLoader"; interface IRoomCardProps { room: Room; } + +const roomService = new RoomService(); + export default function RoomCard(props: IRoomCardProps) { const { currUser } = useAuth(); const { teamUsersMap, user } = useTeamDashboardContext(); + const [loading, setLoading] = useState(false); var isUserInRoom: boolean; if (props.room.membersInRoom?.includes(currUser.uid)) { @@ -117,6 +125,51 @@ export default function RoomCard(props: IRoomCardProps) { ); }; + async function handleLeavingRoom() { + setLoading(true); + + try { + // filter current membersInRoom and return new array + const newMembersInRoom = props.room.membersInRoom.filter( + (memberId) => memberId != currUser.uid + ); + + // update the room with room id to have a new array of userIds + await roomService.updateMembersInRoom(props.room.id, newMembersInRoom); + } catch (error) { + console.error(error); + + toast.error("unable to leave room...something went wrong"); + } + + setLoading(false); + } + + async function handleJoiningRoom() { + setLoading(true); + + try { + // update members in room + const newMembersInRoom = [...props.room.membersInRoom, currUser.uid]; + + // update the room with room id to have a new array of userIds + await roomService.updateMembersInRoom(props.room.id, newMembersInRoom); + + // then window.open to room's link + window.open(props.room.link, "_blank"); + } catch (error) { + console.error(error); + + toast.error("unable to leave room...something went wrong"); + } + + setLoading(false); + } + + if (loading) { + return ; + } + return ( {props.room.description} @@ -154,8 +207,15 @@ export default function RoomCard(props: IRoomCardProps) { {/* room status and link(s) */} - + + + on and off all day + {/* room attachments */} @@ -178,9 +238,21 @@ export default function RoomCard(props: IRoomCardProps) { {/* footer */} {membersInRoom()} - + {isUserInRoom ? ( + + ) : ( + + )} {" "} diff --git a/models/room.ts b/models/room.ts index 130046d..035e043 100644 --- a/models/room.ts +++ b/models/room.ts @@ -22,6 +22,8 @@ export default class Room { createdByUserId: string; teamId: string; + + lastUpdatedDate: Timestamp; } export enum RoomType { diff --git a/services/roomService.tsx b/services/roomService.tsx index 658a791..f07e07a 100644 --- a/services/roomService.tsx +++ b/services/roomService.tsx @@ -5,6 +5,7 @@ import { Firestore, getFirestore, serverTimestamp, + setDoc, } from "firebase/firestore"; import Room from "../models/room"; import { Collections } from "./collections"; @@ -18,4 +19,13 @@ export default class RoomService { createdDate: serverTimestamp(), }); } + + async updateMembersInRoom(roomId: string, newMembersInRoom: string[]) { + const docRef = doc(this.db, Collections.rooms, roomId); + await setDoc( + docRef, + { membersInRoom: newMembersInRoom, lastUpdatedDate: serverTimestamp() }, + { merge: true } + ); + } }