Changes to post|put|patch|delete
This commit is contained in:
@@ -3,7 +3,7 @@ import { ref, watch } from 'vue';
|
||||
import { useForm, Field as FormField } from "vee-validate";
|
||||
import { toTypedSchema } from "@vee-validate/zod";
|
||||
import * as z from "zod";
|
||||
import axios from 'axios';
|
||||
import { router } from '@inertiajs/vue3';
|
||||
import CreateDialog from '../Dialogs/CreateDialog.vue';
|
||||
import UpdateDialog from '../Dialogs/UpdateDialog.vue';
|
||||
import SectionTitle from '../SectionTitle.vue';
|
||||
@@ -95,26 +95,32 @@ const create = async () => {
|
||||
errors.value = {};
|
||||
const { values } = form;
|
||||
|
||||
try {
|
||||
const { data } = await axios.post(route('person.trr.create', props.person), values);
|
||||
if (!Array.isArray(props.person.trrs)) props.person.trrs = (props.person.bank_accounts || props.person.accounts || props.person.bankAccounts || []);
|
||||
(props.person.trrs).push(data.trr);
|
||||
processing.value = false;
|
||||
close();
|
||||
resetForm();
|
||||
} catch (e) {
|
||||
errors.value = e?.response?.data?.errors || {};
|
||||
// Map axios errors to VeeValidate field errors
|
||||
if (errors.value) {
|
||||
Object.keys(errors.value).forEach((field) => {
|
||||
const errorMessages = Array.isArray(errors.value[field])
|
||||
? errors.value[field]
|
||||
: [errors.value[field]];
|
||||
form.setFieldError(field, errorMessages[0]);
|
||||
});
|
||||
router.post(
|
||||
route('person.trr.create', props.person),
|
||||
values,
|
||||
{
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
processing.value = false;
|
||||
close();
|
||||
resetForm();
|
||||
},
|
||||
onError: (inertiaErrors) => {
|
||||
errors.value = inertiaErrors || {};
|
||||
// Map Inertia errors to VeeValidate field errors
|
||||
Object.keys(errors.value).forEach((field) => {
|
||||
const errorMessages = Array.isArray(errors.value[field])
|
||||
? errors.value[field]
|
||||
: [errors.value[field]];
|
||||
form.setFieldError(field, errorMessages[0]);
|
||||
});
|
||||
processing.value = false;
|
||||
},
|
||||
onFinish: () => {
|
||||
processing.value = false;
|
||||
},
|
||||
}
|
||||
processing.value = false;
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
const update = async () => {
|
||||
@@ -122,27 +128,32 @@ const update = async () => {
|
||||
errors.value = {};
|
||||
const { values } = form;
|
||||
|
||||
try {
|
||||
const { data } = await axios.put(route('person.trr.update', { person: props.person, trr_id: props.id }), values);
|
||||
let list = props.person.trrs || props.person.bank_accounts || props.person.accounts || props.person.bankAccounts || [];
|
||||
const idx = list.findIndex(a => a.id === data.trr.id);
|
||||
if (idx !== -1) list[idx] = data.trr;
|
||||
processing.value = false;
|
||||
close();
|
||||
resetForm();
|
||||
} catch (e) {
|
||||
errors.value = e?.response?.data?.errors || {};
|
||||
// Map axios errors to VeeValidate field errors
|
||||
if (errors.value) {
|
||||
Object.keys(errors.value).forEach((field) => {
|
||||
const errorMessages = Array.isArray(errors.value[field])
|
||||
? errors.value[field]
|
||||
: [errors.value[field]];
|
||||
form.setFieldError(field, errorMessages[0]);
|
||||
});
|
||||
router.put(
|
||||
route('person.trr.update', { person: props.person, trr_id: props.id }),
|
||||
values,
|
||||
{
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
processing.value = false;
|
||||
close();
|
||||
resetForm();
|
||||
},
|
||||
onError: (inertiaErrors) => {
|
||||
errors.value = inertiaErrors || {};
|
||||
// Map Inertia errors to VeeValidate field errors
|
||||
Object.keys(errors.value).forEach((field) => {
|
||||
const errorMessages = Array.isArray(errors.value[field])
|
||||
? errors.value[field]
|
||||
: [errors.value[field]];
|
||||
form.setFieldError(field, errorMessages[0]);
|
||||
});
|
||||
processing.value = false;
|
||||
},
|
||||
onFinish: () => {
|
||||
processing.value = false;
|
||||
},
|
||||
}
|
||||
processing.value = false;
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
watch(
|
||||
|
||||
Reference in New Issue
Block a user