|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +<template> |
| 19 | + <div> |
| 20 | + <div v-for="advisory in advisories" :key="advisory.id" style="margin-bottom: 10px;"> |
| 21 | + <a-alert |
| 22 | + :type="advisory.severity || 'info'" |
| 23 | + :show-icon="true" |
| 24 | + :closable="true" |
| 25 | + :message="$t(advisory.message)" |
| 26 | + @close="onAlertClose(advisory)"> |
| 27 | + <template #description> |
| 28 | + <a-space direction="horizontal" size="small"> |
| 29 | + <span v-for="(action, idx) in advisory.actions" :key="idx"> |
| 30 | + <a-button |
| 31 | + v-if="typeof action.show === 'function' ? action.show($store) : action.show" |
| 32 | + size="small" |
| 33 | + :type="(action.primary || advisory.actions.length === 1) ? 'primary' : 'default'" |
| 34 | + @click="onAlertBtnClick(action, advisory)"> |
| 35 | + {{ $t(action.label) }} |
| 36 | + </a-button> |
| 37 | + </span> |
| 38 | + </a-space> |
| 39 | + </template> |
| 40 | + </a-alert> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | +</template> |
| 44 | + |
| 45 | +<script> |
| 46 | +
|
| 47 | +const DISMISSED_ADVISORIES_KEY = 'dismissed_advisories' |
| 48 | +
|
| 49 | +export default { |
| 50 | + name: 'AdvisoriesView', |
| 51 | + components: { |
| 52 | + }, |
| 53 | + props: {}, |
| 54 | + data () { |
| 55 | + return { |
| 56 | + advisories: [] |
| 57 | + } |
| 58 | + }, |
| 59 | + created () { |
| 60 | + this.evaluateAdvisories() |
| 61 | + }, |
| 62 | + computed: { |
| 63 | + }, |
| 64 | + methods: { |
| 65 | + evaluateAdvisories () { |
| 66 | + this.advisories = [] |
| 67 | + const metaAdvisories = this.$route.meta.advisories || [] |
| 68 | + const dismissedAdvisories = this.$localStorage.get(DISMISSED_ADVISORIES_KEY) || [] |
| 69 | + for (const advisory of metaAdvisories) { |
| 70 | + if (dismissedAdvisories.includes(advisory.id)) { |
| 71 | + continue |
| 72 | + } |
| 73 | + Promise.resolve(advisory.condition(this.$store)).then(active => { |
| 74 | + if (active) { |
| 75 | + this.advisories.push(advisory) |
| 76 | + } else if (advisory.dismissOnConditionFail) { |
| 77 | + this.dismissAdvisory(advisory.id, true) |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | + }, |
| 82 | + onAlertClose (advisory) { |
| 83 | + this.dismissAdvisory(advisory.id) |
| 84 | + }, |
| 85 | + dismissAdvisory (advisoryId, skipUpdateLocal) { |
| 86 | + let dismissedAdvisories = this.$localStorage.get(DISMISSED_ADVISORIES_KEY) || [] |
| 87 | + dismissedAdvisories = dismissedAdvisories.filter(id => id !== advisoryId) |
| 88 | + dismissedAdvisories.push(advisoryId) |
| 89 | + this.$localStorage.set(DISMISSED_ADVISORIES_KEY, dismissedAdvisories) |
| 90 | + if (skipUpdateLocal) { |
| 91 | + return |
| 92 | + } |
| 93 | + this.advisories = this.advisories.filter(advisory => advisory.id !== advisoryId) |
| 94 | + }, |
| 95 | + undismissAdvisory (advisory, evaluate) { |
| 96 | + let dismissedAdvisories = this.$localStorage.get(DISMISSED_ADVISORIES_KEY) || [] |
| 97 | + dismissedAdvisories = dismissedAdvisories.filter(id => id !== advisory.id) |
| 98 | + this.$localStorage.set(DISMISSED_ADVISORIES_KEY, dismissedAdvisories) |
| 99 | + if (evaluate) { |
| 100 | + Promise.resolve(advisory.condition(this.$store)).then(active => { |
| 101 | + if (active) { |
| 102 | + this.advisories.push(advisory) |
| 103 | + } |
| 104 | + }) |
| 105 | + } else { |
| 106 | + this.advisories.push(advisory) |
| 107 | + } |
| 108 | + }, |
| 109 | + handleAdvisoryActionError (action, advisory, evaluate) { |
| 110 | + if (action.errorMessage) { |
| 111 | + this.showActionMessage('error', advisory.id, action.errorMessage) |
| 112 | + } |
| 113 | + this.undismissAdvisory(advisory, evaluate) |
| 114 | + }, |
| 115 | + handleAdvisoryActionResult (action, advisory, result) { |
| 116 | + if (result && action.successMessage) { |
| 117 | + this.showActionMessage('success', advisory.id, action.successMessage) |
| 118 | + return |
| 119 | + } |
| 120 | + this.handleAdvisoryActionError(action, advisory, false) |
| 121 | + }, |
| 122 | + showActionMessage (type, key, content) { |
| 123 | + const data = { |
| 124 | + content: this.$t(content), |
| 125 | + key: key, |
| 126 | + duration: type === 'loading' ? 0 : 3 |
| 127 | + } |
| 128 | + if (type === 'loading') { |
| 129 | + this.$message.loading(data) |
| 130 | + } else if (type === 'success') { |
| 131 | + this.$message.success(data) |
| 132 | + } else if (type === 'error') { |
| 133 | + this.$message.error(data) |
| 134 | + } else { |
| 135 | + this.$message.info(data) |
| 136 | + } |
| 137 | + }, |
| 138 | + onAlertBtnClick (action, advisory) { |
| 139 | + this.dismissAdvisory(advisory.id) |
| 140 | + if (typeof action.run !== 'function') { |
| 141 | + return |
| 142 | + } |
| 143 | + if (action.loadingLabel) { |
| 144 | + this.showActionMessage('loading', advisory.id, action.loadingLabel) |
| 145 | + } |
| 146 | + const result = action.run(this.$store, this.$router) |
| 147 | + if (result instanceof Promise) { |
| 148 | + result.then(success => { |
| 149 | + this.handleAdvisoryActionResult(action, advisory, success) |
| 150 | + }).catch(() => { |
| 151 | + this.handleAdvisoryActionError(action, advisory, true) |
| 152 | + }) |
| 153 | + } else { |
| 154 | + this.handleAdvisoryActionResult(action, advisory, result) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +</script> |
0 commit comments