-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCanisterSnapshotCreateDialog.vue
More file actions
139 lines (125 loc) · 3.57 KB
/
CanisterSnapshotCreateDialog.vue
File metadata and controls
139 lines (125 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<template>
<VDialog
v-bind="$attrs"
v-model="open"
:persistent="!canClose"
transition="dialog-bottom-transition"
scrollable
:max-width="props.dialogMaxWidth"
>
<VCard data-test-id="canister-snapshot-create-card">
<VToolbar color="background">
<VToolbarTitle>
{{ dialogTitle }}
</VToolbarTitle>
<VBtn :disabled="!canClose" :icon="mdiClose" @click="open = false" />
</VToolbar>
<VDivider />
<VCardText>
{{ $t('external_canisters.snapshots.create_snapshot_description') }}
<VDivider class="my-4" />
<VTextarea
v-model="canisterCreateSnapshotModel.comment"
name="comment"
class="mt-2"
:prepend-inner-icon="mdiComment"
:label="$t(`requests.comment_optional`)"
variant="filled"
density="comfortable"
auto-grow
rows="2"
hide-details
/>
</VCardText>
<VCardActions class="pa-3">
<VSpacer />
<VBtn
:loading="saving"
color="primary"
data-test-id="submit-btn"
variant="elevated"
@click="createSnapshot"
>
{{ $t('terms.create') }}
</VBtn>
</VCardActions>
</VCard>
</VDialog>
</template>
<script lang="ts" setup>
import { Principal } from '@icp-sdk/core/principal';
import { mdiClose, mdiComment } from '@mdi/js';
import { Ref, computed, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { VBtn, VCard, VDialog, VDivider, VToolbar, VToolbarTitle } from 'vuetify/components';
import { CanisterCreateSnapshotModel } from './external-canisters.types';
import { useStationStore } from '~/stores/station.store';
import { assertAndReturn } from '~/utils/helper.utils';
import {
useOnFailedOperation,
useOnSuccessfulOperation,
} from '~/composables/notifications.composable';
import logger from '~/core/logger.core';
const props = withDefaults(
defineProps<{
open?: boolean;
canisterId: Principal;
dialogMaxWidth?: number;
title?: string;
}>(),
{
open: false,
dialogMaxWidth: 800,
title: undefined,
},
);
const emit = defineEmits<{
(event: 'update:open', payload: boolean): void;
}>();
const i18n = useI18n();
const canClose = ref(true);
const dialogTitle = computed(
() => props.title || i18n.t('external_canisters.snapshots.create_snapshot'),
);
const initialModel = (): CanisterCreateSnapshotModel => {
const model: CanisterCreateSnapshotModel = {
canisterId: Principal.fromUint8Array(props.canisterId.toUint8Array()),
};
return model;
};
const saving = ref(false);
const station = useStationStore();
const canisterCreateSnapshotModel = ref(initialModel()) as Ref<CanisterCreateSnapshotModel>;
const open = computed({
get: () => props.open,
set: isOpen => emit('update:open', isOpen),
});
watch(
open,
isOpen => {
if (isOpen) {
canisterCreateSnapshotModel.value = initialModel();
}
},
{ immediate: true },
);
const createSnapshot = async (): Promise<void> => {
saving.value = true;
try {
const canisterId = assertAndReturn(
canisterCreateSnapshotModel.value.canisterId,
'model.canisterId',
);
const newRequest = await station.service.createExternalCanisterSnapshot(canisterId, {
comment: canisterCreateSnapshotModel.value.comment,
});
useOnSuccessfulOperation(newRequest);
open.value = false;
} catch (error) {
logger.error(`Failed to request canister snapshot creation ${error}`);
useOnFailedOperation();
} finally {
saving.value = false;
}
};
</script>