|
9 | 9 | </template>
|
10 | 10 |
|
11 | 11 | <script>
|
12 |
| -import { createNamespacedHelpers } from 'vuex'; |
13 |
| -import BeekeeperHomeScreen, { EventType } from '@beekeeper/home-screen-sdk'; |
| 12 | +import { mapActions, mapState } from 'vuex'; |
14 | 13 |
|
15 | 14 | import ProfilesGrid from '~/components/ProfilesGrid.vue';
|
16 |
| -import store from '~/store'; |
17 | 15 |
|
18 | 16 | /**
|
19 |
| - * Step 1: Specify widget type |
| 17 | + * Step 1: Specify widget type name |
20 | 18 | *
|
21 | 19 | * In order for the home screen to be aware of the widget we need to register it. That's why we defined here
|
22 | 20 | * a unique string as widget type. We called it 'profiles' since this is the profiles widget.
|
23 | 21 | * See also the "Widget type" in the "Configuration" section on the developers portal
|
24 | 22 | * {@link https://developers.beekeeper.io/v2/welcome/home-screen}
|
25 | 23 | */
|
26 |
| -export const WIDGET_TYPE = 'profiles'; |
| 24 | +export const WIDGET_TYPE_NAME = 'profiles'; |
27 | 25 | export const PROFILE_LIMIT = 50;
|
28 | 26 |
|
29 |
| -const { mapState, mapActions } = createNamespacedHelpers(WIDGET_TYPE); |
30 |
| -
|
31 | 27 |
|
32 | 28 | export default {
|
33 | 29 | components: {
|
34 | 30 | ProfilesGrid,
|
35 | 31 | },
|
36 |
| - /** |
37 |
| - * Step 3: Add widgetInstanceId prop |
38 |
| - * |
39 |
| - * Once the widget is registered, the home screen will try to instantiate it and assign an instance id. |
40 |
| - * This will be used by the home screen to manage all widgets and listen to events. |
41 |
| - * In order for this to work, we therefore need a vue prop called 'widgetInstanceId'. |
42 |
| - */ |
| 32 | + computed: { |
| 33 | + ...mapState(['profiles', 'initialized']), |
| 34 | + }, |
43 | 35 | props: {
|
44 |
| - widgetInstanceId: { |
45 |
| - type: String, |
46 |
| - required: true, |
47 |
| - }, |
48 |
| -
|
49 |
| - /** |
50 |
| - * Step 7: Define customizable input props (Optional) |
51 |
| - * |
52 |
| - * Every widget can have input properties that can be customized by admins. |
53 |
| - * In this example for different widget instances one might want to display more or fewer profiles. |
54 |
| - * The default is now set to 50. |
55 |
| - * |
56 |
| - */ |
57 |
| - maxNumberOfDisplayedProfiles: { |
58 |
| - type: Number, |
59 |
| - required: false, |
| 36 | + properties: { |
| 37 | + type: Object, |
60 | 38 | default() {
|
61 |
| - return PROFILE_LIMIT; |
62 |
| - }, |
| 39 | + return { |
| 40 | + maxNumberOfDisplayedProfiles: 12, |
| 41 | + }; |
| 42 | + } |
63 | 43 | },
|
64 | 44 | },
|
65 |
| - computed: { |
66 |
| - ...mapState(['profiles', 'initialized']), |
67 |
| - }, |
68 | 45 | watch: {
|
69 | 46 | /**
|
70 |
| - * Step 6: Trigger LOADED event |
| 47 | + * Step 3: Trigger `widget-loaded` event |
71 | 48 | *
|
72 | 49 | * Once the profiles are successfully loaded we notify the home screen by triggering an event
|
73 |
| - * that contains the event type and the widget instance id |
74 | 50 | */
|
75 | 51 | initialized(newVal, oldVal) {
|
76 | 52 | if (newVal && newVal !== oldVal) {
|
77 |
| - BeekeeperHomeScreen.triggerEvent(EventType.LOADED, this.widgetInstanceId); |
| 53 | + this.$emit('widget-loaded'); |
78 | 54 | }
|
79 | 55 | },
|
80 | 56 | },
|
81 |
| - beforeCreate() { |
82 |
| - /** |
83 |
| - * Step 4: Register widget store module (Optional: not needed for a static widget without store) |
84 |
| - * |
85 |
| - * In order for Vuex to be aware of this store, we need to register it upon widget creation. |
86 |
| - * When the module is registered, all of its getters, actions and mutations will be automatically |
87 |
| - * namespaced based on the path the module is registered at. |
88 |
| - */ |
89 |
| - this.$store.registerModule(WIDGET_TYPE, store, { preserveState: false }); |
| 57 | + computed: { |
| 58 | + displayedProfiles() { |
| 59 | + return this.properties?.maxNumberOfDisplayedProfiles || PROFILE_LIMIT; |
| 60 | + }, |
90 | 61 | },
|
91 | 62 | created() {
|
92 |
| - this.initStore(this.maxNumberOfDisplayedProfiles); |
| 63 | + this.initStore(displayedProfiles); |
93 | 64 | },
|
94 | 65 | methods: {
|
95 | 66 | ...mapActions({
|
|
0 commit comments