cleaning more things up

This commit is contained in:
Arjun Patel
2022-02-07 09:45:14 -08:00
parent c16a100f49
commit 2b2844ec98
6 changed files with 16 additions and 96 deletions
+1 -3
View File
@@ -6,9 +6,7 @@ import {
} from "firebase/firestore";
import { v4 as uuidv4 } from "uuid";
import IFirestoreSerializable from "./firestoreSerializable";
export class User {
export default class User {
id: string = uuidv4();
emailAddress: string;
@@ -2,7 +2,13 @@ import { LinkType } from "@nirvana/common/models/conversation";
import { UserStatus } from "@nirvana/common/models/user";
import { Tooltip } from "antd";
import { duration } from "moment";
import { useEffect, useRef, useState } from "react";
import {
LegacyRef,
useEffect,
useRef,
useState,
MutableRefObject,
} from "react";
import toast from "react-hot-toast";
import {
FaCheck,
@@ -1,7 +0,0 @@
export default interface IFirestoreSerializable {
id: string;
serialize: () => {};
// deserialize: (firestoreData: {}) => {};
}
-1
View File
@@ -4,7 +4,6 @@ import {
serverTimestamp,
Timestamp,
} from "firebase/firestore";
import IFirestoreSerializable from "./firestoreSerializable";
export class User {
id: string;
+8 -7
View File
@@ -1,8 +1,8 @@
import { Divider } from "antd";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { User } from "../../models/user";
import UserService from "../../services/userService";
import User from "@nirvana/common/models/user";
import UserService from "@nirvana/common/services/userService";
import { useAuth } from "../../contexts/authContext";
export default function Profile() {
@@ -22,7 +22,8 @@ export default function Profile() {
console.log(
"not authenticated...routing from dashboard to teams home"
);
// router.push('/teams/login')
router.push("/teams/login");
return;
}
// get user
@@ -32,10 +33,10 @@ export default function Profile() {
// if the user is null, then go ahead and create the user even though it's not there yet
if (!returnedUser) {
userService.createUser(
await userService.createUser(
currUser.uid,
currUser.email,
currUser.photoURL
currUser.email!,
currUser.photoURL!
);
}
@@ -124,7 +125,7 @@ export default function Profile() {
Please change your google image to change this.
</span>
<img
src={user ? user.avatarUrl : currUser.photoURL}
src={user ? user.avatarUrl : currUser!.photoURL}
alt="asdf"
className="rounded-lg mt-2"
/>
-77
View File
@@ -1,77 +0,0 @@
import { User as FirUser } from "firebase/auth";
import {
Firestore,
getFirestore,
doc,
getDoc,
setDoc,
Timestamp,
serverTimestamp,
onSnapshot,
Unsubscribe,
DocumentSnapshot,
} from "firebase/firestore";
import { User, UserStatus } from "../models/user";
import { Collections } from "./collections";
export default class UserService {
private db: Firestore = getFirestore();
// give back the avatar based on the person's google account avatar
getUserAvatar(displayName: string) {
return `https://ui-avatars.com/api/?name=${displayName}`;
}
async getUser(userId: string): Promise<User | null> {
const docRef = doc(this.db, Collections.users, userId);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
console.log("got user data");
let user: User = docSnap.data() as User;
return user;
} else {
// doc.data() will be undefined in this case
console.log("user not found!");
return null;
}
}
async getUserRealtime(userId: string): Promise<Unsubscribe> {
const docRef = doc(this.db, Collections.users, userId);
const unsub = onSnapshot(docRef, (doc) => {
console.log(doc.data());
});
return unsub;
}
async createUser(userId: string, emailAddress: string, avatarUrl: string) {
const docRef = doc(this.db, Collections.users, userId);
await setDoc(
docRef,
{ emailAddress, avatarUrl, createdDate: serverTimestamp() },
{ merge: true }
);
}
async updateUser(user: User) {
const docRef = doc(this.db, Collections.users, user.id);
await setDoc(
docRef,
{ ...user, lastUpdatedDate: serverTimestamp() },
{ merge: true }
);
}
async updateUserStatus(userId: string, newStatus: UserStatus) {
const docRef = doc(this.db, Collections.users, userId);
await setDoc(
docRef,
{ userStatus: newStatus, lastUpdatedDate: serverTimestamp() },
{ merge: true }
);
}
}