Sharkey/src/client/app/mobile/views/components/follow-button.vue

124 lines
2.4 KiB
Vue
Raw Normal View History

2018-02-15 08:37:25 +02:00
<template>
<button class="mk-follow-button"
2018-03-29 08:48:47 +03:00
:class="{ wait: wait, follow: !user.isFollowing, unfollow: user.isFollowing }"
2018-02-15 08:37:25 +02:00
@click="onClick"
:disabled="wait"
>
2018-03-29 08:48:47 +03:00
<template v-if="!wait && user.isFollowing">%fa:minus%</template>
<template v-if="!wait && !user.isFollowing">%fa:plus%</template>
2018-02-15 08:37:25 +02:00
<template v-if="wait">%fa:spinner .pulse .fw%</template>
2018-04-14 19:04:40 +03:00
{{ user.isFollowing ? '%i18n:@unfollow%' : '%i18n:@follow%' }}
2018-02-15 08:37:25 +02:00
</button>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
props: {
user: {
type: Object,
required: true
}
},
data() {
return {
wait: false,
connection: null,
connectionId: null
};
},
mounted() {
2018-02-18 05:35:18 +02:00
this.connection = (this as any).os.stream.getConnection();
this.connectionId = (this as any).os.stream.use();
2018-02-15 08:37:25 +02:00
this.connection.on('follow', this.onFollow);
this.connection.on('unfollow', this.onUnfollow);
},
beforeDestroy() {
this.connection.off('follow', this.onFollow);
this.connection.off('unfollow', this.onUnfollow);
2018-02-18 05:35:18 +02:00
(this as any).os.stream.dispose(this.connectionId);
2018-02-15 08:37:25 +02:00
},
methods: {
onFollow(user) {
if (user.id == this.user.id) {
2018-03-29 08:48:47 +03:00
this.user.isFollowing = user.isFollowing;
2018-02-15 08:37:25 +02:00
}
},
onUnfollow(user) {
if (user.id == this.user.id) {
2018-03-29 08:48:47 +03:00
this.user.isFollowing = user.isFollowing;
2018-02-15 08:37:25 +02:00
}
},
onClick() {
this.wait = true;
2018-03-29 08:48:47 +03:00
if (this.user.isFollowing) {
2018-02-18 05:35:18 +02:00
(this as any).api('following/delete', {
2018-03-29 08:48:47 +03:00
userId: this.user.id
2018-02-15 08:37:25 +02:00
}).then(() => {
2018-03-29 08:48:47 +03:00
this.user.isFollowing = false;
2018-02-15 08:37:25 +02:00
}).catch(err => {
console.error(err);
}).then(() => {
this.wait = false;
});
} else {
2018-02-18 05:35:18 +02:00
(this as any).api('following/create', {
2018-03-29 08:48:47 +03:00
userId: this.user.id
2018-02-15 08:37:25 +02:00
}).then(() => {
2018-03-29 08:48:47 +03:00
this.user.isFollowing = true;
2018-02-15 08:37:25 +02:00
}).catch(err => {
console.error(err);
}).then(() => {
this.wait = false;
});
}
}
}
});
</script>
<style lang="stylus" scoped>
2018-03-03 06:47:55 +02:00
@import '~const.styl'
2018-02-15 08:37:25 +02:00
.mk-follow-button
display block
user-select none
cursor pointer
padding 0 16px
margin 0
height inherit
font-size 16px
outline none
border solid 1px $theme-color
border-radius 4px
*
pointer-events none
&.follow
color $theme-color
background transparent
&:hover
background rgba($theme-color, 0.1)
&:active
background rgba($theme-color, 0.2)
&.unfollow
color $theme-color-foreground
background $theme-color
&.wait
cursor wait !important
opacity 0.7
> [data-fa]
margin-right 4px
</style>