koel/resources/assets/js/stores/userStore.ts

98 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-07-25 10:25:35 +00:00
import { differenceBy, merge } from 'lodash'
import { http } from '@/services'
2022-04-15 17:00:08 +00:00
import { reactive } from 'vue'
2022-06-10 10:47:46 +00:00
import { arrayify } from '@/utils'
2022-04-15 14:24:30 +00:00
interface UserFormData {
name: string
email: string
is_admin: boolean
}
export interface CreateUserData extends UserFormData {
password: string
}
export interface UpdateUserData extends UserFormData {
password?: string
}
export const userStore = {
2024-07-05 12:20:30 +00:00
vault: new Map<User['id'], User>(),
2022-06-10 10:47:46 +00:00
2022-04-15 17:00:08 +00:00
state: reactive({
2022-04-15 14:24:30 +00:00
users: [] as User[],
current: null! as User,
2022-04-15 17:00:08 +00:00
}),
2022-04-15 14:24:30 +00:00
2024-07-05 12:20:30 +00:00
syncWithVault (users: MaybeArray<User>) {
2022-06-10 10:47:46 +00:00
return arrayify(users).map(user => {
let local = this.byId(user.id)
2022-07-25 10:25:35 +00:00
local = reactive(local ? merge(local, user) : user)
2022-06-10 10:47:46 +00:00
this.vault.set(user.id, local)
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
return local
})
2022-04-15 14:24:30 +00:00
},
2022-07-25 10:25:35 +00:00
init (currentUser: User) {
this.state.users = this.syncWithVault(currentUser)
this.state.current = this.state.users[0]
2022-04-15 14:24:30 +00:00
},
2022-07-25 10:25:35 +00:00
async fetch () {
this.state.users = this.syncWithVault(await http.get<User[]>('users'))
2022-04-15 14:24:30 +00:00
},
2024-07-05 12:20:30 +00:00
byId (id: User['id']) {
2022-06-10 10:47:46 +00:00
return this.vault.get(id)
2022-04-15 14:24:30 +00:00
},
get current () {
return this.state.current
},
2022-04-21 22:51:48 +00:00
async store (data: CreateUserData) {
const user = await http.post<User>('users', data)
2023-08-20 22:35:58 +00:00
this.add(user)
2022-06-10 10:47:46 +00:00
return this.byId(user.id)
2022-04-15 14:24:30 +00:00
},
2024-07-05 12:20:30 +00:00
add (user: MaybeArray<User>) {
2023-08-20 22:35:58 +00:00
this.state.users.push(...this.syncWithVault(user))
},
2022-04-21 22:51:48 +00:00
async update (user: User, data: UpdateUserData) {
this.syncWithVault(await http.put<User>(`users/${user.id}`, data))
2022-04-15 14:24:30 +00:00
},
2022-04-21 22:51:48 +00:00
async destroy (user: User) {
await http.delete(`users/${user.id}`)
this.remove(user)
2022-04-15 14:24:30 +00:00
// Mama, just killed a man
// Put a gun against his head
// Pulled my trigger, now he's dead
// Mama, life had just begun
// But now I've gone and thrown it all away
// Mama, oooh
// Didn't mean to make you cry
// If I'm not back again this time tomorrow
// Carry on, carry on, as if nothing really matters
//
// Too late, my time has come
// Sends shivers down my spine
// Body's aching all the time
// Goodbye everybody - I've got to go
// Gotta leave you all behind and face the truth
// Mama, oooh
// I don't want to die
// I sometimes wish I'd never been born at all
},
remove (user: User) {
this.state.users = differenceBy(this.state.users, [user], 'id')
this.vault.delete(user.id)
},
2022-04-15 14:24:30 +00:00
}