fix extract error from api call
This commit is contained in:
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user