From 7d24a3e4a237a3e5c8f7dafd2392b78c02acf8be Mon Sep 17 00:00:00 2001 From: ingawei <46611122+ingawei@users.noreply.github.com> Date: Fri, 15 Jul 2022 18:42:37 -0700 Subject: [PATCH] Inga/manalink bug fixes (#653) * fixed manalinks bug of claiming own manalink, and also rerouting to home upon claiming if not logged in * no more multiple hardcoded manalink messages --- functions/README.md | 6 ++++- functions/src/claim-manalink.ts | 3 +++ web/components/button.tsx | 7 ++++-- web/components/manalink-card.tsx | 22 +++++++++---------- .../manalinks/create-links-button.tsx | 12 +++++----- web/pages/link/[slug].tsx | 9 +++++--- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/functions/README.md b/functions/README.md index 8013fb20..97a7a33b 100644 --- a/functions/README.md +++ b/functions/README.md @@ -27,6 +27,7 @@ Adapted from https://firebase.google.com/docs/functions/get-started 1. `$ brew install java` 2. `$ sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk` + 2. `$ gcloud auth login` to authenticate the CLI tools to Google Cloud 3. `$ gcloud config set project ` to choose the project (`$ gcloud projects list` to see options) 4. `$ mkdir firestore_export` to create a folder to store the exported database @@ -53,7 +54,10 @@ Adapted from https://firebase.google.com/docs/functions/get-started ## Deploying -0. `$ firebase use prod` to switch to prod +0. After merging, you need to manually deploy to backend: +1. `git checkout main` +1. `git pull origin main` +1. `$ firebase use prod` to switch to prod 1. `$ firebase deploy --only functions` to push your changes live! (Future TODO: auto-deploy functions on Git push) diff --git a/functions/src/claim-manalink.ts b/functions/src/claim-manalink.ts index 3822bbf7..b534f0a3 100644 --- a/functions/src/claim-manalink.ts +++ b/functions/src/claim-manalink.ts @@ -28,6 +28,9 @@ export const claimmanalink = newEndpoint({}, async (req, auth) => { if (amount <= 0 || isNaN(amount) || !isFinite(amount)) throw new APIError(500, 'Invalid amount') + if (auth.uid === fromId) + throw new APIError(400, `You can't claim your own manalink`) + const fromDoc = firestore.doc(`users/${fromId}`) const fromSnap = await transaction.get(fromDoc) if (!fromSnap.exists) { diff --git a/web/components/button.tsx b/web/components/button.tsx index d050b81e..8cdeacdd 100644 --- a/web/components/button.tsx +++ b/web/components/button.tsx @@ -2,12 +2,13 @@ import { ReactNode } from 'react' import clsx from 'clsx' export function Button(props: { - children: ReactNode className?: string onClick?: () => void + children?: ReactNode size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' color?: 'green' | 'red' | 'blue' | 'indigo' | 'yellow' | 'gray' type?: 'button' | 'reset' | 'submit' + disabled?: boolean }) { const { children, @@ -16,6 +17,7 @@ export function Button(props: { size = 'md', color = 'indigo', type = 'button', + disabled = false, } = props const sizeClasses = { @@ -30,7 +32,7 @@ export function Button(props: { + @@ -71,9 +70,8 @@ export function ManalinkCard(props: { export function ManalinkCardPreview(props: { className?: string info: ManalinkInfo - defaultMessage: string }) { - const { className, defaultMessage, info } = props + const { className, info } = props const { expiresTime, maxUses, uses, amount, message } = info return (
{formatMoney(amount)}
-
{message || defaultMessage}
+
{message}
diff --git a/web/components/manalinks/create-links-button.tsx b/web/components/manalinks/create-links-button.tsx index 538e794e..0d1d603e 100644 --- a/web/components/manalinks/create-links-button.tsx +++ b/web/components/manalinks/create-links-button.tsx @@ -66,12 +66,14 @@ function CreateManalinkForm(props: { const defaultExpire = 'week' const [expiresIn, setExpiresIn] = useState(defaultExpire) + const defaultMessage = 'from ' + user.name + const [newManalink, setNewManalink] = useState({ expiresTime: dayjs().add(1, defaultExpire).valueOf(), amount: 100, maxUses: 1, uses: 0, - message: '', + message: defaultMessage, }) const EXPIRE_OPTIONS = { @@ -161,7 +163,7 @@ function CreateManalinkForm(props: {