From 8fa549389072d0c3217894963e16f7186dd8f833 Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Thu, 10 Oct 2024 22:24:36 +0300 Subject: [PATCH] Use environment variables instead of constants --- .env | 4 +++ src/constants.ts | 5 ---- src/lib/appwrite.ts | 9 ++++-- src/lib/context/estimation.tsx | 25 ++++++++-------- src/lib/context/estimationSession.tsx | 41 ++++++++++++++------------- src/main.tsx | 13 ++++++++- src/pages/Profile.tsx | 5 ++++ src/vite-env.d.ts | 11 +++++++ 8 files changed, 73 insertions(+), 40 deletions(-) create mode 100644 .env delete mode 100644 src/constants.ts create mode 100644 src/pages/Profile.tsx diff --git a/.env b/.env new file mode 100644 index 0000000..02409f9 --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +VITE_APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1 +VITE_APPWRITE_PROJECT_ID=scrummie-poker +VITE_APPWRITE_DATABASE_ID=670402eb000f5aff721f +VITE_APPWRITE_ESTIMATION_SESSION_COLLECTION_ID=670402f60023cb78d441 diff --git a/src/constants.ts b/src/constants.ts deleted file mode 100644 index 2c03033..0000000 --- a/src/constants.ts +++ /dev/null @@ -1,5 +0,0 @@ -// TODO: Think about moving to .env -export const APPWRITE_ENDPOINT = 'https://cloud.appwrite.io/v1'; -export const APPWRITE_PROJECT_ID = 'scrummie-poker'; -export const APPWRITE_DATABASE_ID = '670402eb000f5aff721f'; -export const APPWRITE_ESTIMATION_SESSION_COLLECTION_ID = '670402f60023cb78d441'; diff --git a/src/lib/appwrite.ts b/src/lib/appwrite.ts index fa5b8d3..78aa0c0 100644 --- a/src/lib/appwrite.ts +++ b/src/lib/appwrite.ts @@ -1,11 +1,16 @@ import { Client, Account, Databases } from 'appwrite'; -import { APPWRITE_ENDPOINT, APPWRITE_PROJECT_ID } from '../constants'; export const client = new Client(); -client.setEndpoint(APPWRITE_ENDPOINT).setProject(APPWRITE_PROJECT_ID); +client + .setEndpoint(import.meta.env.VITE_APPWRITE_ENDPOINT) + .setProject(import.meta.env.VITE_APPWRITE_PROJECT_ID); export { ID } from 'appwrite'; export const account = new Account(client); export const databases = new Databases(client); + +export const DATABASE_ID = import.meta.env.VITE_APPWRITE_DATABASE_ID; +export const ESTIMATION_SESSION_COLLECTION_ID = import.meta.env + .VITE_APPWRITE_ESTIMATION_SESSION_COLLECTION_ID; diff --git a/src/lib/context/estimation.tsx b/src/lib/context/estimation.tsx index 71246fe..cb81b51 100644 --- a/src/lib/context/estimation.tsx +++ b/src/lib/context/estimation.tsx @@ -5,12 +5,13 @@ import { useEffect, useState, } from 'react'; -import { client, databases } from '../appwrite'; -import { DatabaseModels, EntityModels } from '../types'; import { - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, -} from '../../constants'; + client, + DATABASE_ID, + databases, + ESTIMATION_SESSION_COLLECTION_ID, +} from '../appwrite'; +import { DatabaseModels, EntityModels } from '../types'; import { useUser } from './user'; import { EstimationSessionTicket } from '../types/entityModels'; @@ -78,8 +79,8 @@ export const EstimationContextProvider = (props: PropsWithChildren) => { databases .getDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, sessionId, ) .then((payload) => { @@ -89,7 +90,7 @@ export const EstimationContextProvider = (props: PropsWithChildren) => { return client.subscribe( [ - `databases.${APPWRITE_DATABASE_ID}.collections.${APPWRITE_ESTIMATION_SESSION_COLLECTION_ID}.documents.${sessionId}`, + `databases.${DATABASE_ID}.collections.${ESTIMATION_SESSION_COLLECTION_ID}.documents.${sessionId}`, ], ({ payload }) => { const userId = userData?.$id ?? ''; // TODO: Not sure if this is the user id or session @@ -108,8 +109,8 @@ export const EstimationContextProvider = (props: PropsWithChildren) => { data: Partial, ) => { await databases.updateDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, sessionId, { sessionState: JSON.stringify({ @@ -159,8 +160,8 @@ export const EstimationContextProvider = (props: PropsWithChildren) => { .map((x) => JSON.stringify(x)); await databases.updateDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, sessionId, { tickets: newTicketsValue, diff --git a/src/lib/context/estimationSession.tsx b/src/lib/context/estimationSession.tsx index 57e21a5..941f9ff 100644 --- a/src/lib/context/estimationSession.tsx +++ b/src/lib/context/estimationSession.tsx @@ -5,12 +5,13 @@ import { useEffect, useState, } from 'react'; -import { client, databases } from '../appwrite'; -import { ID, Models, Query } from 'appwrite'; import { - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, -} from '../../constants'; + client, + DATABASE_ID, + databases, + ESTIMATION_SESSION_COLLECTION_ID, +} from '../appwrite'; +import { ID, Models, Query } from 'appwrite'; interface EstimationSessionType extends Models.Document { userId: string; @@ -72,8 +73,8 @@ export function EstimationSessionProvider(props: PropsWithChildren) { estimationSession: Omit, ) => { const response = await databases.createDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, ID.unique(), estimationSession, ); @@ -84,8 +85,8 @@ export function EstimationSessionProvider(props: PropsWithChildren) { const remove = async (id: string) => { await databases.deleteDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, id, ); setEstimationSessions((estimationSessions) => @@ -102,8 +103,8 @@ export function EstimationSessionProvider(props: PropsWithChildren) { ) => { const currentSession = estimationSessions.find((x) => x.$id === sessionId); const response = await databases.updateDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, sessionId, { tickets: currentSession?.tickets.concat([ @@ -132,8 +133,8 @@ export function EstimationSessionProvider(props: PropsWithChildren) { const selectTicket = async (sessionId: string, ticketId: string) => { const response = await databases.updateDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, sessionId, { sessionState: JSON.stringify({ @@ -171,8 +172,8 @@ export function EstimationSessionProvider(props: PropsWithChildren) { }, ]); const response = await databases.updateDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, sessionId, { sessionState: JSON.stringify({ @@ -192,8 +193,8 @@ export function EstimationSessionProvider(props: PropsWithChildren) { const revealVotes = async (sessionId: string) => { const currentState = getState(sessionId); const response = await databases.updateDocument( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, sessionId, { sessionState: JSON.stringify({ @@ -212,8 +213,8 @@ export function EstimationSessionProvider(props: PropsWithChildren) { const init = async () => { const response = await databases.listDocuments( - APPWRITE_DATABASE_ID, - APPWRITE_ESTIMATION_SESSION_COLLECTION_ID, + DATABASE_ID, + ESTIMATION_SESSION_COLLECTION_ID, [Query.orderDesc('$createdAt'), Query.limit(10)], ); setEstimationSessions(response.documents); @@ -224,7 +225,7 @@ export function EstimationSessionProvider(props: PropsWithChildren) { return client.subscribe( [ - `databases.${APPWRITE_DATABASE_ID}.collections.${APPWRITE_ESTIMATION_SESSION_COLLECTION_ID}.documents`, + `databases.${DATABASE_ID}.collections.${ESTIMATION_SESSION_COLLECTION_ID}.documents`, ], (payload) => { setEstimationSessions((estimationSessions) => diff --git a/src/main.tsx b/src/main.tsx index b203482..bd0e2fd 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -16,6 +16,7 @@ import { EstimationSessionProvider } from './lib/context/estimationSession'; import { EstimationContextProvider } from './lib/context/estimation'; import Estimation from './pages/Estimation/Estimation'; import Header from './components/Header'; +import Profile from './pages/Profile'; interface RouterContext { userContext: UserContextType; @@ -61,6 +62,12 @@ const loginRoute = createRoute({ getParentRoute: () => rootRoute, }); +const profileRoute = createRoute({ + path: 'profile', + component: Profile, + getParentRoute: () => authenticatedRoute, +}); + const estimationSessionRoute = createRoute({ path: 'estimate/session/$sessionId', component: Estimation, @@ -69,7 +76,11 @@ const estimationSessionRoute = createRoute({ const router = createRouter({ routeTree: rootRoute.addChildren([ - authenticatedRoute.addChildren([indexRoute, estimationSessionRoute]), + authenticatedRoute.addChildren([ + indexRoute, + profileRoute, + estimationSessionRoute, + ]), loginRoute, ]), context: { diff --git a/src/pages/Profile.tsx b/src/pages/Profile.tsx new file mode 100644 index 0000000..fe93038 --- /dev/null +++ b/src/pages/Profile.tsx @@ -0,0 +1,5 @@ +const Profile = () => { + return

TODO

; +}; + +export default Profile; diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 11f02fe..b833d2b 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1 +1,12 @@ /// + +interface ImportMetaEnv { + readonly VITE_APPWRITE_ENDPOINT: string; + readonly VITE_APPWRITE_PROJECT_ID: string; + readonly VITE_APPWRITE_DATABASE_ID: string; + readonly VITE_APPWRITE_ESTIMATION_SESSION_COLLECTION_ID: string; +} + +interface ImportMeta { + readonly env: ImportMetaEnv; +}