fix extract error from api call

This commit is contained in:
2025-10-09 00:34:57 +02:00
parent 774756a747
commit 4f1452e0d9
4 changed files with 12 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
<script setup>
import { ref, reactive, computed } from 'vue'
import { postBook } from './api.js'
import { postBook, extractFormErrorFromField } from './api.js'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter();
@@ -11,24 +11,12 @@
});
const errors = ref(null)
const titleError = computed(() => {
return extractErrorFromField("Title");
return extractFormErrorFromField("Title", errors.value);
})
const authorError = computed(() => {
return extractErrorFromField("Author");
return extractFormErrorFromField("Author", errors.value);
})
function extractErrorFromField(fieldName) {
if (errors.value === null) {
return "";
}
const titleErr = errors.value.find((e) => e["field"] === fieldName);
if (typeof titleErr !== 'undefined') {
return titleErr.error
} else {
return "";
}
}
function onSubmit(e) {
postBook(book)
.then((res) => {