|
|
|
@ -5,7 +5,7 @@
|
|
|
|
|
<h1>Books</h1>
|
|
|
|
|
<hr>
|
|
|
|
|
<br><br>
|
|
|
|
|
<alert :message="message" v-if="showMessage" v-on:alert-closed="showMessage = !showMessage"></alert>
|
|
|
|
|
<alert :variant="alertVariant" :message="message" v-if="showMessage" v-on:alert-closed="showMessage = !showMessage"></alert>
|
|
|
|
|
<button type="button" class="btn btn-success btn-sm" v-b-modal.book-modal>Add Book</button>
|
|
|
|
|
<br><br>
|
|
|
|
|
<table class="table table-hover">
|
|
|
|
@ -26,7 +26,7 @@
|
|
|
|
|
<span v-else>No</span>
|
|
|
|
|
</td>
|
|
|
|
|
<td>
|
|
|
|
|
<button type="button" class="btn btn-warning btn-sm">Update</button>
|
|
|
|
|
<button type="button" class="btn btn-warning btn-sm" v-b-modal.book-update-modal @click="editBook(book)">Update</button>
|
|
|
|
|
<button type="button" class="btn btn-danger btn-sm">Delete</button>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
@ -68,6 +68,40 @@
|
|
|
|
|
<b-button type="reset" variant="danger">Reset</b-button>
|
|
|
|
|
</b-form>
|
|
|
|
|
</b-modal>
|
|
|
|
|
<b-modal ref="editBookModal"
|
|
|
|
|
id="book-update-modal"
|
|
|
|
|
title="Update"
|
|
|
|
|
hide-footer>
|
|
|
|
|
<b-form @submit.prevent="onSubmitUpdate" @reset.prevent="onResetUpdate" class="w-100">
|
|
|
|
|
<b-form-group id="form-title-edit-group"
|
|
|
|
|
label="Title:"
|
|
|
|
|
label-for="form-title-edit-input">
|
|
|
|
|
<b-form-input id="form-title-edit-input"
|
|
|
|
|
type="text"
|
|
|
|
|
v-model="editForm.title"
|
|
|
|
|
required
|
|
|
|
|
placeholder="Enter title">
|
|
|
|
|
</b-form-input>
|
|
|
|
|
</b-form-group>
|
|
|
|
|
<b-form-group id="form-author-edit-group"
|
|
|
|
|
label="Author:"
|
|
|
|
|
label-for="form-author-edit-input">
|
|
|
|
|
<b-form-input id="form-author-edit-input"
|
|
|
|
|
type="text"
|
|
|
|
|
v-model="editForm.author"
|
|
|
|
|
required
|
|
|
|
|
placeholder="Enter author">
|
|
|
|
|
</b-form-input>
|
|
|
|
|
</b-form-group>
|
|
|
|
|
<b-form-group id="form-read-edit-group">
|
|
|
|
|
<b-form-checkbox-group v-model="editForm.read" id="form-checks">
|
|
|
|
|
<b-form-checkbox value="true">Read?</b-form-checkbox>
|
|
|
|
|
</b-form-checkbox-group>
|
|
|
|
|
</b-form-group>
|
|
|
|
|
<b-button type="submit" variant="primary">Update</b-button>
|
|
|
|
|
<b-button type="reset" variant="danger">Cancel</b-button>
|
|
|
|
|
</b-form>
|
|
|
|
|
</b-modal>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
@ -96,21 +130,37 @@ export default {
|
|
|
|
|
read: [],
|
|
|
|
|
},
|
|
|
|
|
message: '',
|
|
|
|
|
alertVariant: '',
|
|
|
|
|
showMessage: false,
|
|
|
|
|
editForm: {
|
|
|
|
|
id: '',
|
|
|
|
|
title: '',
|
|
|
|
|
author: '',
|
|
|
|
|
read: [],
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
methods:{
|
|
|
|
|
...mapActions(['LOAD_BOOKS_LIST', 'POST_BOOK']),
|
|
|
|
|
...mapActions(['LOAD_BOOKS_LIST', 'POST_BOOK', 'PUT_BOOK']),
|
|
|
|
|
initForm(){
|
|
|
|
|
this.addBookForm.title = ''
|
|
|
|
|
this.addBookForm.author = ''
|
|
|
|
|
this.addBookForm.read = []
|
|
|
|
|
this.addBookForm.read = [];
|
|
|
|
|
this.editForm.id = '';
|
|
|
|
|
this.editForm.title = '';
|
|
|
|
|
this.editForm.author = '';
|
|
|
|
|
this.editForm.read = [];
|
|
|
|
|
},
|
|
|
|
|
editBook(book){
|
|
|
|
|
Object.assign(this.editForm, book)
|
|
|
|
|
},
|
|
|
|
|
onSubmit(){
|
|
|
|
|
this.$refs.addBookModal.hide()
|
|
|
|
|
let read = false
|
|
|
|
|
if (this.addBookForm.read[0]) read = true
|
|
|
|
|
const payload = {
|
|
|
|
|
id: this.addBookForm.id,
|
|
|
|
|
title: this.addBookForm.title,
|
|
|
|
|
author: this.addBookForm.author,
|
|
|
|
|
read,// property shorthand
|
|
|
|
@ -118,15 +168,49 @@ export default {
|
|
|
|
|
// this is an alternative way to call actions
|
|
|
|
|
// this.$store.dispatch('POST_BOOK', payload)
|
|
|
|
|
this.POST_BOOK(payload)
|
|
|
|
|
this.message = 'book added!'
|
|
|
|
|
this.showMessage = true
|
|
|
|
|
.then((response) => {
|
|
|
|
|
this.message = "Book added!"
|
|
|
|
|
this.alertVariant = "success"
|
|
|
|
|
this.showMessage = true
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
this.message = "Error in adding book"
|
|
|
|
|
this.alertVariant = "danger"
|
|
|
|
|
this.showMessage = true
|
|
|
|
|
})
|
|
|
|
|
this.initForm()
|
|
|
|
|
},
|
|
|
|
|
onReset() {
|
|
|
|
|
//alternatively capture the event and evt.preventDefault()
|
|
|
|
|
this.$refs.addBookModal.hide()
|
|
|
|
|
this.initForm()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onSubmitUpdate() {
|
|
|
|
|
this.$refs.editBookModal.hide()
|
|
|
|
|
let read = false
|
|
|
|
|
if (this.editForm.read[0]) read = true
|
|
|
|
|
const payload = {
|
|
|
|
|
id: this.editForm.id,
|
|
|
|
|
title: this.editForm.title,
|
|
|
|
|
author: this.editForm.author,
|
|
|
|
|
read,
|
|
|
|
|
}
|
|
|
|
|
this.PUT_BOOK(payload)
|
|
|
|
|
.then((response) => {
|
|
|
|
|
this.message = "Book updated"
|
|
|
|
|
this.alertVariant = "success"
|
|
|
|
|
this.showMessage = true
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
this.message = "Error in updating book"
|
|
|
|
|
this.alertVariant = "danger"
|
|
|
|
|
this.showMessage = true
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
onResetUpdate() {
|
|
|
|
|
this.$refs.editBookModal.hide()
|
|
|
|
|
this.initForm()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
computed:{
|
|
|
|
@ -135,6 +219,13 @@ export default {
|
|
|
|
|
created() {
|
|
|
|
|
//this.$store.dispatch('
|
|
|
|
|
this.LOAD_BOOKS_LIST()
|
|
|
|
|
.then((response) => {
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
this.message = "Error in fetching book list"
|
|
|
|
|
this.alertVariant = "danger"
|
|
|
|
|
this.showMessage = true
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|