-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entries.vue
87 lines (75 loc) · 2.3 KB
/
Entries.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<template>
<div class="main">
<button v-if="new2old" @click="new2old=!new2old">Newest to Oldest</button>
<button v-else-if="!new2old" @click="new2old=!new2old">Oldest to newest</button>
<div v-if="new2old">
<ul :key="entry.id" v-for="entry in newToOldest" class="list-entries">
<li>
<h3>{{entry.title}}</h3>
<p>{{entry.date|prettifyDate }}</p>
<img :src="entry.image" alt="Entry image" srcset="">
<p>{{entry.text}}</p>
<hr>
</li>
</ul>
</div>
<div v-if="!new2old">
<ul :key="entry.id" v-for="entry in oldestToNew" class="list-entries">
<li>
<h3>{{entry.title}}</h3>
<p>{{entry.date|prettifyDate }}</p>
<img :src="entry.image" alt="Entry image" srcset="">
<p>{{entry.text}}</p>
<hr>
</li>
</ul>
</div>
</div>
</template>
<script>
export default {
name: 'Entries',
props: {
entries: Array
},
data:function(){
return {
new2old:true // default ordering is new to old
}
},
filters:{
prettifyDate:function(value){
var datetime=new Date(value);
var datepretty=datetime.toDateString();
var timepretty=datetime.toLocaleTimeString();
return datepretty+" "+timepretty
}
},
computed:{
// there will be some other ways to write the same functionality more "cleaner" way but for now it works :)
newToOldest:function(){
return this.entries.slice().sort((a,b)=>new Date(b.date)-new Date(a.date)) // slice is for not mutating actual array (throws error)
},
oldestToNew:function(){
return this.entries.slice().sort((a,b)=>new Date(a.date)-new Date(b.date)) // slice is for not mutating actual array (throws error)
}
},
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>