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

131 lines
2.7 KiB
Vue
Raw Normal View History

2018-02-15 08:37:25 +02:00
<template>
<button class="mk-follow-button"
2018-06-02 06:58:56 +03:00
:class="{ wait: wait, following: u.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">
2018-06-02 06:58:56 +03:00
<template v-if="u.hasPendingFollowRequestFromYou">%fa:hourglass-half% %i18n:@request-pending%</template>
<template v-else-if="u.isFollowing">%fa:minus% %i18n:@unfollow%</template>
<template v-else-if="!u.isFollowing && u.isLocked">%fa:plus% %i18n:@follow-request%</template>
<template v-else-if="!u.isFollowing && !u.isLocked">%fa:plus% %i18n:@follow%</template>
2018-06-01 18:15:17 +03:00
</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 {
2018-06-02 06:58:56 +03:00
u: this.user,
2018-02-15 08:37:25 +02:00
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) {
2018-06-02 06:58:56 +03:00
if (user.id == this.u.id) {
this.u.isFollowing = user.isFollowing;
2018-02-15 08:37:25 +02:00
}
},
onUnfollow(user) {
2018-06-02 06:58:56 +03:00
if (user.id == this.u.id) {
this.u.isFollowing = user.isFollowing;
2018-02-15 08:37:25 +02:00
}
},
2018-06-02 06:58:56 +03:00
async onClick() {
2018-02-15 08:37:25 +02:00
this.wait = true;
2018-06-02 06:58:56 +03:00
try {
if (this.u.isFollowing) {
this.u = await (this as any).api('following/delete', {
userId: this.u.id
2018-06-01 18:15:17 +03:00
});
} else {
2018-06-02 06:58:56 +03:00
if (this.u.isLocked && this.u.hasPendingFollowRequestFromYou) {
this.u = await (this as any).api('following/requests/cancel', {
userId: this.u.id
});
} else if (this.u.isLocked) {
this.u = await (this as any).api('following/create', {
userId: this.u.id
});
} else {
this.u = await (this as any).api('following/create', {
userId: this.user.id
});
}
2018-06-01 18:15:17 +03:00
}
2018-06-02 06:58:56 +03:00
} catch (e) {
console.error(e);
} finally {
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-06-02 06:58:56 +03:00
color $theme-color
background transparent
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
2018-06-02 06:58:56 +03:00
&.following
color $theme-color-foreground
background $theme-color
2018-02-15 08:37:25 +02:00
&:hover
background rgba($theme-color, 0.1)
&:active
background rgba($theme-color, 0.2)
&.wait
cursor wait !important
opacity 0.7
> [data-fa]
margin-right 4px
</style>