Skip to content

Commit

Permalink
Move getTaskTypes, getProjects and getTemplates to factory pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
negreirosleo committed Jan 26, 2024
1 parent 9493bf9 commit 8a686e1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
5 changes: 3 additions & 2 deletions frontend/src/app/planner/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { getProjects } from '@/infra/project/getProjects'
import { makeGetProjects } from '@/infra/project/getProjects'
import { serverFetch } from '@/infra/lib/serverFetch'
import { ProjectSelection } from './components/ProjectSelection'

const getPageData = async () => {
const apiClient = await serverFetch()
const getProjects = makeGetProjects(apiClient)

return getProjects(apiClient)
return getProjects()
}

export default async function ProjectManagement() {
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/app/tasks/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { DayView } from './DayView'
import { getProjects } from '@/infra/project/getProjects'
import { getTaskTypes } from '@/infra/taskType/getTaskTypes'
import { makeGetProjects } from '@/infra/project/getProjects'
import { makeGetTaskTypes } from '@/infra/taskType/getTaskTypes'
import { makeGetTemplates } from '@/infra/template/getTemplates'
import { serverFetch } from '@/infra/lib/serverFetch'
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
import { getServerSession } from 'next-auth'
import { getTemplates } from '@/infra/template/getTemplates'

const getPageData = async () => {
const apiClient = await serverFetch()
const session = await getServerSession(authOptions)
const { id: userId } = session!.user

return await Promise.all([
getProjects(apiClient),
getTaskTypes(apiClient),
getTemplates(apiClient, { userId })
])
const getProjects = makeGetProjects(apiClient)
const getTaskTypes = makeGetTaskTypes(apiClient)
const getTemplates = makeGetTemplates(apiClient)

return await Promise.all([getProjects(), getTaskTypes(), getTemplates({ userId })])
}

export default async function Tasks() {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/infra/project/getProjects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiClient } from '@/infra/lib/apiClient'
import { Project } from '@/domain/Project'

export const getProjects = async (apiClient: ApiClient): Promise<Array<Project>> => {
export const makeGetProjects = (apiClient: ApiClient) => async (): Promise<Array<Project>> => {
const response = await apiClient('/v1/projects/')

if (!response.ok) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/infra/taskType/getTaskTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiClient } from '@/infra/lib/apiClient'
import { TaskType } from '@/domain/TaskType'

export const getTaskTypes = async (apiClient: ApiClient): Promise<Array<TaskType>> => {
export const makeGetTaskTypes = (apiClient: ApiClient) => async (): Promise<Array<TaskType>> => {
const response = await apiClient('/v1/timelog/task_types/')

if (!response.ok) {
Expand Down
26 changes: 13 additions & 13 deletions frontend/src/infra/template/getTemplates.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Template } from '@/domain/Template'
import { ApiClient } from '../lib/apiClient'
import { User } from '@/domain/User'

export const getTemplates = async (
apiClient: ApiClient,
{ userId }: { userId: number }
): Promise<Array<Template>> => {
const params = new URLSearchParams({ user_id: userId.toString() })
const response = await apiClient(`/v1/timelog/templates?${params}`, {
next: { tags: ['templates'] }
})
export const makeGetTemplates =
(apiClient: ApiClient) =>
async ({ userId }: { userId: User['id'] }): Promise<Array<Template>> => {
const params = new URLSearchParams({ user_id: userId.toString() })
const response = await apiClient(`/v1/timelog/templates?${params}`, {
next: { tags: ['templates'] }
})

if (!response.ok) {
throw new Error('Failed to fetch Templates')
}
if (!response.ok) {
throw new Error('Failed to fetch Templates')
}

return await response.json()
}
return await response.json()
}

0 comments on commit 8a686e1

Please sign in to comment.