Skip to content

Commit

Permalink
feat(front-end): add task response type
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jun 14, 2023
1 parent 3e6b06f commit 3a6713a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
20 changes: 10 additions & 10 deletions apps/frontend/src/api/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { http } from './http'
import type { TaskResponse } from './types'
import type { Task } from '@/store'
import { TaskStatus } from '@/store'

export function fetchAllTasks({ pId, status }: { pId?: string; status?: TaskStatus }) {
return http.get('/tasks', {
return http.get<TaskResponse[], TaskResponse[]>('/tasks', {
params: {
projectId: pId,
status,
Expand All @@ -12,49 +13,48 @@ export function fetchAllTasks({ pId, status }: { pId?: string; status?: TaskStat
}

export function fetchCreateTask(title: string, pId: string) {
return http.post('/tasks', {
return http.post<TaskResponse, TaskResponse>('/tasks', {
title,
projectId: pId,
})
}

export function fetchUpdateTaskTitle(taskId: Task['id'], title: string) {
return http.patch(`/tasks/${taskId}`, {
return http.patch<TaskResponse>(`/tasks/${taskId}`, {
title,
})
}
export function fetchUpdateTaskContent(taskId: Task['id'], content: string) {
return http.patch(`/tasks/${taskId}`, {
return http.patch<TaskResponse>(`/tasks/${taskId}`, {
content,
})
}

export function fetchCompleteTask(taskId: Task['id']) {
return http.patch(`/tasks/${taskId}`, {
return http.patch<TaskResponse>(`/tasks/${taskId}`, {
status: TaskStatus.COMPLETED,
})
}

export function fetchRestoreTask(taskId: Task['id']) {
return http.patch(`/tasks/${taskId}`, {
return http.patch<TaskResponse>(`/tasks/${taskId}`, {
status: TaskStatus.ACTIVE,
})
}

export function fetchMoveTaskToProject(taskId: Task['id'], projectId: string) {
return http.patch(`/tasks/${taskId}`, {
return http.patch<TaskResponse>(`/tasks/${taskId}`, {
projectId,
})
}

export function fetchRemoveTask(taskId: Task['id']) {
return http.patch(`/tasks/${taskId}`, {
return http.patch<TaskResponse>(`/tasks/${taskId}`, {
status: TaskStatus.REMOVED,
})
}

export function fetchUpdateTaskPosition(taskId: Task['id'], position: number) {
return http.patch(`/tasks/${taskId}`, {
return http.patch<TaskResponse>(`/tasks/${taskId}`, {
position,
})
}
12 changes: 12 additions & 0 deletions apps/frontend/src/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { TaskStatus } from '@/store/tasks'

export interface TaskResponse {
title: string
content: string
status: TaskStatus
projectId: string
position: number
_id: string
created_at: string
updated_at: string
}
6 changes: 3 additions & 3 deletions apps/frontend/src/components/command/searchTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export async function searchTasks(input: string) {
const tasksStore = useTasksStore()
const projectsStore = useListProjectsStore()

const rawTasks = await tasksStore.findAllTasksNotRemoved()
const tasks = rawTasks.map((task) => {
const tasks = await tasksStore.findAllTasksNotRemoved()
const fuseTasks = tasks.map((task) => {
const done = task.status === TaskStatus.COMPLETED
const from = done ? completeSmartProject : projectsStore.findProject(task.projectId)
return {
Expand All @@ -32,7 +32,7 @@ export async function searchTasks(input: string) {
from,
}
})
fuse.setCollection(tasks)
fuse.setCollection(fuseTasks)

filteredTasks.value = fuse.search(input)
}
Expand Down
18 changes: 8 additions & 10 deletions apps/frontend/src/store/listProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ export const useListProjectsStore = defineStore('newProjects', () => {

if (typeof projectOrNameOrId === 'string')
project = findProject(projectOrNameOrId)

else
project = projectOrNameOrId
else project = projectOrNameOrId

if (project)
tasksSelectorStore.setCurrentSelector(project)
}

function findProject(projectIdOrName: string): ListProject | undefined {
return projects.value.find(p => p.name === projectIdOrName || p.id === projectIdOrName)
return projects.value.find(
p => p.name === projectIdOrName || p.id === projectIdOrName,
)
}

async function createProject(name: string) {
Expand Down Expand Up @@ -80,12 +80,10 @@ function normalizeProject(rawProject: any): ListProject {
}

export async function loadListProjectTasks(pId: string) {
const rawTasks = await fetchAllTasks(
{
pId,
status: TaskStatus.ACTIVE,
},
)
const rawTasks = await fetchAllTasks({
pId,
status: TaskStatus.ACTIVE,
})

return rawTasks
}
16 changes: 8 additions & 8 deletions apps/frontend/src/store/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
fetchUpdateTaskTitle,
} from '@/api'
import { useTasksSelectorStore } from '@/store'
import type { TaskResponse } from '@/api/types'

export enum TaskStatus {
ACTIVE = 'active',
Expand All @@ -34,8 +35,8 @@ export const useTasksStore = defineStore('tasksStore', () => {
const tasks = ref<Task[]>([])
const currentActiveTask = ref<Task>()

function updateTasks(rawTasks: any) {
tasks.value = rawTasks.map(normalizeTask)
function updateTasks(rawTasks: TaskResponse[]) {
tasks.value = rawTasks.map(mapTaskResponseToTask)
}

async function addTask(title: string) {
Expand All @@ -45,7 +46,7 @@ export const useTasksStore = defineStore('tasksStore', () => {
return

const newRawTask = await fetchCreateTask(title, tasksSelectorStore.currentSelector.id)
const task = normalizeTask(newRawTask)
const task = mapTaskResponseToTask(newRawTask)
tasks.value.unshift(task)
changeActiveTask(task)
}
Expand Down Expand Up @@ -136,10 +137,10 @@ export const useTasksStore = defineStore('tasksStore', () => {
}

async function findAllTasksNotRemoved() {
const activeTasks: any = await fetchAllTasks({ status: TaskStatus.ACTIVE })
const completedTasks: any = await fetchAllTasks({ status: TaskStatus.COMPLETED })
const activeTasks = await fetchAllTasks({ status: TaskStatus.ACTIVE })
const completedTasks = await fetchAllTasks({ status: TaskStatus.COMPLETED })

return [...activeTasks.map(normalizeTask), ...completedTasks.map(normalizeTask)]
return [...activeTasks.map(mapTaskResponseToTask), ...completedTasks.map(mapTaskResponseToTask)]
}

async function moveTaskToProject(task: Task, projectId: string) {
Expand Down Expand Up @@ -167,8 +168,7 @@ export const useTasksStore = defineStore('tasksStore', () => {
}
})

// TODO 这里的 any 后面应该改成后端返回来的接口shape
function normalizeTask(rawTask: any): Task {
function mapTaskResponseToTask(rawTask: TaskResponse): Task {
return {
id: rawTask._id,
title: rawTask.title,
Expand Down
7 changes: 2 additions & 5 deletions apps/frontend/src/store/tasksSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ export const useTasksSelectorStore = defineStore('tasksSelectorStore', () => {
if (!currentSelector.value)
return

let rawTasks
if (currentSelector.value.type === 'listProject')
rawTasks = await loadListProjectTasks(currentSelector.value.id)
tasksStore.updateTasks(await loadListProjectTasks(currentSelector.value.id))

else if (currentSelector.value.type === 'smartProject')
rawTasks = await loadSmartProjectTasks(currentSelector.value.name)

tasksStore.updateTasks(rawTasks)
tasksStore.updateTasks(await loadSmartProjectTasks(currentSelector.value.name))
}

async function setCurrentSelector(selector: TasksSelector) {
Expand Down

0 comments on commit 3a6713a

Please sign in to comment.