Employees-Index: Create new User.

This commit is contained in:
Sockenklaus
2021-11-17 03:35:49 +01:00
parent 8071e8ef91
commit 6f237ff8fa
12 changed files with 408 additions and 347 deletions

View File

@@ -2,6 +2,7 @@ import { defineStore, acceptHMRUpdate } from 'pinia'
import axios from '@/axios'
import { useUser } from './user'
import { useNotifications } from './notifications'
import Axios from 'axios'
const user = useUser()
const notifications = useNotifications()
@@ -12,7 +13,7 @@ export const useEmployee = defineStore({
state: () => {
return {
clean: {
/** @type Employee */
/** @type { Employee } */
employee: {
id: NaN,
firstName: '',
@@ -25,7 +26,8 @@ export const useEmployee = defineStore({
isActive: false,
username: '',
password: '',
passwordConfirm: ''
passwordConfirm: '',
role: ''
},
},
@@ -42,8 +44,12 @@ export const useEmployee = defineStore({
isActive: false,
username: '',
password: '',
passwordConfirm: ''
passwordConfirm: '',
role: ''
},
/** @type { EmployeeApiValidationError[] } */
apiValidationErrors: Array<EmployeeApiValidationError>(),
}
},
@@ -77,8 +83,17 @@ export const useEmployee = defineStore({
Object.assign(this.employee, this.clean.employee)
},
/** TODO: #23 Persist user if password is changed */
async persist() {
if(this.employee.id){
this.patchEmployee()
}
else {
this.postEmployee()
}
},
async patchEmployee() {
try {
let result
let payload = Object.assign({}, this.employee)
@@ -109,7 +124,35 @@ export const useEmployee = defineStore({
}
else console.log(error)
}
}
},
async postEmployee() {
let result
try {
result = await axios.post<Employee>(
'employees',
this.employee,
{
headers: user.header
}
)
this.assignTruthyValues(this.employee, result.data)
this.assignTruthyValues(this.clean.employee, result.data)
notifications.add('success', result.statusText)
console.log(result)
}
catch(error){
console.log("enter catch")
if(Axios.isAxiosError(error)) {
console.log("enter axios error")
let data = error.response?.data as { errors: EmployeeApiValidationError[] }
console.log(data)
this.apiValidationErrors = [...data.errors]
}
}
},
},
getters: {