Skip to content

Commit da0b54f

Browse files
committed
#33 Initial work toward a snackbar factory
1 parent 7b654c2 commit da0b54f

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

src/App.vue

+4-1
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,19 @@
7676
<v-footer :fixed="fixed" app>
7777
<span>&copy; 2017</span>
7878
</v-footer>
79+
<snackbar-service options="{text:'hllo', status: 'ok'}" id="SnackbarService" ref="SnackbarService" />
7980
</v-app>
8081
</template>
8182

8283
<script lang="ts">
8384
import UserLoginAndRegistrationContainer from '@/components/UserLoginAndRegistrationContainer.vue';
85+
import SnackbarService from '@/components/SnackbarService.vue';
8486
8587
export default {
8688
name: 'App',
8789
components: {
88-
UserLoginAndRegistrationContainer
90+
UserLoginAndRegistrationContainer,
91+
SnackbarService
8992
},
9093
data() {
9194
return {

src/components/HelloWorld.vue

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
</blockquote>
1414
<h1>Hi</h1>
1515

16-
<UserLogin />
16+
<v-btn color="success" @click="newSnack">text</v-btn>
17+
<!-- <UserLogin /> -->
1718

1819
</v-layout>
1920
</v-slide-y-transition>
@@ -22,6 +23,8 @@
2223

2324
<script lang="ts">
2425
import UserLogin from './UserLogin.vue';
26+
import SnackbarService from '@/components/SnackbarService.vue';
27+
import { Status } from '@/enums/Status';
2528
2629
export default {
2730
name: 'HelloWorld',
@@ -30,6 +33,14 @@ export default {
3033
},
3134
components: {
3235
UserLogin
36+
},
37+
methods: {
38+
newSnack: () => {
39+
SnackbarService.addSnack({
40+
text: 'test',
41+
status: Status.ok
42+
});
43+
}
3344
}
3445
};
3546
</script>

src/components/SnackbarService.vue

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)