-
Notifications
You must be signed in to change notification settings - Fork 87
/
follow.vue
77 lines (74 loc) · 2.03 KB
/
follow.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
<template>
<button type="button" class="btn btn-follow" :class="btnClasses" @click="toggle"
v-tooltip="tooltip" :tooltip-placement="tooltipPlacement">
<span class="fa" :class="icon"></span>
<span v-if="withLabel">{{ label }}</span>
<span v-if="followers">{{ followers }}</span>
</button>
</template>
<script>
import i18n from 'i18n';
export default {
props: {
classes: {
type: Array,
coerce(value) {
if (Array.isArray(value)) return value;
return value.split(' ').filter(value => value.trim());
}
},
followers: {
type: Number,
default: undefined
},
following: {
type: Boolean,
default: false
},
withLabel: {
type: Boolean,
default: false
},
tooltip: {
type: String,
default: i18n._("I'll be informed about its activity")
},
tooltipPlacement: {
type: String,
default: 'left'
},
url: {
type: String,
required: true
}
},
computed: {
btnClasses() {
const classes = {active: this.following};
this.classes.forEach(cls => {
classes[cls] = true;
});
return classes;
},
icon() {
return this.following ? 'fa-star': 'fa-star-o';
},
label() {
return this.following ? this._('Unfollow') : this._('Follow');
}
},
methods: {
toggle() {
this.$auth(this._('You need to be logged in to follow.'));
const method = this.following ? this.$api.delete(this.url) : this.$api.post(this.url);
method.then(data => {
this.following = !this.following;
if (this.followers !== undefined) {
this.followers = data.followers;
}
});
}
}
};
</script>
<style lang="less"></style>