Library/frontend/src/components/BookOverview.vue

189 lines
5.4 KiB
Vue
Raw Normal View History

2017-11-21 12:23:46 +00:00
<template>
<div v-if="user.authenticated">
2017-11-29 15:43:58 +00:00
<h1>{{ book.title }}</h1>
2017-11-21 12:23:46 +00:00
<div class="ui info message" v-if="loading">
<icon name="refresh" spin></icon>&nbsp;&nbsp;
2017-11-22 13:17:29 +00:00
<span v-lang.general.loading></span>
2017-11-21 12:23:46 +00:00
</div>
<div v-if="!loading">
2017-11-21 12:23:46 +00:00
<router-link :to="{ name: 'book-edit', params: { id: bookID} }" class="ui teal labeled icon button" style="float: right;">
<i class="edit icon"></i>
2017-11-22 13:17:29 +00:00
<span v-lang.general.edit></span>
2017-11-21 12:23:46 +00:00
</router-link>
2017-11-24 10:54:52 +00:00
<list :infos="bookList" />
2017-11-30 14:26:28 +00:00
<p class="grey">
<span v-lang.general.created="this.createdTime"></span><br/>
<span v-if="this.book.created !== this.book.updated" v-lang.general.lastEdit="this.editedTime"></span>
</p>
2017-11-21 12:23:46 +00:00
</div>
</div>
</template>
<script>
import auth from '../auth'
import {HTTP} from '../http-common'
// import router from '../router'
export default {
name: 'Book',
data () {
return {
user: auth.user,
book: {},
allStatus: [],
2017-11-24 10:54:52 +00:00
bookID: this.$route.params.id,
bookList: {},
2017-11-30 14:26:28 +00:00
AuthorList: [],
createdTime: {
date: '',
time: ''
},
editedTime: {
date: '',
time: ''
}
2017-11-21 12:23:46 +00:00
}
},
created () {
this.loadStatus()
this.loadBook()
2017-12-05 14:55:56 +00:00
document.title = this.translate('nav').books
2017-11-21 12:23:46 +00:00
},
watch: {
// call again the method if the route changes
'$route': 'loadBook'
},
methods: {
errorNotification (e) {
// Build the notification text from error response
let err = e.message
if (e.response.data.message) {
err += '<br/>' + e.response.data.message
}
// Fire a notification
this.$notify({
type: 'error',
title: this.langGeneral.error,
text: err
})
},
2017-11-21 12:23:46 +00:00
loadBook () {
this.loading = true
this.book = {}
HTTP.get(`books/` + this.bookID)
.then(response => {
this.book = response.data
2017-11-24 10:54:52 +00:00
// Get all authors and build an array with them
2017-11-29 15:43:58 +00:00
let authors = this.book.authors
2017-11-21 12:23:46 +00:00
for (const au in authors) {
2017-11-24 10:54:52 +00:00
this.AuthorList.push({
2017-11-29 15:43:58 +00:00
content: authors[au].forename + ' ' + authors[au].lastname,
2017-11-24 10:54:52 +00:00
link: {
name: 'author-show',
params: {
2017-11-29 15:43:58 +00:00
id: authors[au].id
2017-11-24 10:54:52 +00:00
}
}
})
2017-11-21 12:23:46 +00:00
}
// Make Status a name, not an id
2017-11-29 15:43:58 +00:00
this.book.status = this.getStatusByID(this.book.status)
2017-11-24 10:54:52 +00:00
// Build the list object
this.bookList = [
2017-11-28 15:03:25 +00:00
{
header: this.translate('books').description,
2017-11-29 15:43:58 +00:00
content: this.book.description.replace(/\n/g, '<br>')
2017-11-28 15:03:25 +00:00
},
2017-11-24 10:54:52 +00:00
{
header: this.translate('books').gridColumns.isbn,
2017-11-29 15:43:58 +00:00
content: this.book.isbn
2017-11-24 10:54:52 +00:00
},
{
header: this.translate('books').gridColumns.year,
2017-11-29 15:43:58 +00:00
content: this.book.year
2017-11-24 10:54:52 +00:00
},
{
header: this.translate('books').gridColumns.price,
2017-11-29 15:43:58 +00:00
content: this.book.price
2017-11-24 10:54:52 +00:00
},
2017-11-28 15:03:25 +00:00
{
header: this.translate('books').gridColumns.quantity,
2017-11-29 15:43:58 +00:00
content: this.book.quantity
2017-11-28 15:03:25 +00:00
},
2017-11-24 10:54:52 +00:00
{
header: this.translate('books').gridColumns.status,
2017-11-29 15:43:58 +00:00
content: this.book.status
2017-11-24 10:54:52 +00:00
},
{
header: this.translate('books').gridColumns.publisher,
2017-11-29 15:43:58 +00:00
content: this.book.publisher.name,
2017-11-24 10:54:52 +00:00
link: {
name: 'publisher-show',
params: {
2017-11-29 15:43:58 +00:00
id: this.book.publisher.id
2017-11-24 10:54:52 +00:00
}
}
},
{
header: this.translate('books').gridColumns.authors,
content: this.AuthorList
}
]
2017-11-30 14:26:28 +00:00
// Beautify the date
let c = new Date(this.book.created * 1000)
// c.setSeconds()
this.createdTime = {
date: ('0' + c.getDate()).slice(-2) + '.' + ('0' + (c.getMonth() + 1)).slice(-2) + '.' + c.getFullYear(),
time: ('0' + c.getHours()).slice(-2) + ':' + ('0' + c.getMinutes()).slice(-2)
}
let e = new Date(this.book.updated * 1000)
// e.setSeconds()
this.editedTime = {
date: ('0' + e.getDate()).slice(-2) + '.' + ('0' + (e.getMonth() + 1)).slice(-2) + '.' + e.getFullYear(),
time: ('0' + e.getHours()).slice(-2) + ':' + ('0' + e.getMinutes()).slice(-2)
}
2017-12-05 14:55:56 +00:00
// Set the title
document.title = this.book.title + ' | ' + this.translate('nav').books
2017-11-30 14:26:28 +00:00
this.loading = false
2017-11-21 12:23:46 +00:00
})
.catch(e => {
this.loading = false
this.errorNotification(e)
2017-11-21 12:23:46 +00:00
})
},
loadStatus: function () {
HTTP.get('status')
.then(response => {
this.allStatus = response.data
})
.catch(e => {
this.errorNotification(e)
2017-11-21 12:23:46 +00:00
})
},
getStatusByID: function (id) {
// TODO: is there a better way to do this?
for (const i in this.allStatus) {
if (this.allStatus[i].ID === id) {
return this.allStatus[i].Name
}
}
return ''
}
}
}
</script>