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

124 lines
2.4 KiB
Vue
Raw Normal View History

2018-02-15 07:37:25 +01:00
<template>
<button class="mk-follow-button"
2018-03-29 07:48:47 +02:00
:class="{ wait: wait, follow: !user.isFollowing, unfollow: user.isFollowing }"
2018-02-15 07:37:25 +01:00
@click="onClick"
:disabled="wait"
>
2018-03-29 07:48:47 +02:00
<template v-if="!wait && user.isFollowing">%fa:minus%</template>
<template v-if="!wait && !user.isFollowing">%fa:plus%</template>
2018-02-15 07:37:25 +01:00
<template v-if="wait">%fa:spinner .pulse .fw%</template>
2018-05-20 13:26:38 +02:00
{{ user.isFollowing ? '%i18n:@unfollow%' : '%i18n:@follow%' }}
2018-02-15 07:37:25 +01: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 04:35:18 +01:00
this.connection = (this as any).os.stream.getConnection();
this.connectionId = (this as any).os.stream.use();
2018-02-15 07:37:25 +01: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 04:35:18 +01:00
(this as any).os.stream.dispose(this.connectionId);
2018-02-15 07:37:25 +01:00
},
methods: {
onFollow(user) {
if (user.id == this.user.id) {
2018-03-29 07:48:47 +02:00
this.user.isFollowing = user.isFollowing;
2018-02-15 07:37:25 +01:00
}
},
onUnfollow(user) {
if (user.id == this.user.id) {
2018-03-29 07:48:47 +02:00
this.user.isFollowing = user.isFollowing;
2018-02-15 07:37:25 +01:00
}
},
onClick() {
this.wait = true;
2018-03-29 07:48:47 +02:00
if (this.user.isFollowing) {
2018-02-18 04:35:18 +01:00
(this as any).api('following/delete', {
2018-03-29 07:48:47 +02:00
userId: this.user.id
2018-02-15 07:37:25 +01:00
}).then(() => {
2018-03-29 07:48:47 +02:00
this.user.isFollowing = false;
2018-02-15 07:37:25 +01:00
}).catch(err => {
console.error(err);
}).then(() => {
this.wait = false;
});
} else {
2018-02-18 04:35:18 +01:00
(this as any).api('following/create', {
2018-03-29 07:48:47 +02:00
userId: this.user.id
2018-02-15 07:37:25 +01:00
}).then(() => {
2018-03-29 07:48:47 +02:00
this.user.isFollowing = true;
2018-02-15 07:37:25 +01:00
}).catch(err => {
console.error(err);
}).then(() => {
this.wait = false;
});
}
}
}
});
</script>
<style lang="stylus" scoped>
2018-03-03 05:47:55 +01:00
@import '~const.styl'
2018-02-15 07:37:25 +01: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>