users.ts
1.39 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
48
49
50
51
52
53
54
55
56
57
58
59
import { defineStore } from 'pinia'
import {
addUser,
type Filters,
getUsers,
Pagination,
removeUser,
Sorting,
updateUser,
uploadAvatar,
} from '../data/pages/users'
import { User } from '../pages/users/types'
export const useUsersStore = defineStore('users', {
state: () => {
return {
items: [] as User[],
pagination: { page: 1, perPage: 10, total: 0 },
}
},
actions: {
async getAll(options: { pagination?: Pagination; sorting?: Sorting; filters?: Partial<Filters> }) {
const { data, pagination } = await getUsers({
...options.filters,
...options.sorting,
...options.pagination,
})
this.items = data
this.pagination = pagination
},
async add(user: User) {
const [newUser] = await addUser(user)
this.items.unshift(newUser)
return newUser
},
async update(user: User) {
const [updatedUser] = await updateUser(user)
const index = this.items.findIndex(({ id }) => id === user.id)
this.items.splice(index, 1, updatedUser)
return updatedUser
},
async remove(user: User) {
const isRemoved = await removeUser(user)
if (isRemoved) {
const index = this.items.findIndex(({ id }) => id === user.id)
this.items.splice(index, 1)
}
},
async uploadAvatar(formData: FormData) {
return uploadAvatar(formData)
},
},
})