|
1 |
| -import { sleep } from '../../services/utils' |
2 |
| -import projectsDb from './projects-db.json' |
3 |
| -import usersDb from './users-db.json' |
| 1 | +import api from '../../services/api' |
| 2 | +import { Project } from '../../pages/projects/types' |
4 | 3 |
|
5 |
| -// Simulate API calls |
6 | 4 | export type Pagination = {
|
7 | 5 | page: number
|
8 | 6 | perPage: number
|
9 | 7 | total: number
|
10 | 8 | }
|
11 | 9 |
|
12 | 10 | export type Sorting = {
|
13 |
| - sortBy: keyof (typeof projectsDb)[number] | undefined |
| 11 | + sortBy: 'project_owner' | 'team' | 'created_at' |
14 | 12 | sortingOrder: 'asc' | 'desc' | null
|
15 | 13 | }
|
16 | 14 |
|
17 |
| -const getSortItem = (obj: any, sortBy: keyof (typeof projectsDb)[number]) => { |
18 |
| - if (sortBy === 'project_owner') { |
19 |
| - return obj.project_owner.fullname |
20 |
| - } |
21 |
| - |
22 |
| - if (sortBy === 'team') { |
23 |
| - return obj.team.map((user: any) => user.fullname).join(', ') |
24 |
| - } |
25 |
| - |
26 |
| - if (sortBy === 'creation_date') { |
27 |
| - return new Date(obj[sortBy]) |
28 |
| - } |
29 |
| - |
30 |
| - return obj[sortBy] |
31 |
| -} |
32 |
| - |
33 |
| -export const getProjects = async (options: Sorting & Pagination) => { |
34 |
| - await sleep(1000) |
35 |
| - |
36 |
| - const projects = projectsDb.map((project) => ({ |
37 |
| - ...project, |
38 |
| - project_owner: usersDb.find((user) => user.id === project.project_owner)! as (typeof usersDb)[number], |
39 |
| - team: usersDb.filter((user) => project.team.includes(user.id)) as (typeof usersDb)[number][], |
40 |
| - })) |
41 |
| - |
42 |
| - if (options.sortBy && options.sortingOrder) { |
43 |
| - projects.sort((a, b) => { |
44 |
| - a = getSortItem(a, options.sortBy!) |
45 |
| - b = getSortItem(b, options.sortBy!) |
46 |
| - if (a < b) { |
47 |
| - return options.sortingOrder === 'asc' ? -1 : 1 |
48 |
| - } |
49 |
| - if (a > b) { |
50 |
| - return options.sortingOrder === 'asc' ? 1 : -1 |
51 |
| - } |
52 |
| - return 0 |
53 |
| - }) |
54 |
| - } |
55 |
| - |
56 |
| - const normalizedProjects = projects.slice((options.page - 1) * options.perPage, options.page * options.perPage) |
| 15 | +export const getProjects = async (options: Partial<Sorting> & Pagination) => { |
| 16 | + const projects: Project[] = await fetch(api.allProjects()).then((r) => r.json()) |
57 | 17 |
|
58 | 18 | return {
|
59 |
| - data: normalizedProjects, |
| 19 | + data: projects, |
60 | 20 | pagination: {
|
61 | 21 | page: options.page,
|
62 | 22 | perPage: options.perPage,
|
63 |
| - total: projectsDb.length, |
| 23 | + total: projects.length, |
64 | 24 | },
|
65 | 25 | }
|
66 | 26 | }
|
67 | 27 |
|
68 |
| -export const addProject = async (project: Omit<(typeof projectsDb)[number], 'id' | 'creation_date'>) => { |
69 |
| - await sleep(1000) |
70 |
| - |
71 |
| - const newProject = { |
72 |
| - ...project, |
73 |
| - id: projectsDb.length + 1, |
74 |
| - creation_date: new Date().toLocaleDateString('gb', { day: 'numeric', month: 'short', year: 'numeric' }), |
75 |
| - } |
| 28 | +export const addProject = async (project: Omit<Project, 'id' | 'created_at'>) => { |
| 29 | + const headers = new Headers() |
| 30 | + headers.append('Content-Type', 'application/json') |
76 | 31 |
|
77 |
| - projectsDb.push(newProject) |
78 |
| - |
79 |
| - return { |
80 |
| - ...newProject, |
81 |
| - project_owner: usersDb.find((user) => user.id === project.project_owner)! as (typeof usersDb)[number], |
82 |
| - team: usersDb.filter((user) => project.team.includes(user.id)) as (typeof usersDb)[number][], |
83 |
| - } |
| 32 | + return fetch(api.allProjects(), { method: 'POST', body: JSON.stringify(project), headers }).then((r) => r.json()) |
84 | 33 | }
|
85 | 34 |
|
86 |
| -export const updateProject = async (project: (typeof projectsDb)[number]) => { |
87 |
| - await sleep(1000) |
88 |
| - |
89 |
| - const index = projectsDb.findIndex((p) => p.id === project.id) |
90 |
| - projectsDb[index] = project |
91 |
| - |
92 |
| - return project |
| 35 | +export const updateProject = async (project: Omit<Project, 'created_at'>) => { |
| 36 | + const headers = new Headers() |
| 37 | + headers.append('Content-Type', 'application/json') |
| 38 | + return fetch(api.project(project.id), { method: 'PUT', body: JSON.stringify(project), headers }).then((r) => r.json()) |
93 | 39 | }
|
94 | 40 |
|
95 |
| -export const removeProject = async (project: (typeof projectsDb)[number]) => { |
96 |
| - await sleep(1000) |
97 |
| - |
98 |
| - const index = projectsDb.findIndex((p) => p.id === project.id) |
99 |
| - projectsDb.splice(index, 1) |
100 |
| - |
101 |
| - return project |
| 41 | +export const removeProject = async (project: Project) => { |
| 42 | + return fetch(api.project(project.id), { method: 'DELETE' }) |
102 | 43 | }
|
0 commit comments