Refactored employee-Details to fit store and fetch / persist logic into pinia store. Added a clean state to reset to.
This commit is contained in:
123
src/stores/employee.ts
Normal file
123
src/stores/employee.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import { defineStore, acceptHMRUpdate } from 'pinia'
|
||||
import axios from '@/axios'
|
||||
import { useUser } from './user'
|
||||
import { useNotifications } from './notifications'
|
||||
|
||||
type ResultData = {
|
||||
employee: {
|
||||
id: number,
|
||||
firstName: string,
|
||||
lastName: string,
|
||||
shorthand: string,
|
||||
phone: string,
|
||||
mobile: string,
|
||||
email: string,
|
||||
contractHours: number,
|
||||
hasUser: boolean,
|
||||
},
|
||||
user?: {
|
||||
id?: number,
|
||||
username?: string
|
||||
}
|
||||
}
|
||||
|
||||
const user = useUser()
|
||||
const notifications = useNotifications()
|
||||
|
||||
export const useEmployee = defineStore({
|
||||
id: 'employee',
|
||||
|
||||
state: () => {
|
||||
return {
|
||||
clean: {
|
||||
employee: {
|
||||
id: NaN,
|
||||
firstName: '',
|
||||
lastName: '',
|
||||
shorthand: '',
|
||||
phone: '',
|
||||
mobile: '',
|
||||
email: '',
|
||||
contractHours: NaN,
|
||||
hasUser: false
|
||||
},
|
||||
user: {
|
||||
id: NaN,
|
||||
username: '',
|
||||
password: '',
|
||||
passwordConfirm: '',
|
||||
}
|
||||
},
|
||||
|
||||
employee: {
|
||||
id: NaN,
|
||||
firstName: '',
|
||||
lastName: '',
|
||||
shorthand: '',
|
||||
phone: '',
|
||||
mobile: '',
|
||||
email: '',
|
||||
contractHours: NaN,
|
||||
hasUser: false
|
||||
},
|
||||
|
||||
user: {
|
||||
id: NaN,
|
||||
username: '',
|
||||
password: '',
|
||||
passwordConfirm: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
async fetchFromApi(id: string | string[]) {
|
||||
|
||||
try {
|
||||
const data : ResultData = await <ResultData>(await axios.get('employees/'+id, {
|
||||
headers: {
|
||||
'Authorization': 'Bearer '+user.token
|
||||
}
|
||||
})).data
|
||||
|
||||
Object.assign(this.clean.employee, data.employee)
|
||||
Object.assign(this.clean.user, data.user)
|
||||
Object.assign(this.employee, data.employee),
|
||||
Object.assign(this.user, data.user)
|
||||
}
|
||||
catch(err){
|
||||
if(err instanceof Error) notifications.add('danger', err.message, -1)
|
||||
else console.log(err)
|
||||
}
|
||||
},
|
||||
|
||||
reset() {
|
||||
Object.assign(this.user, this.clean.user)
|
||||
Object.assign(this.employee, this.clean.employee)
|
||||
},
|
||||
|
||||
/** TODO: #23 Persist user if password is changed */
|
||||
async persist() {
|
||||
try {
|
||||
const result = await axios.patch('employees/'+this.employee.id, this.employee,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': 'Bearer '+user.token
|
||||
}
|
||||
}
|
||||
)
|
||||
Object.assign(this.clean.employee, this.employee)
|
||||
Object.assign(this.clean.user, this.user)
|
||||
notifications.add('success', result.statusText)
|
||||
}
|
||||
catch(error) {
|
||||
if(error instanceof Error) notifications.add('danger', error.message, -1)
|
||||
else console.log(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if(import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useEmployee, import.meta.hot))
|
||||
}
|
||||
Reference in New Issue
Block a user