Personal_Website/resources/js/components/BlogRecent.vue

73 lines
2.2 KiB
Vue

<template>
<section class="blog-recent">
<div class="header">
<h3>Latest Articles</h3>
<div class="all-articles">
<router-link :to="{ name: 'blog' }">All articles</router-link>
</div>
</div>
<div class="list">
<ul class="blog-items">
<li class="blog-item" v-for="article of articles" :key="article.id" @click="onClick(article)">
<img
class="article-image"
src="//via.placeholder.com/128"
alt="Article Topic"
/>
<div class="article-text">
<h4 class="article-title">
{{ article.title }}
</h4>
<span class="article-date" :title="absDate(article.date)">{{ relDate(article.date) }}</span>
<span class="article-published" v-if="!article.published">Unpublished</span>
</div>
</li>
</ul>
</div>
</section>
</template>
<script>
export default {
data() {
return {
loading: false,
articles: [],
error: null,
};
},
mounted() {
axios.get("/api/blog/recent").then((res) => {
if (res.status > 399) {
this.error = res.statusText;
} else {
this.articles = res.data;
}
this.loading = false;
}).catch(ex => {
this.loading = false;
this.error = ex.message;
});
},
methods: {
relDate(date) {
return moment(date).fromNow();
},
absDate(date) {
return moment(date).format('MMMM Do YYYY, hh:mm:ss');
},
onClick(article) {
const date = moment(article.date);
const year = date.year();
const month = date.month() + 1;
const day = date.date();
const id = article.id;
const slug = article.slug;
this.$router.push({ name: 'blog-article', params: { year, month, day, id, slug }});
},
},
}
</script>