projects.ts
1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import api from '../../services/api'
import { Project } from '../../pages/projects/types'
export type Pagination = {
page: number
perPage: number
total: number
}
export type Sorting = {
sortBy: 'project_owner' | 'team' | 'created_at'
sortingOrder: 'asc' | 'desc' | null
}
export const getProjects = async (options: Partial<Sorting> & Pagination) => {
const projects: Project[] = await fetch(api.allProjects()).then((r) => r.json())
return {
data: projects,
pagination: {
page: options.page,
perPage: options.perPage,
total: projects.length,
},
}
}
export const addProject = async (project: Omit<Project, 'id' | 'created_at'>) => {
const headers = new Headers()
headers.append('Content-Type', 'application/json')
return fetch(api.allProjects(), { method: 'POST', body: JSON.stringify(project), headers }).then((r) => r.json())
}
export const updateProject = async (project: Omit<Project, 'created_at'>) => {
const headers = new Headers()
headers.append('Content-Type', 'application/json')
return fetch(api.project(project.id), { method: 'PUT', body: JSON.stringify(project), headers }).then((r) => r.json())
}
export const removeProject = async (project: Project) => {
return fetch(api.project(project.id), { method: 'DELETE' })
}