|
| 1 | +<template> |
| 2 | +<div class="example-wrapper"> |
| 3 | + <template v-if="images"> |
| 4 | + <div v-for="(image, index) in images" :key="index" class="image-row"> |
| 5 | + <p class="picture-title">{{ image.title }}</p> |
| 6 | + <img class="picture-result" :src="image.path"/> |
| 7 | + </div> |
| 8 | + </template> |
| 9 | +</div> |
| 10 | +</template> |
| 11 | + |
| 12 | +<script> |
| 13 | +/** |
| 14 | + * A simple example which you can use as a template for creating your own widget. |
| 15 | + * Takes two optional parameters (`text` and `count`), and fetches a list of images |
| 16 | + * from dummyapis.com, then renders the results to the UI. |
| 17 | + */ |
| 18 | +
|
| 19 | +import axios from 'axios'; |
| 20 | +import WidgetMixin from '@/mixins/WidgetMixin'; |
| 21 | +import { widgetApiEndpoints } from '@/utils/defaults'; |
| 22 | +
|
| 23 | +export default { |
| 24 | + mixins: [WidgetMixin], |
| 25 | + components: {}, |
| 26 | + data() { |
| 27 | + return { |
| 28 | + images: null, // Will store our results from the API |
| 29 | + }; |
| 30 | + }, |
| 31 | + mounted() { |
| 32 | + this.fetchData(); |
| 33 | + }, |
| 34 | + computed: { |
| 35 | + /* Get the users chosen number of results, from this.options.count |
| 36 | + * If not present, or not a number, then return the default (5) |
| 37 | + */ |
| 38 | + count() { |
| 39 | + const usersChoice = this.options.count; |
| 40 | + if (!usersChoice || !Number.isNaN(usersChoice)) { |
| 41 | + return 5; |
| 42 | + } |
| 43 | + return usersChoice; |
| 44 | + }, |
| 45 | + /* Get users desired image text, or return `Dashy` */ |
| 46 | + text() { |
| 47 | + const usersChoice = this.options.text; |
| 48 | + if (!usersChoice) return 'Dashy'; |
| 49 | + return usersChoice; |
| 50 | + }, |
| 51 | + /* Generate the data endpoint for the API request */ |
| 52 | + endpoint() { |
| 53 | + return `${widgetApiEndpoints.exampleEndpoint}?text=${this.text}&noofimages=${this.count}`; |
| 54 | + }, |
| 55 | + }, |
| 56 | + methods: { |
| 57 | + /* The update() method extends mixin, used to update the data. |
| 58 | + * It's called by parent component, when the user presses update |
| 59 | + */ |
| 60 | + update() { |
| 61 | + this.startLoading(); |
| 62 | + this.fetchData(); |
| 63 | + }, |
| 64 | + /* Make the data request to the computed API endpoint */ |
| 65 | + fetchData() { |
| 66 | + axios.get(this.endpoint) |
| 67 | + .then((response) => { |
| 68 | + // The request has completed successfully, call function to process the data |
| 69 | + this.processData(response.data); |
| 70 | + }) |
| 71 | + .catch((dataFetchError) => { |
| 72 | + // If an error occurs, then calling this.error() will handle this gracefully |
| 73 | + this.error('Unable to fetch data', dataFetchError); |
| 74 | + }) |
| 75 | + .finally(() => { |
| 76 | + // When the request is done, hide the loader |
| 77 | + this.finishLoading(); |
| 78 | + }); |
| 79 | + }, |
| 80 | + /* Convert API response data into a format to be consumed by the UI */ |
| 81 | + processData(response) { |
| 82 | + const results = []; |
| 83 | + response.forEach((image, index) => { |
| 84 | + results.push({ |
| 85 | + path: image, |
| 86 | + title: `Image ${index + 1}`, |
| 87 | + }); |
| 88 | + }); |
| 89 | + // Now, in the HTML, we can reference the `images` array |
| 90 | + this.images = results; |
| 91 | + }, |
| 92 | + }, |
| 93 | +}; |
| 94 | +</script> |
| 95 | + |
| 96 | +<style scoped lang="scss"> |
| 97 | +.example-wrapper { |
| 98 | + .image-row { |
| 99 | + display: flex; |
| 100 | + align-items: center; |
| 101 | + justify-content: space-around; |
| 102 | + p.picture-title { |
| 103 | + font-size: 1.2rem; |
| 104 | + color: var(--widget-text-color); |
| 105 | + } |
| 106 | + img.picture-result { |
| 107 | + width: 4rem; |
| 108 | + margin: 0.5rem 0; |
| 109 | + border-radius: var(--curve-factor); |
| 110 | + } |
| 111 | + &:not(:last-child) { |
| 112 | + border-bottom: 1px dashed var(--widget-text-color); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | +
|
| 117 | +</style> |
0 commit comments