这是indexloc提供的服务,不要输入任何密码
Skip to content

Milestone 6: User settings #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 65 additions & 4 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"node": "18"
},
"dependencies": {
"axios": "^1.7.2",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"firebase-admin": "^12.1.1",
Expand All @@ -27,6 +28,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/node": "^18.19.34",
"@types/sanitize-html": "^2.11.0",
"@types/sharp": "^0.32.0",
Expand Down
6 changes: 5 additions & 1 deletion functions/src/models/TanamUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ export type TanamRole = "publisher" | "admin";
export interface ITanamUser<TimestampType> {
role?: TanamRole;
name?: string;
colorMode: "dark" | "light";
createdAt: TimestampType;
updatedAt: TimestampType;
}

export abstract class TanamUser<TimestampType, FieldValueType> {
constructor(id: string, json: ITanamUser<TimestampType>) {
this.id = id;
this.name = json.name;
this.role = json.role ?? "publisher";
this.name = json.name;
this.colorMode = json.colorMode ?? "light";
this.createdAt = json.createdAt;
this.updatedAt = json.updatedAt;
}

public readonly id: string;
public role: TanamRole;
public name?: string;
public colorMode: "dark" | "light";
public readonly createdAt: TimestampType;
public readonly updatedAt: TimestampType;

Expand All @@ -28,6 +31,7 @@ export abstract class TanamUser<TimestampType, FieldValueType> {
return {
role: this.role,
name: this.name,
colorMode: this.colorMode,
createdAt: this.createdAt ?? this.getServerTimestamp(),
updatedAt: this.getServerTimestamp(),
};
Expand Down
1 change: 1 addition & 0 deletions functions/src/models/TanamUserAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class TanamUserAdmin extends TanamUser<Timestamp, FieldValue> {
return new TanamUserAdmin(snap.id, {
role: data.role,
name: data.name,
colorMode: data.colorMode,
createdAt: data.createdAt || Timestamp.now(),
updatedAt: data.updatedAt || Timestamp.now(),
});
Expand Down
1 change: 1 addition & 0 deletions functions/src/triggers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const tanamNewUserInit = onDocumentCreated("tanam-users/{docId}", async (
...docData,
name: firebaseUser.displayName,
role: existingDocs.size === 1 ? "admin" : "publisher",
colorMode: "light",
createdAt: Timestamp.now(),
updatedAt: Timestamp.now(),
});
Expand Down
17 changes: 11 additions & 6 deletions hosting/src/components/Header/DarkModeSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import useColorMode from "@/hooks/useColorMode";
import {Switcher} from "@/components/Form/Switcher";
import {useAuthentication} from "@/hooks/useAuthentication";
import useColorMode from "@/hooks/useColorMode";
import {useTanamUser} from "@/hooks/useTanamUser";
import {useEffect} from "react";

const DarkModeSwitcher = () => {
const [colorMode, setColorMode] = useColorMode();
const {authUser} = useAuthentication();
const {tanamUser, saveColorMode} = useTanamUser(authUser?.uid);

const handleColorModeToggle = (checked: boolean) => {
if (typeof setColorMode === "function") {
setColorMode(checked ? "dark" : "light");
useEffect(() => {
if (typeof setColorMode === "function" && !!tanamUser?.colorMode) {
setColorMode(tanamUser.colorMode);
}
};
}, [tanamUser]);

return (
<Switcher
style="default"
defaultChecked={colorMode === "dark"}
onChange={handleColorModeToggle}
onChange={async (checked) => await saveColorMode(checked ? "dark" : "light")}
onIcon="i-ri-moon-clear-fill text-white"
offIcon="i-ri-sun-line"
/>
Expand Down
31 changes: 20 additions & 11 deletions hosting/src/hooks/useTanamUser.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import {TanamUserClient} from "@/models/TanamUserClient";
import {UserNotification} from "@/models/UserNotification";
import {firestore, storage} from "@/plugins/firebase";
import {doc, onSnapshot} from "firebase/firestore";
import {doc, onSnapshot, updateDoc} from "firebase/firestore";
import {getDownloadURL, ref} from "firebase/storage";
import {useEffect, useState} from "react";

interface UseTanamDocumentsResult {
data: TanamUserClient | null;
error: UserNotification | null;
}

/**
* Hook to get a Tanam user document from Firestore
*
* @param {string?} uid User ID
* @return {UseTanamDocumentsResult} Hook for documents subscription
*/
export function useTanamUser(uid?: string): UseTanamDocumentsResult {
const [data, setData] = useState<TanamUserClient | null>(null);
export function useTanamUser(uid?: string) {
const [tanamUser, setTanamUser] = useState<TanamUserClient | null>(null);
const [error, setError] = useState<UserNotification | null>(null);

useEffect(() => {
if (!uid) {
setData(null);
setTanamUser(null);
return;
}

Expand All @@ -35,7 +30,7 @@ export function useTanamUser(uid?: string): UseTanamDocumentsResult {
}

const tanamUser = TanamUserClient.fromFirestore(snapshot);
setData(tanamUser);
setTanamUser(tanamUser);
},
(err) => {
setError(new UserNotification("error", "Error fetching user", err.message));
Expand All @@ -46,7 +41,21 @@ export function useTanamUser(uid?: string): UseTanamDocumentsResult {
return () => unsubscribe();
}, [uid]);

return {data, error};
async function saveColorMode(colorMode: "dark" | "light") {
if (!uid) {
return;
}

try {
const docRef = doc(firestore, `tanam-users`, uid);
return updateDoc(docRef, {colorMode});
} catch (error) {
const typedError = error as Error;
setError(new UserNotification("error", "Failed to set color mode to " + colorMode, typedError.message));
}
}

return {tanamUser, saveColorMode, error};
}

interface UseProfileImageResult {
Expand Down
1 change: 1 addition & 0 deletions hosting/src/models/TanamUserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class TanamUserClient extends TanamUser<Timestamp, FieldValue> {
return new TanamUserClient(snap.id, {
role: data.role,
name: data.name,
colorMode: data.colorMode,
createdAt: data.createdAt || Timestamp.now(),
updatedAt: data.updatedAt || Timestamp.now(),
});
Expand Down
Loading