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

139 lines
3 KiB
Vue
Raw Normal View History

2018-02-15 08:37:25 +02:00
<template>
<button class="mk-follow-button"
2018-06-01 18:15:17 +03:00
:class="{ wait: wait, following: user.isFollowing, unfollow: user.isFollowing }"
2018-02-15 08:37:25 +02:00
@click="onClick"
:disabled="wait"
>
2018-06-01 18:15:17 +03:00
<template v-if="!wait">
<template v-if="user.hasPendingFollowRequestFromYou">%fa:hourglass-half% %i18n:@request-pending%</template>
<template v-else-if="user.isFollowing">%fa:minus% %i18n:@unfollow%</template>
<template v-else-if="!user.isFollowing && user.isLocked">%fa:plus% %i18n:@follow-request%</template>
<template v-else-if="!user.isFollowing && !user.isLocked">%fa:plus% %i18n:@follow%</template>
</template>
<template v-else>%fa:spinner .pulse .fw%</template>
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-06-01 18:15:17 +03:00
if (this.user.isLocked && this.user.hasPendingFollowRequestFromYou) {
(this as any).api('following/requests/cancel', {
userId: this.user.id
}).then(() => {
this.user.hasPendingFollowRequestFromYou = false;
}).catch(err => {
console.error(err);
}).then(() => {
this.wait = false;
});
} else {
(this as any).api('following/create', {
userId: this.user.id
}).then(() => {
this.user.isFollowing = true;
}).catch(err => {
console.error(err);
}).then(() => {
this.wait = false;
});
}
2018-02-15 08:37:25 +02:00
}
}
}
});
</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
2018-06-01 18:15:17 +03:00
line-height 36px
font-size 14px
2018-02-15 08:37:25 +02:00
outline none
border solid 1px $theme-color
2018-06-01 18:15:17 +03:00
border-radius 36px
2018-02-15 08:37:25 +02:00
*
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>