|
| 1 | +<script setup lang="ts"> |
| 2 | +import axios from 'axios' |
| 3 | +import { Ref, nextTick, reactive, ref } from 'vue' |
| 4 | +import ConfirmablePasswordController from '@/wayfinder/actions/Laravel/Fortify/Http/Controllers/ConfirmablePasswordController' |
| 5 | +import { confirmation } from '@/wayfinder/routes/password' |
| 6 | +
|
| 7 | +const emit = defineEmits(['confirmed']) |
| 8 | +
|
| 9 | +const confirmingPassword = ref(false) |
| 10 | +
|
| 11 | +const form = reactive({ |
| 12 | + password: '', |
| 13 | + error: '', |
| 14 | + processing: false, |
| 15 | +}) |
| 16 | +
|
| 17 | +const passwordInput: Ref<HTMLInputElement | null> = ref(null) |
| 18 | +
|
| 19 | +const startConfirmingPassword = () => { |
| 20 | + axios.get(confirmation.url()).then(response => { |
| 21 | + if (response.data.confirmed) { |
| 22 | + emit('confirmed') |
| 23 | + } else { |
| 24 | + confirmingPassword.value = true |
| 25 | +
|
| 26 | + nextTick(() => passwordInput.value?.focus()) |
| 27 | + } |
| 28 | + }) |
| 29 | +} |
| 30 | +
|
| 31 | +const confirmPassword = () => { |
| 32 | + form.processing = true |
| 33 | +
|
| 34 | + axios.post(ConfirmablePasswordController.store.url(), { |
| 35 | + password: form.password, |
| 36 | + }).then(() => { |
| 37 | + form.processing = false |
| 38 | +
|
| 39 | + closeModal() |
| 40 | + nextTick().then(() => emit('confirmed')) |
| 41 | + }).catch(error => { |
| 42 | + form.processing = false |
| 43 | + form.error = error.response.data.errors.password[0] |
| 44 | + passwordInput.value?.focus() |
| 45 | + }) |
| 46 | +} |
| 47 | +
|
| 48 | +const closeModal = () => { |
| 49 | + confirmingPassword.value = false |
| 50 | + form.password = '' |
| 51 | + form.error = '' |
| 52 | +} |
| 53 | +</script> |
| 54 | + |
| 55 | +<template> |
| 56 | + <span> |
| 57 | + <span @click="startConfirmingPassword"> |
| 58 | + <slot/> |
| 59 | + </span> |
| 60 | + |
| 61 | + <dialog :open="confirmingPassword"> |
| 62 | + <form> |
| 63 | + <input |
| 64 | + type="text" |
| 65 | + name="email" |
| 66 | + autocomplete="username email" |
| 67 | + style="display: none;" |
| 68 | + > |
| 69 | + |
| 70 | + <div> |
| 71 | + <input |
| 72 | + ref="passwordInput" |
| 73 | + v-model="form.password" |
| 74 | + type="password" |
| 75 | + placeholder="Password" |
| 76 | + autocomplete="current-password" |
| 77 | + @keyup.enter="confirmPassword" |
| 78 | + > |
| 79 | + |
| 80 | + <div v-if="form.error"> |
| 81 | + <mark>{{ form.error }}</mark> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + |
| 85 | + <button |
| 86 | + :disabled="form.processing" |
| 87 | + @click="confirmPassword" |
| 88 | + > |
| 89 | + Confirm |
| 90 | + </button> |
| 91 | + |
| 92 | + <button type="button" @click="closeModal"> |
| 93 | + Cancel |
| 94 | + </button> |
| 95 | + </form> |
| 96 | + </dialog> |
| 97 | + </span> |
| 98 | +</template> |
0 commit comments