Skip to content

Instantly share code, notes, and snippets.

@rokumatsumoto
Last active October 22, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokumatsumoto/7fc11afa8300484a358f8ffd2267f0e2 to your computer and use it in GitHub Desktop.
Save rokumatsumoto/7fc11afa8300484a358f8ffd2267f0e2 to your computer and use it in GitHub Desktop.
simplified download button component for testing purpose
<script>
import { mapActions, mapState } from 'vuex';
export default {
name: 'DownloadButton',
props: {
cssClass: {
type: String,
required: true,
},
text: {
type: String,
required: true,
},
downloadingText: {
type: String,
required: true,
},
recordId: {
type: String,
required: true,
},
endpoint: {
type: String,
required: true,
}
},
computed: {
...mapState(['isDownloading']),
buttonText() {
return this.isDownloading ? this.downloadingText : this.text;
},
iconCssClass() {
return this.isDownloading ? 'fa-circle-notch fa-spin' : 'fa-cloud-download-alt';
}
},
channels: {
DownloadChannel: {
connected() {
console.log('I am connected.');
},
rejected() {},
received(data) {
if (data.url !== undefined) {
window.location = data.url;
}
},
disconnected() {
console.log('I am disconnected.');
},
}
},
methods: {
...mapActions([
'fetchDownloadUrl',
]),
download() {
// subscribe DownloadChannel
this.$cable.subscribe({ channel: 'DownloadChannel', id: this.recordId });
// after receive download url from server -> unsubscribe DownloadChannel
// calling `unsubscribe` here for demonstrating the problem
const vm = this;
setTimeout(function(){
vm.$cable.unsubscribe('DownloadChannel');
}, 1000);
}
}
}
</script>
<template>
<div>
<button ref="downloadButton" :class="cssClass" @click="download" :disabled="isDownloading">
<span><i class="fas fa-lg" :class="iconCssClass"></i></span>
<span class="btn-inner--text">{{ buttonText }}</span>
</button>
</div>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment