feat: Implement Fast Request feature with file upload and request management
This commit is contained in:
@@ -1,22 +1,104 @@
|
||||
import { step } from '@primeuix/themes/aura/stepper'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
const emptyCase = () => ({
|
||||
petId: null,
|
||||
name: null,
|
||||
type: null,
|
||||
gender: null,
|
||||
breed: null,
|
||||
age: null,
|
||||
weight: null,
|
||||
sterilized: null,
|
||||
})
|
||||
|
||||
export const useFastRequestStore = defineStore('FastRequestStore', {
|
||||
state: () => ({
|
||||
endpoints: {
|
||||
create: '/dashboard/fast-requests/create',
|
||||
store: '/dashboard/fast-requests',
|
||||
},
|
||||
step: 1,
|
||||
requestType: null,
|
||||
requestCase: {
|
||||
name: null,
|
||||
species: null,
|
||||
gender: null,
|
||||
age: null,
|
||||
weight: null,
|
||||
neutered: null,
|
||||
requestCase: emptyCase(),
|
||||
requestDetails: {
|
||||
priority: null,
|
||||
note: null,
|
||||
},
|
||||
requestDetails: null,
|
||||
requestDoctor: null,
|
||||
requestReview: null,
|
||||
historyFiles: [],
|
||||
scanFiles: [],
|
||||
doctorId: null,
|
||||
pets: [],
|
||||
doctors: [],
|
||||
submitting: false,
|
||||
submittedDocument: null,
|
||||
}),
|
||||
getters: {
|
||||
selectedPet: (state) => state.pets.find((pet) => pet.id === state.requestCase.petId) || null,
|
||||
selectedDoctor: (state) => state.doctors.find((doctor) => doctor.id === state.doctorId) || null,
|
||||
canProceed: (state) => {
|
||||
switch (state.step) {
|
||||
case 1:
|
||||
return !!state.requestType
|
||||
case 2:
|
||||
return !!state.requestCase.petId || (!!state.requestCase.name && !!state.requestCase.type)
|
||||
case 3:
|
||||
return !!state.requestDetails.priority
|
||||
default:
|
||||
return true
|
||||
}
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
async fetchCreateData() {
|
||||
const { data } = await window.axios.get(this.endpoints.create)
|
||||
this.pets = data.pets
|
||||
this.doctors = data.doctors
|
||||
},
|
||||
selectArchivePet(petId) {
|
||||
this.requestCase = emptyCase()
|
||||
this.requestCase.petId = petId
|
||||
},
|
||||
clearArchivePet() {
|
||||
this.requestCase.petId = null
|
||||
},
|
||||
nextStep() {
|
||||
if (this.step < 5) this.step++
|
||||
},
|
||||
prevStep() {
|
||||
if (this.step > 1) this.step--
|
||||
},
|
||||
async submit() {
|
||||
this.submitting = true
|
||||
try {
|
||||
const formData = new FormData()
|
||||
formData.append('modality', this.requestType)
|
||||
formData.append('priority', this.requestDetails.priority)
|
||||
if (this.requestDetails.note) formData.append('note', this.requestDetails.note)
|
||||
|
||||
}
|
||||
if (this.requestCase.petId) {
|
||||
formData.append('pet_id', this.requestCase.petId)
|
||||
} else {
|
||||
Object.entries(this.requestCase).forEach(([key, value]) => {
|
||||
if (key !== 'petId' && value !== null && value !== '') {
|
||||
formData.append(`pet[${key}]`, key === 'sterilized' ? (value ? 1 : 0) : value)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (this.doctorId) formData.append('doctor_id', this.doctorId)
|
||||
|
||||
this.historyFiles.forEach((file) => formData.append('history_files[]', file))
|
||||
this.scanFiles.forEach((file) => formData.append('scan_files[]', file))
|
||||
|
||||
const { data } = await window.axios.post(this.endpoints.store, formData)
|
||||
this.submittedDocument = data.document
|
||||
return data.document
|
||||
} finally {
|
||||
this.submitting = false
|
||||
}
|
||||
},
|
||||
reset() {
|
||||
this.$reset()
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user