From b8d660e8d183993c77b2b48b21cd67c889220cc5 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Wed, 11 May 2022 00:56:44 -0700 Subject: [PATCH] Add field to edit API key on profile --- web/lib/firebase/users.ts | 7 +++++++ web/pages/profile.tsx | 42 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/web/lib/firebase/users.ts b/web/lib/firebase/users.ts index 6867b2d0..d7acd2bb 100644 --- a/web/lib/firebase/users.ts +++ b/web/lib/firebase/users.ts @@ -55,6 +55,13 @@ export async function updateUser(userId: string, update: Partial) { await updateDoc(doc(db, 'users', userId), { ...update }) } +export async function updatePrivateUser( + userId: string, + update: Partial +) { + await updateDoc(doc(db, 'private-users', userId), { ...update }) +} + export function listenForUser( userId: string, setUser: (user: User | null) => void diff --git a/web/pages/profile.tsx b/web/pages/profile.tsx index 07930d49..cf4cc8b4 100644 --- a/web/pages/profile.tsx +++ b/web/pages/profile.tsx @@ -1,4 +1,5 @@ import { useEffect, useState } from 'react' +import { RefreshIcon } from '@heroicons/react/outline' import Router from 'next/router' import { AddFundsButton } from 'web/components/add-funds-button' @@ -13,7 +14,7 @@ import { uploadImage } from 'web/lib/firebase/storage' import { Col } from 'web/components/layout/col' import { Row } from 'web/components/layout/row' import { User } from 'common/user' -import { updateUser } from 'web/lib/firebase/users' +import { updateUser, updatePrivateUser } from 'web/lib/firebase/users' import { defaultBannerUrl } from 'web/components/user-page' import { SiteLink } from 'web/components/site-link' import Textarea from 'react-expanding-textarea' @@ -63,6 +64,7 @@ export default function ProfilePage() { const [avatarLoading, setAvatarLoading] = useState(false) const [name, setName] = useState(user?.name || '') const [username, setUsername] = useState(user?.username || '') + const [apiKey, setApiKey] = useState(privateUser?.apiKey || '') useEffect(() => { if (user) { @@ -72,6 +74,12 @@ export default function ProfilePage() { } }, [user]) + useEffect(() => { + if (privateUser) { + setApiKey(privateUser.apiKey || '') + } + }, [privateUser]) + const updateDisplayName = async () => { const newName = cleanDisplayName(name) @@ -103,6 +111,17 @@ export default function ProfilePage() { } } + const updateApiKey = async (e: React.MouseEvent) => { + const newApiKey = crypto.randomUUID() + if (user?.id != null) { + setApiKey(newApiKey) + await updatePrivateUser(user.id, { apiKey: newApiKey }).catch(() => { + setApiKey(privateUser?.apiKey || '') + }) + } + e.preventDefault() + } + const fileHandler = async (event: any) => { const file = event.target.files[0] @@ -155,7 +174,6 @@ export default function ProfilePage() {
- -
+
+ +
+ + +
+
+ {user && ( <> {/* TODO: Allow users with M$ 2000 of assets to set custom banners */}