mirror of https://github.com/apache/cloudstack.git
113 lines
3.5 KiB
JavaScript
113 lines
3.5 KiB
JavaScript
// Licensed to the Apache Software Foundation (ASF) under one
|
|
// or more contributor license agreements. See the NOTICE file
|
|
// distributed with this work for additional information
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
// to you under the Apache License, Version 2.0 (the
|
|
// "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
import _ from 'lodash'
|
|
import { api } from '@/api'
|
|
import { message, notification } from 'ant-design-vue'
|
|
|
|
export const pollJobPlugin = {
|
|
|
|
install (Vue) {
|
|
Vue.prototype.$pollJob = function (options) {
|
|
/**
|
|
* @param {String} jobId
|
|
* @param {String} [successMessage=Success]
|
|
* @param {Function} [successMethod=() => {}]
|
|
* @param {String} [errorMessage=Error]
|
|
* @param {Function} [errorMethod=() => {}]
|
|
* @param {String} [loadingMessage=Loading...]
|
|
* @param {String} [catchMessage=Error caught]
|
|
* @param {Function} [catchMethod=() => {}]
|
|
* @param {Number} [loadingDuration=3]
|
|
* @param {Object} [action=null]
|
|
*/
|
|
const {
|
|
jobId,
|
|
successMessage = 'Success',
|
|
successMethod = () => {},
|
|
errorMessage = 'Error',
|
|
errorMethod = () => {},
|
|
loadingMessage = 'Loading...',
|
|
catchMessage = 'Error caught',
|
|
catchMethod = () => {},
|
|
loadingDuration = 3,
|
|
action = null
|
|
} = options
|
|
|
|
api('queryAsyncJobResult', { jobId }).then(json => {
|
|
const result = json.queryasyncjobresultresponse
|
|
|
|
if (result.jobstatus === 1) {
|
|
message.success(successMessage)
|
|
successMethod(result)
|
|
} else if (result.jobstatus === 2) {
|
|
notification.error({
|
|
message: errorMessage,
|
|
description: result.jobresult.errortext
|
|
})
|
|
errorMethod(result)
|
|
} else if (result.jobstatus === 0) {
|
|
message
|
|
.loading(loadingMessage, loadingDuration)
|
|
.then(() => this.$pollJob(options, action))
|
|
}
|
|
}).catch(e => {
|
|
console.error(`${catchMessage} - ${e}`)
|
|
notification.error({
|
|
message: 'Error',
|
|
description: catchMessage,
|
|
duration: 0
|
|
})
|
|
catchMethod && catchMethod()
|
|
})
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
export const notifierPlugin = {
|
|
|
|
install (Vue) {
|
|
Vue.prototype.$notifyError = function (error) {
|
|
var msg = 'Request Failed'
|
|
var desc = ''
|
|
if (error && error.response) {
|
|
if (error.response.status) {
|
|
msg = `Request Failed (${error.response.status})`
|
|
}
|
|
if (error.message) {
|
|
desc = error.message
|
|
}
|
|
if (error.response.headers && 'x-description' in error.response.headers) {
|
|
desc = error.response.headers['x-description']
|
|
}
|
|
if (desc === '' && error.response.data) {
|
|
const responseKey = _.findKey(error.response.data, 'errortext')
|
|
if (responseKey) {
|
|
desc = error.response.data[responseKey].errortext
|
|
}
|
|
}
|
|
}
|
|
notification.error({
|
|
message: msg,
|
|
description: desc,
|
|
duration: 0
|
|
})
|
|
}
|
|
}
|
|
}
|