projects.ts
1.31 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
44
45
46
47
import { defineStore } from 'pinia'
import { addProject, getProjects, Pagination, removeProject, Sorting, updateProject } from '../data/pages/projects'
import { Project } from '../pages/projects/types'
export const useProjectsStore = defineStore('projects', {
state: () => {
return {
items: [] as Project[],
pagination: {
page: 1,
perPage: 10,
total: 0,
} as Pagination,
}
},
actions: {
async getAll(options: { pagination: Pagination; sorting?: Sorting }) {
const { data, pagination } = await getProjects({
...options.sorting,
...options.pagination,
})
this.items = data
this.pagination = pagination
},
async add(project: Omit<Project, 'id' | 'created_at'>) {
const [newProject] = await addProject(project)
this.items.push(newProject)
},
async update(project: Project) {
const [updatedProject] = await updateProject(project)
const index = this.items.findIndex(({ id }) => id === project.id)
this.items.splice(index, 1, updatedProject)
},
async remove(project: Project) {
const isRemoved = await removeProject(project)
if (isRemoved) {
const index = this.items.findIndex(({ id }) => id === project.id)
this.items.splice(index, 1)
}
},
},
})