Skip to content

Commit cd380c4

Browse files
committed
refactor: use built-in form support of NcDialog
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent fc2e027 commit cd380c4

2 files changed

Lines changed: 24 additions & 65 deletions

File tree

src/components/PasswordDialog.vue

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@
66
<script setup lang="ts">
77
import { isAxiosError } from '@nextcloud/axios'
88
import { computed, nextTick, onMounted, ref, useTemplateRef } from 'vue'
9-
import NcButton from '@nextcloud/vue/components/NcButton'
109
import NcDialog from '@nextcloud/vue/components/NcDialog'
11-
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
1210
import NcPasswordField from '@nextcloud/vue/components/NcPasswordField'
1311
import { t } from '../utils/l10n.js'
1412
import { logger } from '../utils/logger.js'
1513
16-
type ICanFocus = {
17-
focus: () => void
18-
select: () => void
19-
}
14+
type DialogButtons = InstanceType<typeof NcDialog>['$props']['buttons']
2015
2116
const props = defineProps<{
2217
/**
@@ -31,12 +26,19 @@ const emit = defineEmits<{
3126
3227
onMounted(focusPasswordField)
3328
34-
const passwordInput = useTemplateRef<ICanFocus>('field')
29+
const passwordInput = useTemplateRef('field')
3530
3631
const password = ref('')
3732
const loading = ref(false)
3833
const hasError = ref<boolean | 403>(false)
3934
35+
const buttons: DialogButtons = [{
36+
label: t('Confirm'),
37+
type: 'submit',
38+
variant: 'primary',
39+
callback,
40+
}]
41+
4042
const helperText = computed(() => {
4143
if (hasError.value !== false) {
4244
if (password.value === '') {
@@ -61,18 +63,19 @@ const helperText = computed(() => {
6163
/**
6264
* Handle confirm button click
6365
*/
64-
async function confirm(): Promise<void> {
66+
async function callback(): Promise<boolean> {
6567
hasError.value = false
6668
loading.value = true
6769
6870
if (password.value === '') {
6971
hasError.value = true
70-
return
72+
return false
7173
}
7274
7375
try {
7476
await props.validate(password.value)
7577
emit('close', true)
78+
return true
7679
} catch (error) {
7780
if (isAxiosError(error) && error.response?.status === 403) {
7881
hasError.value = 403
@@ -82,22 +85,12 @@ async function confirm(): Promise<void> {
8285
8386
logger.error('Exception during password confirmation', { error })
8487
selectPasswordField()
88+
return false
8589
} finally {
8690
loading.value = false
8791
}
8892
}
8993
90-
/**
91-
* Handle the close event.
92-
*
93-
* @param open - The new status
94-
*/
95-
function close(open: boolean): void {
96-
if (!open) {
97-
emit('close', false)
98-
}
99-
}
100-
10194
/**
10295
* Focus the password field
10396
*/
@@ -119,51 +112,28 @@ function selectPasswordField() {
119112

120113
<template>
121114
<NcDialog
115+
is-form
116+
:buttons
122117
:name="t('Authentication required')"
123118
:content-classes="$style.passwordDialog"
124-
@update:open="close">
125-
<!-- Dialog content -->
119+
@update:open="emit('close', false)">
126120
<p>{{ t('This action needs authentication, please confirm it by entering your password.') }}</p>
127-
<form :class="$style.passwordDialogForm" @submit.prevent="confirm">
128-
<NcPasswordField
129-
ref="field"
130-
v-model="password"
131-
:label="t('Password')"
132-
:helper-text="helperText"
133-
:error="hasError !== false"
134-
required />
135-
<NcButton
136-
:class="$style.passwordDialogSubmit"
137-
variant="primary"
138-
type="submit"
139-
:disabled="!password || loading">
140-
<template v-if="loading" #icon>
141-
<NcLoadingIcon :size="20" />
142-
</template>
143-
{{ t('Confirm') }}
144-
</NcButton>
145-
</form>
121+
<NcPasswordField
122+
ref="field"
123+
v-model="password"
124+
:label="t('Password')"
125+
:helper-text="helperText"
126+
:error="hasError !== false"
127+
required />
146128
</NcDialog>
147129
</template>
148130

149131
<style module>
150132
.passwordDialog {
151133
display: flex;
152134
flex-direction: column;
135+
gap: 10px 0;
153136
margin-inline: 6px;
154137
margin-block-end: 6px;
155-
gap: 10px 0;
156-
}
157-
158-
.passwordDialogForm {
159-
display: flex;
160-
flex-direction: column;
161-
gap: 8px 0;
162-
/* allow focus visible outlines */
163-
padding: 2px;
164-
}
165-
166-
.passwordDialogSubmit {
167-
align-self: end;
168138
}
169139
</style>

src/vue-shims.d.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)