cleaning more things up
This commit is contained in:
@@ -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: {}) => {};
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
serverTimestamp,
|
||||
Timestamp,
|
||||
} from "firebase/firestore";
|
||||
import IFirestoreSerializable from "./firestoreSerializable";
|
||||
|
||||
export class User {
|
||||
id: string;
|
||||
|
||||
@@ -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"
|
||||
/>
|
||||
|
||||
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user