11<script setup lang="ts">
22import { ref } from ' vue' ;
3- import { useForm } from ' @inertiajs/vue3 ' ;
3+ import axios from ' axios ' ;
44import ActionSection from ' @/Components/ActionSection.vue' ;
55import DangerButton from ' @/packages/ui/src/Buttons/DangerButton.vue' ;
66import DialogModal from ' @/packages/ui/src/DialogModal.vue' ;
77import { Field , FieldError } from ' @/packages/ui/src/field' ;
88import SecondaryButton from ' @/packages/ui/src/Buttons/SecondaryButton.vue' ;
99import TextInput from ' @/packages/ui/src/Input/TextInput.vue' ;
10+ import { useDeleteUserMutation , useUserQuery } from ' @/utils/useUserQuery' ;
1011
11- const confirmingUserDeletion = ref ( false );
12- const passwordInput = ref < HTMLElement | null >( null );
12+ const { user } = useUserQuery ( );
13+ const deleteUserMutation = useDeleteUserMutation ( );
1314
14- const form = useForm ({
15- password: ' ' ,
16- });
15+ const confirmingUserDeletion = ref (false );
16+ const passwordInput = ref <HTMLInputElement | null >(null );
17+ const password = ref (' ' );
18+ const passwordError = ref (' ' );
19+ const processing = ref (false );
1720
18- const confirmUserDeletion = () => {
21+ function confirmUserDeletion() {
1922 confirmingUserDeletion .value = true ;
20-
2123 setTimeout (() => passwordInput .value ?.focus (), 250 );
22- };
23-
24- const deleteUser = () => {
25- form .delete (route (' current-user.destroy' ), {
26- preserveScroll: true ,
27- onSuccess : () => closeModal (),
28- onError : () => passwordInput .value ?.focus (),
29- onFinish : () => form .reset (),
30- });
31- };
32-
33- const closeModal = () => {
24+ }
25+
26+ async function deleteUser() {
27+ if (! user .value || processing .value ) return ;
28+ processing .value = true ;
29+ passwordError .value = ' ' ;
30+ try {
31+ await axios .post (route (' password.confirm' ), { password: password .value });
32+ } catch (error ) {
33+ processing .value = false ;
34+ if (axios .isAxiosError (error ) && error .response ?.status === 422 ) {
35+ passwordError .value = error .response .data ?.errors ?.password ?.[0 ] ?? ' Invalid password.' ;
36+ } else {
37+ passwordError .value = ' Could not confirm password. Please try again.' ;
38+ }
39+ passwordInput .value ?.focus ();
40+ return ;
41+ }
42+ try {
43+ await deleteUserMutation .mutateAsync (user .value .id );
44+ window .location .href = ' /' ;
45+ } catch {
46+ processing .value = false ;
47+ }
48+ }
49+
50+ function closeModal() {
3451 confirmingUserDeletion .value = false ;
35-
36- form . reset () ;
37- };
52+ password . value = ' ' ;
53+ passwordError . value = ' ' ;
54+ }
3855 </script >
3956
4057<template >
@@ -66,16 +83,14 @@ const closeModal = () => {
6683 <Field class="mt-4">
6784 <TextInput
6885 ref="passwordInput"
69- v-model =" form . password "
86+ v-model =" password "
7087 type="password"
7188 class="block w-3/4"
7289 placeholder="Password"
7390 autocomplete="current-password"
7491 @keyup .enter =" deleteUser " />
7592
76- <FieldError v-if =" form .errors .password " >{{
77- form.errors.password
78- }}</FieldError >
93+ <FieldError v-if =" passwordError " >{{ passwordError }}</FieldError >
7994 </Field >
8095 </template >
8196
@@ -84,8 +99,8 @@ const closeModal = () => {
8499
85100 <DangerButton
86101 class="ms-3"
87- :class =" { ' opacity-25' : form . processing } "
88- :disabled =" form . processing "
102+ :class =" { ' opacity-25' : processing } "
103+ :disabled =" processing "
89104 @click =" deleteUser " >
90105 Delete Account
91106 </DangerButton >
0 commit comments