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

110 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-06-10 10:47:46 +00:00
import { difference } from 'lodash'
2022-04-24 08:50:45 +00:00
import { httpService } 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
export interface UpdateCurrentProfileData {
2022-04-21 22:51:48 +00:00
current_password: string | null
2022-04-15 14:24:30 +00:00
name: string
email: string
2022-06-10 10:47:46 +00:00
avatar?: string
2022-04-15 14:24:30 +00:00
new_password?: string
}
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 = {
2022-06-10 10:47:46 +00:00
vault: new Map<number, User>(),
2022-04-15 17:00:08 +00:00
state: reactive({
2022-04-15 14:24:30 +00:00
users: [] as User[],
2022-06-10 10:47:46 +00:00
current: null as unknown as User
2022-04-15 17:00:08 +00:00
}),
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
syncWithVault (users: User | User[]) {
return arrayify(users).map(user => {
let local = this.byId(user.id)
local = reactive(local ? Object.assign(local, user) : user)
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-06-10 10:47:46 +00:00
async fetch () {
this.state.users = this.syncWithVault(await httpService.get<User[]>('users'))
2022-04-15 14:24:30 +00:00
},
2022-06-10 10:47:46 +00:00
init (currentUser: User) {
this.current = currentUser
this.state.users = this.syncWithVault(this.current)
2022-04-15 14:24:30 +00:00
},
2022-04-21 22:51:48 +00:00
byId (id: number) {
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
},
set current (user: User) {
this.state.current = user
},
2022-04-24 08:50:45 +00:00
login: async (email: string, password: string) => await httpService.post<User>('me', { email, password }),
logout: async () => await httpService.delete('me'),
getProfile: async () => await httpService.get<User>('me'),
2022-04-15 14:24:30 +00:00
2022-04-21 22:51:48 +00:00
async updateProfile (data: UpdateCurrentProfileData) {
2022-06-10 10:47:46 +00:00
Object.assign(this.current, (await httpService.put<User>('me', data)))
2022-04-15 14:24:30 +00:00
},
2022-04-21 22:51:48 +00:00
async store (data: CreateUserData) {
2022-04-24 08:50:45 +00:00
const user = await httpService.post<User>('user', data)
2022-06-10 10:47:46 +00:00
this.state.users.push(...this.syncWithVault(user))
return this.byId(user.id)
2022-04-15 14:24:30 +00:00
},
2022-04-21 22:51:48 +00:00
async update (user: User, data: UpdateUserData) {
2022-06-10 10:47:46 +00:00
this.syncWithVault(await httpService.put<User>(`user/${user.id}`, data))
2022-04-15 14:24:30 +00:00
},
2022-04-21 22:51:48 +00:00
async destroy (user: User) {
2022-04-24 08:50:45 +00:00
await httpService.delete(`user/${user.id}`)
2022-06-10 10:47:46 +00:00
this.state.users = difference(this.state.users, [user])
this.vault.delete(user.id)
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
}
}