Skip to content

Commit

Permalink
🐛 bug(examples): fix ssr demo
Browse files Browse the repository at this point in the history
Closes #151
  • Loading branch information
kazupon committed Apr 24, 2017
1 parent 2283075 commit 059034f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
8 changes: 7 additions & 1 deletion examples/ssr/src/components/Comment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<li v-if="comment" class="comment">
<div class="by">
<router-link :to="'/user/' + comment.by">{{ comment.by }}</router-link>
{{ comment.time | timeAgo($tc) }} {{ $t('time.ago') }}
{{ time }}
</div>
<div class="text" v-html="comment.text"></div>
<div class="toggle" :class="{ open }" v-if="comment.kids && comment.kids.length">
Expand All @@ -19,6 +19,8 @@
</template>

<script>
import { timeAgo } from '../util'
export default {
name: 'comment',
props: ['id'],
Expand All @@ -30,6 +32,10 @@ export default {
computed: {
comment () {
return this.$store.state.items[this.id]
},
time () {
const { value, unit } = timeAgo(this.comment.time)
return `${value} ${this.$tc('time.units.' + unit, value)} ${this.$t('time.ago')}`
}
},
methods: {
Expand Down
10 changes: 8 additions & 2 deletions examples/ssr/src/components/Item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
by <router-link :to="'/user/' + item.by">{{ item.by }}</router-link>
</span>
<span class="time">
{{ item.time | timeAgo($tc) }} {{ $t('time.ago') }}
{{ time }}
</span>
<span v-if="item.type !== 'job'" class="comments-link">
| <router-link :to="'/item/' + item.id">{{ item.descendants }} {{ $t('item.comments') }}</router-link>
Expand All @@ -27,11 +27,17 @@
</template>

<script>
import { timeAgo } from '../filters'
import { timeAgo } from '../util'
export default {
name: 'news-item',
props: ['item'],
computed: {
time () {
const { value, unit } = timeAgo(this.item.time)
return `${value} ${this.$tc('time.units.' + unit, value)} ${this.$t('time.ago')}`
}
},
// https://github.com/vuejs/vue/blob/next/packages/vue-server-renderer/README.md#component-caching
serverCacheKey: ({ item: { id, __lastUpdated, time }}) => {
return `${id}::${__lastUpdated}::${time}`
Expand Down
10 changes: 10 additions & 0 deletions examples/ssr/src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function timeAgo (time) {
const between = Date.now() / 1000 - Number(time)
if (between < 3600) {
return { value: ~~(between / 60), unit: 'minute' }
} else if (between < 86400) {
return { value: ~~(between / 3600), unit: 'hour' }
} else {
return { value: ~~(between / 86400), unit: 'day' }
}
}
7 changes: 6 additions & 1 deletion examples/ssr/src/views/ItemView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<p class="meta">
{{ item.score }} points
| by <router-link :to="'/user/' + item.by">{{ item.by }}</router-link>
{{ item.time | timeAgo($tc) }} {{ $t('time.ago') }}
{{ time }}
</p>
</div>
<div class="item-view-comments">
Expand All @@ -30,6 +30,7 @@
<script>
import Spinner from '../components/Spinner.vue'
import Comment from '../components/Comment.vue'
import { timeAgo } from '../util'
function fetchItem (store) {
return store.dispatch('FETCH_ITEMS', {
Expand Down Expand Up @@ -66,6 +67,10 @@ export default {
computed: {
item () {
return this.$store.state.items[this.$route.params.id]
},
time () {
const { value, unit } = timeAgo(this.item.time)
return `${value} ${this.$tc('time.units.' + unit, value)} ${this.$t('time.ago')}`
}
},
// on the server, only fetch the item itself
Expand Down
7 changes: 6 additions & 1 deletion examples/ssr/src/views/UserView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<template v-if="user">
<h1>{{ $t('user.id') }} : {{ user.id }}</h1>
<ul class="meta">
<li><span class="label">{{ $t('user.created') }}:</span> {{ user.created | timeAgo($tc) }} {{ $t('time.ago') }}</li>
<li><span class="label">{{ $t('user.created') }}:</span> {{ time }}</li>
<li><span class="label">{{ $t('user.karma') }}:</span> {{user.karma}}</li>
<li v-if="user.about" v-html="user.about" class="about"></li>
</ul>
Expand All @@ -18,6 +18,7 @@

<script>
import Spinner from '../components/Spinner.vue'
import { timeAgo } from '../util'
function fetchUser (store) {
return store.dispatch('FETCH_USER', {
Expand All @@ -31,6 +32,10 @@ export default {
computed: {
user () {
return this.$store.state.users[this.$route.params.id]
},
time () {
const { value, unit } = timeAgo(this.user.created)
return `${value} ${this.$tc('time.units.' + unit, value)} ${this.$t('time.ago')}`
}
},
preFetch: fetchUser,
Expand Down

0 comments on commit 059034f

Please sign in to comment.