mirror of
https://github.com/harvester/harvester-ui-extension.git
synced 2025-12-13 21:21:44 +00:00
feat: add confirm popup
Signed-off-by: Yi-Ya Chen <yiya.chen@suse.com> (cherry picked from commit 526d1bb8753fcb039caf7979e554cc59d72ad550)
This commit is contained in:
parent
752cf2e93a
commit
a8ea0645e4
194
pkg/harvester/dialog/ConfirmExecutionDialog.vue
Normal file
194
pkg/harvester/dialog/ConfirmExecutionDialog.vue
Normal file
@ -0,0 +1,194 @@
|
||||
<script>
|
||||
import { mapState } from 'vuex';
|
||||
import { exceptionToErrorsArray } from '@shell/utils/error';
|
||||
import { alternateLabel } from '@shell/utils/platform';
|
||||
import AsyncButton from '@shell/components/AsyncButton';
|
||||
import { Banner } from '@components/Banner';
|
||||
import { Card } from '@components/Card';
|
||||
import { escapeHtml } from '@shell/utils/string';
|
||||
import CopyToClipboardText from '@shell/components/CopyToClipboardText';
|
||||
|
||||
/**
|
||||
* @name ConfirmExecutionDialog
|
||||
* @description Dialog component to confirm the related resources before executing the action.
|
||||
*/
|
||||
export default {
|
||||
name: 'ConfirmExecutionDialog',
|
||||
|
||||
emits: ['close'],
|
||||
|
||||
components: {
|
||||
AsyncButton,
|
||||
Banner,
|
||||
Card,
|
||||
CopyToClipboardText
|
||||
},
|
||||
|
||||
props: {
|
||||
/**
|
||||
* @property resources to be deleted.
|
||||
* @type {Resource[]} Array of the resource model's instance
|
||||
*/
|
||||
resources: {
|
||||
type: Array,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
|
||||
data() {
|
||||
return { errors: [], confirmName: '' };
|
||||
},
|
||||
|
||||
computed: {
|
||||
...mapState('action-menu', ['modalData']),
|
||||
|
||||
warningMessageKey() {
|
||||
return this.modalData.warningMessageKey;
|
||||
},
|
||||
|
||||
names() {
|
||||
return this.resources.map((obj) => obj.nameDisplay).slice(0, 5);
|
||||
},
|
||||
|
||||
resourceNames() {
|
||||
return this.names.reduce((res, name, i) => {
|
||||
if (i >= 5) {
|
||||
return res;
|
||||
}
|
||||
res += `<b>${ escapeHtml(name) }</b>`;
|
||||
if (i === this.names.length - 1) {
|
||||
res += this.plusMore;
|
||||
} else {
|
||||
res += i === this.resources.length - 2 ? ' and ' : ', ';
|
||||
}
|
||||
|
||||
return res;
|
||||
}, '');
|
||||
},
|
||||
|
||||
nameToMatch() {
|
||||
return this.resources[0].nameDisplay;
|
||||
},
|
||||
|
||||
plusMore() {
|
||||
const remaining = this.resources.length - this.names.length;
|
||||
|
||||
return this.t('dialog.confirmExecution.andOthers', { count: remaining });
|
||||
},
|
||||
|
||||
type() {
|
||||
const types = new Set(this.resources.reduce((array, each) => {
|
||||
array.push(each.type);
|
||||
|
||||
return array;
|
||||
}, []));
|
||||
|
||||
if (types.size > 1) {
|
||||
return this.t('generic.resource', { count: this.resources.length });
|
||||
}
|
||||
|
||||
const schema = this.resources[0]?.schema;
|
||||
|
||||
if ( !schema ) {
|
||||
return `resource${ this.resources.length === 1 ? '' : 's' }`;
|
||||
}
|
||||
|
||||
return this.$store.getters['type-map/labelFor'](schema, this.resources.length);
|
||||
},
|
||||
|
||||
applyDisabled() {
|
||||
return this.confirmName !== this.nameToMatch;
|
||||
},
|
||||
|
||||
protip() {
|
||||
return this.t('dialog.confirmExecution.protip', { alternateLabel });
|
||||
},
|
||||
},
|
||||
|
||||
methods: {
|
||||
escapeHtml,
|
||||
|
||||
close() {
|
||||
this.confirmName = '';
|
||||
this.errors = [];
|
||||
this.$emit('close');
|
||||
},
|
||||
|
||||
async apply(buttonDone) {
|
||||
try {
|
||||
for (const resource of this.resources) {
|
||||
await resource.doActionGrowl(this.modalData.action, {});
|
||||
}
|
||||
buttonDone(true);
|
||||
this.close();
|
||||
} catch (e) {
|
||||
this.errors = exceptionToErrorsArray(e);
|
||||
buttonDone(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Card
|
||||
class="prompt-related"
|
||||
:show-highlight-border="false"
|
||||
>
|
||||
<template #title>
|
||||
<h4 class="text-default-text">
|
||||
{{ t('dialog.confirmExecution.title') }}
|
||||
</h4>
|
||||
</template>
|
||||
|
||||
<template #body>
|
||||
<div class="pl-10 pr-10">
|
||||
<span
|
||||
v-clean-html="t(warningMessageKey, { type, names: resourceNames }, true)"
|
||||
></span>
|
||||
|
||||
<div class="mt-10 mb-10">
|
||||
<span
|
||||
v-clean-html="t('dialog.confirmExecution.confirmName', { nameToMatch: escapeHtml(nameToMatch) }, true)"
|
||||
></span>
|
||||
</div>
|
||||
<div class="mb-10">
|
||||
<CopyToClipboardText :text="nameToMatch" />
|
||||
</div>
|
||||
<input
|
||||
id="confirm"
|
||||
v-model="confirmName"
|
||||
type="text"
|
||||
/>
|
||||
<div class="text-info mt-20">
|
||||
{{ protip }}
|
||||
</div>
|
||||
<Banner
|
||||
v-for="(error, i) in errors"
|
||||
:key="i"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #actions>
|
||||
<button
|
||||
class="btn role-secondary mr-10"
|
||||
@click="close"
|
||||
>
|
||||
{{ t('generic.cancel') }}
|
||||
</button>
|
||||
<AsyncButton
|
||||
mode="apply"
|
||||
class="btn bg-error ml-10"
|
||||
:disabled="applyDisabled"
|
||||
@click="apply"
|
||||
/>
|
||||
</template>
|
||||
</Card>
|
||||
</template>
|
||||
|
||||
<style lang='scss' scoped>
|
||||
.actions {
|
||||
text-align: right;
|
||||
}
|
||||
</style>
|
||||
@ -38,6 +38,22 @@ asyncButton:
|
||||
success: Restarted
|
||||
waiting: Restarting…
|
||||
|
||||
dialog:
|
||||
confirmExecution:
|
||||
title: Are you sure?
|
||||
andOthers: |-
|
||||
{count, plural,
|
||||
=0 {}
|
||||
=1 { and <b>one other </b>}
|
||||
other { and <b>{count} other </b>}
|
||||
}
|
||||
confirmName: "Enter <b>{nameToMatch}</b> below to confirm:"
|
||||
protip: "Tip: Hold the {alternateLabel} key while clicking action to bypass this confirmation"
|
||||
stop:
|
||||
message: "Are you sure you want to continue stop the {type} {names}?"
|
||||
pause:
|
||||
message: "Are you sure you want to continue pause the {type} {names}?"
|
||||
|
||||
harvester:
|
||||
productLabel: 'Harvester'
|
||||
modal:
|
||||
|
||||
@ -103,6 +103,7 @@ export default class VirtVm extends HarvesterResource {
|
||||
return [
|
||||
{
|
||||
action: 'stopVM',
|
||||
altAction: 'altStopVM',
|
||||
enabled: !!this.actions?.stop,
|
||||
icon: 'icon icon-close',
|
||||
label: this.t('harvester.action.stop'),
|
||||
@ -117,6 +118,7 @@ export default class VirtVm extends HarvesterResource {
|
||||
},
|
||||
{
|
||||
action: 'pauseVM',
|
||||
altAction: 'altPauseVM',
|
||||
enabled: !!this.actions?.pause,
|
||||
icon: 'icon icon-pause',
|
||||
label: this.t('harvester.action.pause')
|
||||
@ -402,7 +404,16 @@ export default class VirtVm extends HarvesterResource {
|
||||
return node?.id;
|
||||
}
|
||||
|
||||
pauseVM() {
|
||||
pauseVM(resources = this) {
|
||||
this.$dispatch('promptModal', {
|
||||
resources,
|
||||
action: 'pause',
|
||||
warningMessageKey: 'dialog.confirmExecution.pause.message',
|
||||
component: 'ConfirmExecutionDialog'
|
||||
});
|
||||
}
|
||||
|
||||
altPauseVM() {
|
||||
this.doActionGrowl('pause', {});
|
||||
}
|
||||
|
||||
@ -417,7 +428,16 @@ export default class VirtVm extends HarvesterResource {
|
||||
this.doActionGrowl('unpause', {});
|
||||
}
|
||||
|
||||
stopVM() {
|
||||
stopVM(resources = this) {
|
||||
this.$dispatch('promptModal', {
|
||||
resources,
|
||||
action: 'stop',
|
||||
warningMessageKey: 'dialog.confirmExecution.stop.message',
|
||||
component: 'ConfirmExecutionDialog'
|
||||
});
|
||||
}
|
||||
|
||||
altStopVM() {
|
||||
this.doActionGrowl('stop', {});
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user