Files
cloudron-box/dashboard/src/components/FallbackEmailDialog.vue

102 lines
2.8 KiB
Vue
Raw Normal View History

2025-06-11 10:22:09 +02:00
<script setup>
2025-12-05 18:06:03 +01:00
import { ref, useTemplateRef } from 'vue';
2025-07-10 11:55:11 +02:00
import { EmailInput, PasswordInput, Dialog, FormGroup } from '@cloudron/pankow';
import { isValidEmail } from '@cloudron/pankow/utils';
2025-11-13 16:10:27 +01:00
import ProfileModel from '../models/ProfileModel.js';
2025-06-11 10:22:09 +02:00
const emit = defineEmits([ 'success' ]);
const profileModel = ProfileModel.create();
const dialog = useTemplateRef('dialog');
const formError = ref({});
const busy = ref (false);
const email = ref('');
const password = ref('');
2025-12-05 18:06:03 +01:00
const form = useTemplateRef('form');
const isFormValid = ref(false);
function checkValidity() {
isFormValid.value = form.value ? form.value.checkValidity() : false;
2025-06-11 10:22:09 +02:00
2025-12-05 18:06:03 +01:00
if (isFormValid.value) {
if (!isValidEmail(email.value)) isFormValid.value = false;
}
}
2025-06-11 10:22:09 +02:00
async function onSubmit() {
2025-12-05 18:06:03 +01:00
if (!form.value.reportValidity()) return;
2025-06-11 10:22:09 +02:00
busy.value = true;
formError.value = {};
const [error] = await profileModel.setFallbackEmail(email.value, password.value);
if (error) {
if (error.status === 400) formError.value.email = error.body.message;
else if (error.status === 412) formError.value.password = error.body.message;
2025-06-11 10:34:58 +02:00
else {
formError.value.generic = error.status ? error.body.message : 'Internal error';
console.error('Failed to set fallback email', error);
}
2025-06-11 10:22:09 +02:00
busy.value = false;
2025-06-11 10:34:58 +02:00
return;
2025-06-11 10:22:09 +02:00
}
emit('success');
dialog.value.close();
busy.value = false;
formError.value = {};
}
defineExpose({
async open(e) {
email.value = e;
password.value = '';
busy.value = false;
formError.value = {};
dialog.value.open();
2025-12-05 18:06:03 +01:00
setTimeout(checkValidity, 100); // update state of the confirm button
2025-06-11 10:22:09 +02:00
}
});
</script>
<template>
<Dialog ref="dialog"
:title="$t('profile.changeFallbackEmail.title')"
:confirm-label="$t('main.dialog.save')"
:confirm-active="!busy && isFormValid"
:confirm-busy="busy"
confirm-style="primary"
:reject-label="$t('main.dialog.cancel')"
:reject-active="!busy"
reject-style="secondary"
@confirm="onSubmit"
>
2025-12-05 18:06:03 +01:00
<form @submit.prevent="onSubmit" autocomplete="off" ref="form" @input="checkValidity()">
2025-06-11 10:22:09 +02:00
<fieldset :disabled="busy">
2025-12-05 18:06:03 +01:00
<input type="submit" style="display: none;"/>
2025-06-11 10:22:09 +02:00
<div class="error-label" v-if="formError.generic">{{ formError.generic }}</div>
<FormGroup :has-error="formError.email">
<label>{{ $t('profile.changeEmail.email') }}</label>
2025-12-05 18:06:03 +01:00
<EmailInput v-model="email" required/>
2025-06-11 10:22:09 +02:00
<div class="error-label" v-if="formError.email">{{ formError.email }}</div>
</FormGroup>
<FormGroup :has-error="formError.password">
<label>{{ $t('profile.changeEmail.password') }}</label>
2025-12-05 18:06:03 +01:00
<PasswordInput v-model="password" required/>
2025-06-11 10:22:09 +02:00
<div class="error-label" v-if="formError.password">{{ formError.password }}</div>
</FormGroup>
</fieldset>
</form>
</Dialog>
</template>