|
| 1 | +<template> |
| 2 | + <v-snackbar |
| 3 | + v-model="show" |
| 4 | + :timeout="options.timeout" |
| 5 | + bottom |
| 6 | + right |
| 7 | + > |
| 8 | + {{ options.text }} |
| 9 | + |
| 10 | + |
| 11 | + <v-btn |
| 12 | + color="pink" |
| 13 | + flat |
| 14 | + @click="snackbar = false" |
| 15 | + > |
| 16 | + Close |
| 17 | + </v-btn> |
| 18 | + </v-snackbar> |
| 19 | +</template> |
| 20 | + |
| 21 | + |
| 22 | +<script lang="ts"> |
| 23 | +import Vue from 'vue'; |
| 24 | +import Component from 'vue-class-component'; |
| 25 | +import { Status } from '@/enums/Status'; |
| 26 | +import { Prop, Watch } from 'vue-property-decorator'; |
| 27 | +import { watch } from 'fs'; |
| 28 | +
|
| 29 | +interface SnackbarOptions { |
| 30 | + text: string; |
| 31 | + status: Status; |
| 32 | + timeout?: number; |
| 33 | +} |
| 34 | +
|
| 35 | +@Component |
| 36 | +export default class SnackbarService extends Vue { |
| 37 | + public static addSnack(options: SnackbarOptions) { |
| 38 | + options = { |
| 39 | + text: options.text, |
| 40 | + status: options.status, |
| 41 | + timeout: options.timeout !== undefined ? options.timeout : 5000 |
| 42 | + }; |
| 43 | +
|
| 44 | + // get singleton instance of snackbar service |
| 45 | + const service: SnackbarService = |
| 46 | + (document.getElementById('SnackbarService')! as any) as SnackbarService; |
| 47 | +
|
| 48 | + // // update the props |
| 49 | + service.options = options; |
| 50 | + service.show = true; |
| 51 | + service.$forceUpdate(); |
| 52 | + // const snack = new SnackbarService(); |
| 53 | + // snack.options = options; |
| 54 | +
|
| 55 | + } |
| 56 | +
|
| 57 | + @Prop({ |
| 58 | + default: () => { |
| 59 | + return { |
| 60 | + text: 'This is some text', |
| 61 | + status: Status.ok, |
| 62 | + timeout: 5000 |
| 63 | + }; |
| 64 | + } |
| 65 | + }) private options: SnackbarOptions; |
| 66 | + private show: boolean = true; |
| 67 | +
|
| 68 | + public created() { |
| 69 | + if (this.$store.state.snack) { |
| 70 | + this.options = this.$store.state.snack; |
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | + @Watch('$store.state.snack') |
| 75 | + public update() { |
| 76 | + if (this.$store.state.snack) { |
| 77 | + this.options = this.$store.state.snack; |
| 78 | + } |
| 79 | + } |
| 80 | +
|
| 81 | +
|
| 82 | + /** |
| 83 | + * A history of snacks served in this session. |
| 84 | + * |
| 85 | + * Possible future work: write these to localstorage/pouchdb |
| 86 | + * for persistance |
| 87 | + */ |
| 88 | + // private snacks: SnackbarOptions[] = []; |
| 89 | +
|
| 90 | + private clearSnack() { |
| 91 | + this.$store.state.snack = {}; |
| 92 | + delete this.$store.state.snack; |
| 93 | + this.show = false; |
| 94 | + } |
| 95 | +} |
| 96 | +</script> |
0 commit comments