From fc55c0491c55ab4d0505edc19392a498554f1b54 Mon Sep 17 00:00:00 2001 From: William Correa Date: Thu, 2 May 2024 20:21:45 -0300 Subject: [PATCH] feature: configure http mode development --- frontend/config/dependencies.ts | 10 +++--- frontend/config/env.ts | 14 ++++++-- frontend/docker-compose.yml | 11 +++++++ .../Infrastructure/Http/HttpGameRepository.ts | 33 +++++++++++++++++++ .../view/pages/public/auth/SignInPage.tsx | 9 ++--- 5 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 frontend/docker-compose.yml create mode 100644 frontend/src/Infrastructure/Http/HttpGameRepository.ts diff --git a/frontend/config/dependencies.ts b/frontend/config/dependencies.ts index 617e931..1254aa5 100644 --- a/frontend/config/dependencies.ts +++ b/frontend/config/dependencies.ts @@ -14,7 +14,8 @@ import InMemoryUserConfigRepository from '../src/Infrastructure/Memory/InMemoryU import SupabaseAuthRepository from '../src/Infrastructure/Supabase/SupabaseAuthRepository.ts' import SupabaseGameRepository from '../src/Infrastructure/Supabase/SupabaseGameRepository.ts' -import { getInheritDriver, getSessionDriver, isDevelopmentMode } from './env.ts' +import { getInheritDriver, getSessionDriver, getDevelopmentMode } from './env.ts' +import HttpGameRepository from '../src/Infrastructure/Http/HttpGameRepository.ts' const binds: DriverResolver = { [DriverType.json]: { @@ -22,7 +23,7 @@ const binds: DriverResolver = { }, [DriverType.http]: { AuthRepository: () => HttpAuthRepository.build(), - GameRepository: () => new InMemoryGameRepository(), + GameRepository: () => HttpGameRepository.build(), }, [DriverType.memory]: { AuthRepository: () => new InMemoryAuthRepository(), @@ -56,9 +57,10 @@ export default function () { container.register('AuthService', { useClass: AuthService }) let authDriver: Driver = getInheritDriver() - if (isDevelopmentMode()) { + const type = getDevelopmentMode() + if (type) { authDriver = { - type: DriverType.memory, + type: type, config: {} } } diff --git a/frontend/config/env.ts b/frontend/config/env.ts index 18b9b17..43ff5d8 100644 --- a/frontend/config/env.ts +++ b/frontend/config/env.ts @@ -1,8 +1,18 @@ import { getInitialSession, sessionStore } from '../view/stores/session.ts' -import { Driver } from '../src/Domain/Contracts.ts' +import { Driver, DriverType } from '../src/Domain/Contracts.ts' export const getInheritDriver = (): Driver => getInitialSession().driver export const getSessionDriver = (): Driver => sessionStore.state.driver -export const isDevelopmentMode = (): boolean => import.meta.env.VITE_DEVELOPMENT_MODE === 'true' +export const getDevelopmentMode = (): DriverType | undefined => { + const type = import.meta.env.VITE_DEVELOPMENT_DRIVER_TYPE + switch (type) { + case 'http': + return DriverType.http + case 'memory': + return DriverType.memory + default: + return undefined + } +} diff --git a/frontend/docker-compose.yml b/frontend/docker-compose.yml new file mode 100644 index 0000000..4addffc --- /dev/null +++ b/frontend/docker-compose.yml @@ -0,0 +1,11 @@ +services: + quiz-node: + container_name: quiz-node + image: node:20-alpine + working_dir: /var/www + volumes: + - .:/var/www + command: > + sh -c "npm install && npm run dev -- --host" + ports: + - "5173:5173" diff --git a/frontend/src/Infrastructure/Http/HttpGameRepository.ts b/frontend/src/Infrastructure/Http/HttpGameRepository.ts new file mode 100644 index 0000000..0e31802 --- /dev/null +++ b/frontend/src/Infrastructure/Http/HttpGameRepository.ts @@ -0,0 +1,33 @@ +import GameRepository from '../../Domain/Game/GameRepository.ts' +import Game from '../../Domain/Game/Game.ts' +import { HttpClientDriverContract } from '../Driver/Http/Contracts.ts' +import HttpClientFactory from '../Driver/Http/HttpClientFactory.ts' +import { Status } from '../../Domain/Contracts.ts' + +export default class HttpGameRepository implements GameRepository { + private http: HttpClientDriverContract + + constructor (factory: HttpClientFactory) { + this.http = factory.make() + } + + static build () { + return new this(new HttpClientFactory()) + } + + async findById (id: number | string): Promise { + const response = await this.http.get(`/api/v1/games/${id}`) + if (response.status !== Status.success) { + throw new Error(response.message) + } + return response.data as unknown as Game + } + + async paginate (page: number, limit: number): Promise { + const response = await this.http.get(`/api/v1/games?page=${page}&limit=${limit}`) + if (response.status !== Status.success) { + throw new Error(response.message) + } + return response.data as unknown as Game[] + } +} diff --git a/frontend/view/pages/public/auth/SignInPage.tsx b/frontend/view/pages/public/auth/SignInPage.tsx index b0d4577..ca10dc9 100644 --- a/frontend/view/pages/public/auth/SignInPage.tsx +++ b/frontend/view/pages/public/auth/SignInPage.tsx @@ -1,6 +1,6 @@ import { useNavigate } from 'react-router-dom' -import { isDevelopmentMode } from '../../../../config/env.ts' +import { getDevelopmentMode } from '../../../../config/env.ts' import { Session } from '../../../../src/Domain/Auth/Auth.ts' import { DriverType } from '../../../../src/Domain/Contracts.ts' @@ -20,9 +20,10 @@ export function SignInPage () { const { loading } = useLoading() const type = session.driver.type === DriverType.supabase ? 'otp' : 'password' - const initial = { - username: isDevelopmentMode() ? 'arretado@phpcomrapadura.org' : '', - password: isDevelopmentMode() ? '***************' : '' + const isDevelopment = !!getDevelopmentMode() + const initial: SignInData = { + username: isDevelopment ? 'arretado@phpcomrapadura.org' : '', + password: isDevelopment ? '***************' : '' } const { value,