From 46f738fe0221d528aac150cb6519cc894363ce4f Mon Sep 17 00:00:00 2001 From: Keane Chan Date: Fri, 7 Oct 2022 20:54:36 +0800 Subject: [PATCH] [resumes][fix] fix file upload failure --- apps/portal/.env.example | 4 ---- apps/portal/.env.local.example | 3 +++ apps/portal/package.json | 2 ++ apps/portal/src/env/schema.mjs | 7 ++++--- apps/portal/src/pages/resumes/submit.tsx | 6 +++--- apps/portal/src/utils/supabaseClient.ts | 6 +++--- yarn.lock | 10 ++++++++++ 7 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 apps/portal/.env.local.example diff --git a/apps/portal/.env.example b/apps/portal/.env.example index a3d958b7..bf48e8b5 100644 --- a/apps/portal/.env.example +++ b/apps/portal/.env.example @@ -8,7 +8,3 @@ NEXTAUTH_URL=http://localhost:3000 # Next Auth GitHub Provider GITHUB_CLIENT_ID=a5164b1943b5413ff2f5 GITHUB_CLIENT_SECRET= - -# Supabase -SUPABASE_URL= -SUPABASE_ANON_KEY= diff --git a/apps/portal/.env.local.example b/apps/portal/.env.local.example new file mode 100644 index 00000000..2bbde716 --- /dev/null +++ b/apps/portal/.env.local.example @@ -0,0 +1,3 @@ +# Supabase +SUPABASE_URL= +SUPABASE_ANON_KEY= diff --git a/apps/portal/package.json b/apps/portal/package.json index 8f984204..163e36fb 100644 --- a/apps/portal/package.json +++ b/apps/portal/package.json @@ -32,6 +32,7 @@ "react-pdf": "^5.7.2", "react-query": "^3.39.2", "superjson": "^1.10.0", + "uuid": "^9.0.0", "zod": "^3.18.0" }, "devDependencies": { @@ -41,6 +42,7 @@ "@types/react": "^18.0.21", "@types/react-dom": "^18.0.6", "@types/react-pdf": "^5.7.2", + "@types/uuid": "^8.3.4", "autoprefixer": "^10.4.12", "postcss": "^8.4.16", "prettier-plugin-tailwindcss": "^0.1.13", diff --git a/apps/portal/src/env/schema.mjs b/apps/portal/src/env/schema.mjs index 5fd67c58..37eb2f5b 100644 --- a/apps/portal/src/env/schema.mjs +++ b/apps/portal/src/env/schema.mjs @@ -12,8 +12,6 @@ export const serverSchema = z.object({ NEXTAUTH_SECRET: z.string(), NEXTAUTH_URL: z.string().url(), NODE_ENV: z.enum(['development', 'test', 'production']), - SUPABASE_ANON_KEY: z.string(), - SUPABASE_URL: z.string(), }); /** @@ -22,7 +20,8 @@ export const serverSchema = z.object({ * To expose them to the client, prefix them with `NEXT_PUBLIC_`. */ export const clientSchema = z.object({ - // NEXT_PUBLIC_BAR: z.string(), + NEXT_PUBLIC_SUPABASE_ANON_KEY: z.string(), + NEXT_PUBLIC_SUPABASE_URL: z.string().url(), }); /** @@ -33,4 +32,6 @@ export const clientSchema = z.object({ */ export const clientEnv = { // NEXT_PUBLIC_BAR: process.env.NEXT_PUBLIC_BAR, + NEXT_PUBLIC_SUPABASE_ANON_KEY: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY, + NEXT_PUBLIC_SUPABASE_URL: process.env.NEXT_PUBLIC_SUPABASE_URL, }; diff --git a/apps/portal/src/pages/resumes/submit.tsx b/apps/portal/src/pages/resumes/submit.tsx index cc7e7fda..b75fe72b 100644 --- a/apps/portal/src/pages/resumes/submit.tsx +++ b/apps/portal/src/pages/resumes/submit.tsx @@ -4,6 +4,7 @@ import { useRouter } from 'next/router'; import { useMemo, useState } from 'react'; import type { SubmitHandler } from 'react-hook-form'; import { useForm } from 'react-hook-form'; +import { v4 as uuidv4 } from 'uuid'; import { PaperClipIcon } from '@heroicons/react/24/outline'; import { Button, Select, TextArea, TextInput } from '@tih/ui'; @@ -51,12 +52,11 @@ export default function SubmitResumeForm() { const onSubmit: SubmitHandler = async (data) => { if (resumeFile == null) { - // TODO: Handle error return; } - // TODO: Generate unique URL - const url = resumeFile.name; + // Prefix with uuid so that it is always unique + const url = `${uuidv4()}-${resumeFile.name}`; const { error } = await supabase.storage .from('resumes') .upload(url, resumeFile); diff --git a/apps/portal/src/utils/supabaseClient.ts b/apps/portal/src/utils/supabaseClient.ts index 5168b6c7..18d0e0ea 100644 --- a/apps/portal/src/utils/supabaseClient.ts +++ b/apps/portal/src/utils/supabaseClient.ts @@ -1,8 +1,8 @@ import { createClient } from '@supabase/supabase-js'; -import { env } from '~/env/server.mjs'; +import { env } from '~/env/client.mjs'; -const supabaseUrl = env.SUPABASE_URL; -const supabaseAnonKey = env.SUPABASE_ANON_KEY; +const supabaseUrl = env.NEXT_PUBLIC_SUPABASE_URL; +const supabaseAnonKey = env.NEXT_PUBLIC_SUPABASE_ANON_KEY; export const supabase = createClient(supabaseUrl, supabaseAnonKey); diff --git a/yarn.lock b/yarn.lock index ed38ac0a..11ba51fd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3688,6 +3688,11 @@ resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.6.tgz#250a7b16c3b91f672a24552ec64678eeb1d3a08d" integrity sha512-PBjIUxZHOuj0R15/xuwJYjFi+KZdNFrehocChv4g5hu6aFroHue8m0lBP0POdK2nKzbw0cgV1mws8+V/JAcEkQ== +"@types/uuid@^8.3.4": + version "8.3.4" + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc" + integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw== + "@types/webpack-env@^1.16.0": version "1.18.0" resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.18.0.tgz#ed6ecaa8e5ed5dfe8b2b3d00181702c9925f13fb" @@ -14565,6 +14570,11 @@ uuid@^8.3.2: resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== +uuid@^9.0.0: + version "9.0.0" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5" + integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg== + v8-compile-cache-lib@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"