diff --git a/src/data/pages/projects.ts b/src/data/pages/projects.ts index 2e193652e..87f1b98f2 100644 --- a/src/data/pages/projects.ts +++ b/src/data/pages/projects.ts @@ -1,6 +1,8 @@ -import api from '../../services/api' -import { Project } from '../../pages/projects/types' +import { sleep } from '../../services/utils' +import projectsDb from './projects-db.json' +import usersDb from './users-db.json' +// Simulate API calls export type Pagination = { page: number perPage: number @@ -8,11 +10,11 @@ export type Pagination = { } export type Sorting = { - sortBy: 'project_owner' | 'team' | 'created_at' + sortBy: keyof (typeof projectsDb)[number] | undefined sortingOrder: 'asc' | 'desc' | null } -const getSortItem = (obj: any, sortBy: Sorting['sortBy']) => { +const getSortItem = (obj: any, sortBy: keyof (typeof projectsDb)[number]) => { if (sortBy === 'project_owner') { return obj.project_owner.fullname } @@ -21,15 +23,21 @@ const getSortItem = (obj: any, sortBy: Sorting['sortBy']) => { return obj.team.map((user: any) => user.fullname).join(', ') } - if (sortBy === 'created_at') { + if (sortBy === 'creation_date') { return new Date(obj[sortBy]) } return obj[sortBy] } -export const getProjects = async (options: Partial & Pagination) => { - const projects: Project[] = await fetch(api.allProjects()).then((r) => r.json()) +export const getProjects = async (options: Sorting & Pagination) => { + await sleep(1000) + + const projects = projectsDb.map((project) => ({ + ...project, + project_owner: usersDb.find((user) => user.id === project.project_owner)! as (typeof usersDb)[number], + team: usersDb.filter((user) => project.team.includes(user.id)) as (typeof usersDb)[number][], + })) if (options.sortBy && options.sortingOrder) { projects.sort((a, b) => { @@ -52,24 +60,43 @@ export const getProjects = async (options: Partial & Pagination) => { pagination: { page: options.page, perPage: options.perPage, - total: projects.length, + total: projectsDb.length, }, } } -export const addProject = async (project: Omit) => { - const headers = new Headers() - headers.append('Content-Type', 'application/json') +export const addProject = async (project: Omit<(typeof projectsDb)[number], 'id' | 'creation_date'>) => { + await sleep(1000) + + const newProject = { + ...project, + id: projectsDb.length + 1, + creation_date: new Date().toLocaleDateString('gb', { day: 'numeric', month: 'short', year: 'numeric' }), + } - return fetch(api.allProjects(), { method: 'POST', body: JSON.stringify(project), headers }).then((r) => r.json()) + projectsDb.push(newProject) + + return { + ...newProject, + project_owner: usersDb.find((user) => user.id === project.project_owner)! as (typeof usersDb)[number], + team: usersDb.filter((user) => project.team.includes(user.id)) as (typeof usersDb)[number][], + } } -export const updateProject = async (project: Omit) => { - 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 updateProject = async (project: (typeof projectsDb)[number]) => { + await sleep(1000) + + const index = projectsDb.findIndex((p) => p.id === project.id) + projectsDb[index] = project + + return project } -export const removeProject = async (project: Project) => { - return fetch(api.project(project.id), { method: 'DELETE' }) +export const removeProject = async (project: (typeof projectsDb)[number]) => { + await sleep(1000) + + const index = projectsDb.findIndex((p) => p.id === project.id) + projectsDb.splice(index, 1) + + return project } diff --git a/src/data/pages/users.ts b/src/data/pages/users.ts index b04fa2abf..a8e9d5134 100644 --- a/src/data/pages/users.ts +++ b/src/data/pages/users.ts @@ -27,12 +27,12 @@ const getSortItem = (obj: any, sortBy: string) => { export const getUsers = async (filters: Partial) => { const { isActive, search, sortBy, sortingOrder } = filters - let filteredUsers: User[] = await fetch(api.allUsers()).then((r) => r.json()) + let filteredUsers: User[] = await fetch(api.getAllUsers()).then((r) => r.json()) - filteredUsers = filteredUsers.filter((user) => user.active === isActive) + filteredUsers = filteredUsers.filter((user) => user.isActive === isActive) if (search) { - filteredUsers = filteredUsers.filter((user) => user.fullname.toLowerCase().includes(search.toLowerCase())) + filteredUsers = filteredUsers.filter((user) => user.fullName.toLowerCase().includes(search.toLowerCase())) } if (sortBy && sortingOrder) { @@ -64,15 +64,15 @@ export const addUser = async (user: User) => { const headers = new Headers() headers.append('Content-Type', 'application/json') - return fetch(api.allUsers(), { method: 'POST', body: JSON.stringify(user), headers }).then((r) => r.json()) + return fetch(api.getAllUsers(), { method: 'POST', body: JSON.stringify(user), headers }) } export const updateUser = async (user: User) => { const headers = new Headers() headers.append('Content-Type', 'application/json') - return fetch(api.user(user.id), { method: 'PUT', body: JSON.stringify(user), headers }).then((r) => r.json()) + return fetch(api.getUser(user.id), { method: 'PUT', body: JSON.stringify(user), headers }) } export const removeUser = async (user: User) => { - return fetch(api.user(user.id), { method: 'DELETE' }) + return fetch(api.getUser(user.id), { method: 'DELETE' }) } diff --git a/src/pages/admin/dashboard/cards/ProjectTable.vue b/src/pages/admin/dashboard/cards/ProjectTable.vue index 5296b4fa5..b29f2a137 100644 --- a/src/pages/admin/dashboard/cards/ProjectTable.vue +++ b/src/pages/admin/dashboard/cards/ProjectTable.vue @@ -1,11 +1,11 @@