@greatapps/common 1.1.24 → 1.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/dist/components/account/ConfigurationsMyAccountModal.mjs +121 -0
  2. package/dist/components/account/ConfigurationsMyAccountModal.mjs.map +1 -0
  3. package/dist/components/account/ConfirmGlobalPreferencesModal.mjs +45 -0
  4. package/dist/components/account/ConfirmGlobalPreferencesModal.mjs.map +1 -0
  5. package/dist/components/account/DisableTwoFactorAuthModal.mjs +67 -0
  6. package/dist/components/account/DisableTwoFactorAuthModal.mjs.map +1 -0
  7. package/dist/components/account/TwoFactorAuthModal.mjs +190 -0
  8. package/dist/components/account/TwoFactorAuthModal.mjs.map +1 -0
  9. package/dist/components/account/constants.mjs +32 -0
  10. package/dist/components/account/constants.mjs.map +1 -0
  11. package/dist/components/account/hooks/useChangeEmailForm.mjs +53 -0
  12. package/dist/components/account/hooks/useChangeEmailForm.mjs.map +1 -0
  13. package/dist/components/account/hooks/useChangePasswordForm.mjs +47 -0
  14. package/dist/components/account/hooks/useChangePasswordForm.mjs.map +1 -0
  15. package/dist/components/account/hooks/useChangePhoneForm.mjs +64 -0
  16. package/dist/components/account/hooks/useChangePhoneForm.mjs.map +1 -0
  17. package/dist/components/account/hooks/useConfigurationsModal.mjs +33 -0
  18. package/dist/components/account/hooks/useConfigurationsModal.mjs.map +1 -0
  19. package/dist/components/account/hooks/useDisableTwoFactorAuthForm.mjs +54 -0
  20. package/dist/components/account/hooks/useDisableTwoFactorAuthForm.mjs.map +1 -0
  21. package/dist/components/account/hooks/useOtpVerification.mjs +94 -0
  22. package/dist/components/account/hooks/useOtpVerification.mjs.map +1 -0
  23. package/dist/components/account/hooks/useTwoFactorAuthForm.mjs +88 -0
  24. package/dist/components/account/hooks/useTwoFactorAuthForm.mjs.map +1 -0
  25. package/dist/components/account/sections/ChangeEmailModal.mjs +180 -0
  26. package/dist/components/account/sections/ChangeEmailModal.mjs.map +1 -0
  27. package/dist/components/account/sections/ChangePasswordSection.mjs +64 -0
  28. package/dist/components/account/sections/ChangePasswordSection.mjs.map +1 -0
  29. package/dist/components/account/sections/ChangePhoneModal.mjs +182 -0
  30. package/dist/components/account/sections/ChangePhoneModal.mjs.map +1 -0
  31. package/dist/components/account/sections/MyProfileSection.mjs +312 -0
  32. package/dist/components/account/sections/MyProfileSection.mjs.map +1 -0
  33. package/dist/components/account/sections/PreferencesSection.mjs +246 -0
  34. package/dist/components/account/sections/PreferencesSection.mjs.map +1 -0
  35. package/dist/components/account/sections/SecuritySection.mjs +147 -0
  36. package/dist/components/account/sections/SecuritySection.mjs.map +1 -0
  37. package/dist/components/ui/form/ComboboxField.mjs +130 -0
  38. package/dist/components/ui/form/ComboboxField.mjs.map +1 -0
  39. package/dist/components/ui/form/FormField.mjs +103 -0
  40. package/dist/components/ui/form/FormField.mjs.map +1 -0
  41. package/dist/components/ui/form/PhoneInput.mjs +78 -0
  42. package/dist/components/ui/form/PhoneInput.mjs.map +1 -0
  43. package/dist/components/ui/form/SelectField.mjs +80 -0
  44. package/dist/components/ui/form/SelectField.mjs.map +1 -0
  45. package/dist/components/ui/form/SwitchOptionFieldWithIcon.mjs +40 -0
  46. package/dist/components/ui/form/SwitchOptionFieldWithIcon.mjs.map +1 -0
  47. package/dist/components/ui/form/TextAreaField.mjs +56 -0
  48. package/dist/components/ui/form/TextAreaField.mjs.map +1 -0
  49. package/dist/enums/AccountSectionType.mjs +11 -0
  50. package/dist/enums/AccountSectionType.mjs.map +1 -0
  51. package/dist/hooks/useCountdownTimer.mjs +29 -0
  52. package/dist/hooks/useCountdownTimer.mjs.map +1 -0
  53. package/dist/hooks/useIsMobile.mjs +18 -0
  54. package/dist/hooks/useIsMobile.mjs.map +1 -0
  55. package/dist/hooks/usePasswordVisibility.mjs +13 -0
  56. package/dist/hooks/usePasswordVisibility.mjs.map +1 -0
  57. package/dist/index.mjs +80 -0
  58. package/dist/index.mjs.map +1 -1
  59. package/dist/modules/accounts/actions/account-management.action.mjs +163 -0
  60. package/dist/modules/accounts/actions/account-management.action.mjs.map +1 -0
  61. package/dist/modules/accounts/hooks/useAccountManagement.mjs +64 -0
  62. package/dist/modules/accounts/hooks/useAccountManagement.mjs.map +1 -0
  63. package/dist/modules/accounts/services/account.service.mjs +162 -25
  64. package/dist/modules/accounts/services/account.service.mjs.map +1 -1
  65. package/dist/modules/accounts/services/two-factor.service.mjs +77 -0
  66. package/dist/modules/accounts/services/two-factor.service.mjs.map +1 -0
  67. package/dist/modules/auth/services/auth.service.mjs +7 -4
  68. package/dist/modules/auth/services/auth.service.mjs.map +1 -1
  69. package/dist/server.mjs +36 -0
  70. package/dist/server.mjs.map +1 -1
  71. package/dist/utils/browser/clipboard.mjs +36 -0
  72. package/dist/utils/browser/clipboard.mjs.map +1 -0
  73. package/dist/utils/file/index.mjs +9 -0
  74. package/dist/utils/file/index.mjs.map +1 -0
  75. package/dist/utils/format/masks.mjs +56 -0
  76. package/dist/utils/format/masks.mjs.map +1 -0
  77. package/dist/utils/intl/locales.mjs +12 -0
  78. package/dist/utils/intl/locales.mjs.map +1 -0
  79. package/dist/utils/intl/timezones.mjs +41 -0
  80. package/dist/utils/intl/timezones.mjs.map +1 -0
  81. package/dist/utils/validators/account.mjs +26 -0
  82. package/dist/utils/validators/account.mjs.map +1 -0
  83. package/dist/utils/validators/common.mjs +47 -0
  84. package/dist/utils/validators/common.mjs.map +1 -0
  85. package/package.json +5 -1
  86. package/src/components/account/ConfigurationsMyAccountModal.tsx +147 -0
  87. package/src/components/account/ConfirmGlobalPreferencesModal.tsx +66 -0
  88. package/src/components/account/DisableTwoFactorAuthModal.tsx +86 -0
  89. package/src/components/account/TwoFactorAuthModal.tsx +221 -0
  90. package/src/components/account/constants.ts +29 -0
  91. package/src/components/account/hooks/useChangeEmailForm.tsx +56 -0
  92. package/src/components/account/hooks/useChangePasswordForm.tsx +59 -0
  93. package/src/components/account/hooks/useChangePhoneForm.tsx +69 -0
  94. package/src/components/account/hooks/useConfigurationsModal.ts +40 -0
  95. package/src/components/account/hooks/useDisableTwoFactorAuthForm.tsx +69 -0
  96. package/src/components/account/hooks/useOtpVerification.tsx +116 -0
  97. package/src/components/account/hooks/useTwoFactorAuthForm.tsx +114 -0
  98. package/src/components/account/sections/ChangeEmailModal.tsx +224 -0
  99. package/src/components/account/sections/ChangePasswordSection.tsx +64 -0
  100. package/src/components/account/sections/ChangePhoneModal.tsx +229 -0
  101. package/src/components/account/sections/MyProfileSection.tsx +337 -0
  102. package/src/components/account/sections/PreferencesSection.tsx +277 -0
  103. package/src/components/account/sections/SecuritySection.tsx +160 -0
  104. package/src/components/ui/form/ComboboxField.tsx +154 -0
  105. package/src/components/ui/form/FormField.tsx +130 -0
  106. package/src/components/ui/form/PhoneInput.tsx +98 -0
  107. package/src/components/ui/form/SelectField.tsx +118 -0
  108. package/src/components/ui/form/SwitchOptionFieldWithIcon.tsx +47 -0
  109. package/src/components/ui/form/TextAreaField.tsx +70 -0
  110. package/src/enums/AccountSectionType.ts +6 -0
  111. package/src/hooks/useCountdownTimer.ts +41 -0
  112. package/src/hooks/useIsMobile.ts +19 -0
  113. package/src/hooks/usePasswordVisibility.ts +13 -0
  114. package/src/index.ts +70 -0
  115. package/src/modules/accounts/actions/account-management.action.ts +197 -0
  116. package/src/modules/accounts/hooks/useAccountManagement.ts +79 -0
  117. package/src/modules/accounts/services/account.service.ts +229 -50
  118. package/src/modules/accounts/services/two-factor.service.ts +98 -0
  119. package/src/modules/accounts/types.ts +157 -0
  120. package/src/modules/auth/services/auth.service.ts +9 -5
  121. package/src/server.ts +21 -0
  122. package/src/utils/browser/clipboard.ts +34 -0
  123. package/src/utils/file/index.ts +5 -0
  124. package/src/utils/format/masks.ts +52 -0
  125. package/src/utils/intl/locales.ts +15 -0
  126. package/src/utils/intl/timezones.ts +53 -0
  127. package/src/utils/validators/account.ts +36 -0
  128. package/src/utils/validators/common.ts +43 -0
@@ -0,0 +1,114 @@
1
+ 'use client';
2
+
3
+ import { useState, useCallback, useEffect } from 'react';
4
+ import { useMutation } from '@tanstack/react-query';
5
+ import { toast } from 'sonner';
6
+ import { Toast } from '../../ui/feedback/Toast';
7
+ import { copyToClipboard } from '../../../utils/browser/clipboard';
8
+ import {
9
+ generateTwoFactorAction,
10
+ confirmTwoFactorAction,
11
+ } from '../../../modules/accounts/actions/account-management.action';
12
+
13
+ type Step = 'code' | 'recovery';
14
+
15
+ interface UseTwoFactorAuthFormProps {
16
+ onSuccess?: () => void;
17
+ open?: boolean;
18
+ }
19
+
20
+ export default function useTwoFactorAuthForm({ onSuccess, open }: UseTwoFactorAuthFormProps = {}) {
21
+ const [code, setCode] = useState('');
22
+ const [step, setStep] = useState<Step>('code');
23
+
24
+ const generateMutation = useMutation({
25
+ mutationFn: generateTwoFactorAction,
26
+ onSuccess: (result) => {
27
+ if (!result.success) {
28
+ toast.custom((t) => (
29
+ <Toast variant="error" message={result.error ?? 'Erro ao gerar QR Code. Tente novamente.'} toastId={t} />
30
+ ));
31
+ }
32
+ },
33
+ onError: () => {
34
+ toast.custom((t) => (
35
+ <Toast variant="error" message="Erro ao gerar QR Code. Tente novamente." toastId={t} />
36
+ ));
37
+ },
38
+ });
39
+
40
+ const confirmMutation = useMutation({
41
+ mutationFn: (otpCode: string) => confirmTwoFactorAction(otpCode),
42
+ onSuccess: (result) => {
43
+ if (!result.success) {
44
+ toast.custom((t) => (
45
+ <Toast variant="error" message={result.error ?? 'Código inválido'} toastId={t} />
46
+ ));
47
+ return;
48
+ }
49
+ setStep('recovery');
50
+ },
51
+ onError: () => {
52
+ toast.custom((t) => (
53
+ <Toast variant="error" message="Erro ao confirmar código. Tente novamente." toastId={t} />
54
+ ));
55
+ },
56
+ });
57
+
58
+ const { mutate: triggerGenerate } = generateMutation;
59
+
60
+ useEffect(() => {
61
+ if (open) {
62
+ triggerGenerate();
63
+ }
64
+ }, [open, triggerGenerate]);
65
+
66
+ const generateFailed =
67
+ (generateMutation.isSuccess && !generateMutation.data?.success) || generateMutation.isError;
68
+
69
+ const isCodeComplete = code.length === 6;
70
+
71
+ const handleSubmit = () => {
72
+ if (!isCodeComplete) return;
73
+ confirmMutation.mutate(code);
74
+ };
75
+
76
+ const handleFinalize = () => {
77
+ onSuccess?.();
78
+ setTimeout(resetForm, 300);
79
+ };
80
+
81
+ const handleCopyCodes = useCallback(async () => {
82
+ const codes = generateMutation.data?.backupCodes ?? [];
83
+ const success = await copyToClipboard(codes.join('\n'));
84
+ if (success) {
85
+ toast.custom((t) => (
86
+ <Toast variant="success" message="Códigos copiados com sucesso" toastId={t} />
87
+ ));
88
+ }
89
+ }, [generateMutation.data]);
90
+
91
+ const resetForm = () => {
92
+ setCode('');
93
+ setStep('code');
94
+ generateMutation.reset();
95
+ confirmMutation.reset();
96
+ };
97
+
98
+ return {
99
+ code,
100
+ setCode,
101
+ isCodeComplete,
102
+ isGenerating: generateMutation.isPending,
103
+ isConfirming: confirmMutation.isPending,
104
+ generateFailed,
105
+ retryGenerate: triggerGenerate,
106
+ qrcode: generateMutation.data?.qrcode,
107
+ backupCodes: generateMutation.data?.backupCodes ?? [],
108
+ step,
109
+ handleSubmit,
110
+ handleFinalize,
111
+ handleCopyCodes,
112
+ resetForm,
113
+ };
114
+ }
@@ -0,0 +1,224 @@
1
+ 'use client';
2
+
3
+ import { ChevronLeft, X, Mail } from 'lucide-react';
4
+ import { Button } from '../../ui/buttons/Button';
5
+ import { Dialog, DialogContent, DialogHeader, DialogTitle } from '../../ui/overlay/Dialog';
6
+ import { Separator } from '../../ui/data-display/Separator';
7
+ import { InputOTP, InputOTPGroup, InputOTPSlot } from '../../ui/form/InputOtp';
8
+ import { FormField } from '../../ui/form/FormField';
9
+ import { formatTimer } from '../../../utils/format/masks';
10
+ import { OTP_CODE_LENGTH } from '../../../utils/validators/account';
11
+ import { useAuth } from '../../../providers/auth.provider';
12
+ import { useInvalidateUser } from '../../../modules/auth/hooks/useUserQuery';
13
+ import { confirmEmailChangeAction } from '../../../modules/accounts/actions/account-management.action';
14
+ import useChangeEmailForm from '../hooks/useChangeEmailForm';
15
+ import useOtpVerification from '../hooks/useOtpVerification';
16
+
17
+ interface ChangeEmailModalProps {
18
+ open: boolean;
19
+ onClose: () => void;
20
+ }
21
+
22
+ export function ChangeEmailModal({ open, onClose }: ChangeEmailModalProps) {
23
+ const { user } = useAuth();
24
+ const invalidateUser = useInvalidateUser();
25
+
26
+ const {
27
+ showVerification,
28
+ emailToVerify,
29
+ isSubmitting,
30
+ errors,
31
+ register,
32
+ handleContinue,
33
+ handleBack,
34
+ resetForm,
35
+ } = useChangeEmailForm();
36
+
37
+ const handleSuccess = () => {
38
+ invalidateUser();
39
+ resetForm();
40
+ onClose();
41
+ };
42
+
43
+ const {
44
+ code,
45
+ isLoading,
46
+ hasError,
47
+ timer,
48
+ canResend,
49
+ isValidLength,
50
+ handleCodeChange,
51
+ handleValidate,
52
+ handleResend,
53
+ reset: resetOtp,
54
+ } = useOtpVerification({
55
+ onSuccess: handleSuccess,
56
+ successMessage: 'E-mail alterado com sucesso',
57
+ validateFn: async (token) => {
58
+ const result = await confirmEmailChangeAction(token, emailToVerify);
59
+ return result.success;
60
+ },
61
+ resendFn: async () => {
62
+ const { requestEmailChangeAction } = await import('../../../modules/accounts/actions/account-management.action');
63
+ const result = await requestEmailChangeAction(emailToVerify);
64
+ if (!result.success) {
65
+ throw new Error(result.error);
66
+ }
67
+ },
68
+ });
69
+
70
+ const handleClose = () => {
71
+ resetForm();
72
+ resetOtp();
73
+ onClose();
74
+ };
75
+
76
+ const handleBackFromVerification = () => {
77
+ resetOtp();
78
+ handleBack();
79
+ };
80
+
81
+ return (
82
+ <Dialog open={open} onOpenChange={handleClose}>
83
+ <DialogContent
84
+ showCloseButton={false}
85
+ className="flex flex-col p-0 gap-0 max-w-full! border-0 rounded-t-2xl rounded-b-none h-dvh top-0! bottom-0! left-0! right-0! translate-x-0! translate-y-0! lg:max-w-125! lg:h-auto! lg:rounded-lg lg:border lg:top-[50%]! lg:left-[50%]! lg:right-auto! lg:bottom-auto! lg:translate-x-[-50%]! lg:translate-y-[-50%]!"
86
+ >
87
+ {showVerification ? (
88
+ <DialogHeader className="flex flex-row items-center justify-between px-4 py-3 border-b border-gray-200 lg:border-b-0">
89
+ <Button variant="ghost" className="size-8! p-0" onClick={handleBackFromVerification}>
90
+ <ChevronLeft size={20} className="text-gray-950" />
91
+ </Button>
92
+ <DialogTitle className="paragraph-medium-semibold text-gray-950 text-center flex-1">
93
+ Alterar e-mail
94
+ </DialogTitle>
95
+ <Button variant="ghost" className="size-8! p-0" onClick={handleClose}>
96
+ <X size={18} className="text-gray-500" />
97
+ </Button>
98
+ </DialogHeader>
99
+ ) : (
100
+ <>
101
+ <DialogHeader className="sr-only">
102
+ <DialogTitle>Alterar e-mail</DialogTitle>
103
+ </DialogHeader>
104
+ <div className="flex items-center justify-center px-4 py-3 relative border-b border-gray-200 lg:border-b-0">
105
+ <span className="paragraph-medium-semibold text-gray-950 text-center">
106
+ Alterar e-mail
107
+ </span>
108
+ <Button
109
+ variant="ghost"
110
+ className="absolute right-2 size-8! p-0"
111
+ onClick={handleClose}
112
+ >
113
+ <X size={18} className="text-gray-500" />
114
+ </Button>
115
+ </div>
116
+ </>
117
+ )}
118
+
119
+ <div className={`${showVerification ? 'w-full' : 'w-1/2'} h-0.75 bg-pages-300`} />
120
+
121
+ {showVerification ? (
122
+ <form
123
+ onSubmit={(e) => {
124
+ e.preventDefault();
125
+ handleValidate();
126
+ }}
127
+ className="flex flex-col flex-1 lg:flex-none"
128
+ >
129
+ <div className="px-4 lg:px-5 py-5 lg:py-6 flex flex-col gap-4 flex-1 lg:flex-none">
130
+ <div className="flex items-center justify-center size-12 bg-white rounded-lg shadow-xs border border-gray-200">
131
+ <Mail size={20} className="text-gray-950" />
132
+ </div>
133
+
134
+ <div className="flex flex-col gap-1">
135
+ <h3 className="paragraph-medium-semibold text-gray-950">Código de validação</h3>
136
+ <p className="paragraph-small-regular text-gray-600">
137
+ Enviamos um código de validação para o E-mail{' '}
138
+ <span className="font-semibold text-gray-950 break-all">{user?.email}.</span>
139
+ </p>
140
+ </div>
141
+
142
+ <div className="flex flex-col gap-2">
143
+ <InputOTP maxLength={OTP_CODE_LENGTH} value={code} onChange={handleCodeChange}>
144
+ <InputOTPGroup className="flex flex-row gap-2">
145
+ {[...Array(OTP_CODE_LENGTH)].map((_, i) => (
146
+ <InputOTPSlot
147
+ key={i}
148
+ index={i}
149
+ className={`uppercase text-2xl font-medium w-11 lg:w-12 h-13 lg:h-14 rounded-lg border bg-white shadow-none [&_div.animate-caret-blink]:bg-gray-950 ${hasError ? 'border-red-500' : 'border-gray-200'}`}
150
+ />
151
+ ))}
152
+ </InputOTPGroup>
153
+ </InputOTP>
154
+ {hasError && (
155
+ <p className="paragraph-small-regular text-red-500">Código incorreto</p>
156
+ )}
157
+ </div>
158
+
159
+ {canResend ? (
160
+ <button
161
+ type="button"
162
+ onClick={handleResend}
163
+ className="paragraph-small-medium text-gray-950 underline cursor-pointer hover:text-gray-700 w-fit"
164
+ >
165
+ Reenviar código
166
+ </button>
167
+ ) : (
168
+ <p className="paragraph-small-regular text-gray-500">
169
+ Reenviar em{' '}
170
+ <span className="paragraph-small-semibold text-gray-950">
171
+ {formatTimer(timer)}
172
+ </span>
173
+ </p>
174
+ )}
175
+ </div>
176
+
177
+ <Separator />
178
+
179
+ <div className="flex items-center justify-between px-4 lg:px-5 py-4 lg:py-5 mt-auto lg:mt-0">
180
+ <Button variant="secondary" className="h-10!" type="button" onClick={handleClose}>
181
+ Cancelar
182
+ </Button>
183
+ <Button className="h-10!" type="submit" disabled={!isValidLength || isLoading}>
184
+ {isLoading ? 'Validando...' : 'Alterar e-mail'}
185
+ </Button>
186
+ </div>
187
+ </form>
188
+ ) : (
189
+ <form onSubmit={handleContinue} className="flex flex-col flex-1 lg:flex-none">
190
+ <div className="flex flex-col gap-5 lg:gap-6 px-4 lg:px-5 py-5 lg:py-6 flex-1 lg:flex-none">
191
+ <div className="flex flex-col gap-1">
192
+ <span className="paragraph-medium-semibold text-gray-950">
193
+ Atualizar E-mail de contato
194
+ </span>
195
+ <span className="paragraph-small-regular text-gray-600">
196
+ Informe o seu novo E-mail para manter seu contato atualizado.
197
+ </span>
198
+ </div>
199
+
200
+ <FormField
201
+ label="Novo e-mail"
202
+ placeholder="Digite seu novo e-mail"
203
+ type="email"
204
+ {...register('email')}
205
+ error={!!errors.email}
206
+ errorMessage={errors.email?.message}
207
+ leftIcon={Mail}
208
+ />
209
+ </div>
210
+
211
+ <div className="flex items-center justify-between p-4 lg:p-5 border-t border-gray-200 mt-auto lg:mt-0">
212
+ <Button variant="secondary" className="h-10!" type="button" onClick={handleClose}>
213
+ Cancelar
214
+ </Button>
215
+ <Button className="h-10!" type="submit" disabled={isSubmitting}>
216
+ {isSubmitting ? 'Enviando...' : 'Continuar'}
217
+ </Button>
218
+ </div>
219
+ </form>
220
+ )}
221
+ </DialogContent>
222
+ </Dialog>
223
+ );
224
+ }
@@ -0,0 +1,64 @@
1
+ 'use client';
2
+
3
+ import { ArrowLeft, Lock } from 'lucide-react';
4
+ import { Button } from '../../ui/buttons/Button';
5
+ import { FormField } from '../../ui/form/FormField';
6
+ import useChangePasswordForm from '../hooks/useChangePasswordForm';
7
+ import usePasswordVisibility from '../../../hooks/usePasswordVisibility';
8
+
9
+ interface ChangePasswordSectionProps {
10
+ onBack: () => void;
11
+ }
12
+
13
+ export function ChangePasswordSection({ onBack }: ChangePasswordSectionProps) {
14
+ const { register, handleSubmit, errors } = useChangePasswordForm({
15
+ onSuccess: onBack,
16
+ });
17
+ const { showPassword: showCurrentPw, togglePassword: toggleCurrentPw } = usePasswordVisibility();
18
+ const { showPassword: showNewPw, togglePassword: toggleNewPw } = usePasswordVisibility();
19
+
20
+ return (
21
+ <form
22
+ onSubmit={handleSubmit}
23
+ className="flex-1 overflow-y-auto px-4 pb-5 lg:p-5 flex flex-col h-full lg:h-full gap-5 lg:gap-6"
24
+ >
25
+ <div className="flex items-center gap-3">
26
+ <Button type="button" variant="ghost" className="size-8! p-0" onClick={onBack}>
27
+ <ArrowLeft size={20} />
28
+ </Button>
29
+ <span className="paragraph-medium-semibold text-gray-950">Alterar senha</span>
30
+ </div>
31
+
32
+ <div className="flex flex-col gap-4">
33
+ <FormField
34
+ label="Senha atual"
35
+ type="password"
36
+ placeholder="Digite sua senha atual"
37
+ leftIcon={Lock}
38
+ error={!!errors.currentPassword}
39
+ errorMessage={errors.currentPassword?.message}
40
+ classnameContainer="h-10!"
41
+ showPassword={showCurrentPw}
42
+ onTogglePassword={toggleCurrentPw}
43
+ {...register('currentPassword')}
44
+ />
45
+ <FormField
46
+ label="Nova senha"
47
+ type="password"
48
+ placeholder="Digite sua nova senha"
49
+ leftIcon={Lock}
50
+ error={!!errors.newPassword}
51
+ errorMessage={errors.newPassword?.message}
52
+ classnameContainer="h-10!"
53
+ showPassword={showNewPw}
54
+ onTogglePassword={toggleNewPw}
55
+ {...register('newPassword')}
56
+ />
57
+ </div>
58
+
59
+ <Button type="submit" className="w-fit h-10!">
60
+ Salvar nova senha
61
+ </Button>
62
+ </form>
63
+ );
64
+ }
@@ -0,0 +1,229 @@
1
+ 'use client';
2
+
3
+ import { ChevronLeft, X, MessageSquare } from 'lucide-react';
4
+ import { Button } from '../../ui/buttons/Button';
5
+ import { Dialog, DialogContent, DialogHeader, DialogTitle } from '../../ui/overlay/Dialog';
6
+ import { Separator } from '../../ui/data-display/Separator';
7
+ import { InputOTP, InputOTPGroup, InputOTPSlot } from '../../ui/form/InputOtp';
8
+ import PhoneInput from '../../ui/form/PhoneInput';
9
+ import { formatTimer, formatPhone } from '../../../utils/format/masks';
10
+ import { OTP_CODE_LENGTH } from '../../../utils/validators/account';
11
+ import { useAuth } from '../../../providers/auth.provider';
12
+ import { useInvalidateUser } from '../../../modules/auth/hooks/useUserQuery';
13
+ import { confirmPhoneChangeAction } from '../../../modules/accounts/actions/account-management.action';
14
+ import useChangePhoneForm from '../hooks/useChangePhoneForm';
15
+ import useOtpVerification from '../hooks/useOtpVerification';
16
+
17
+ interface ChangePhoneModalProps {
18
+ open: boolean;
19
+ onClose: () => void;
20
+ }
21
+
22
+ export function ChangePhoneModal({ open, onClose }: ChangePhoneModalProps) {
23
+ const { user } = useAuth();
24
+ const invalidateUser = useInvalidateUser();
25
+
26
+ const currentPhoneDisplay = user?.phone
27
+ ? formatPhone(
28
+ user.ddi && user.phone.startsWith(user.ddi) ? user.phone.slice(user.ddi.length) : user.phone
29
+ )
30
+ : '';
31
+
32
+ const {
33
+ showVerification,
34
+ phoneValue,
35
+ isSubmitting,
36
+ errors,
37
+ handlePhoneChange,
38
+ handleContinue,
39
+ handleBack,
40
+ resetForm,
41
+ } = useChangePhoneForm();
42
+
43
+ const handleSuccess = () => {
44
+ invalidateUser();
45
+ resetForm();
46
+ onClose();
47
+ };
48
+
49
+ const {
50
+ code,
51
+ isLoading,
52
+ hasError,
53
+ timer,
54
+ canResend,
55
+ isValidLength,
56
+ handleCodeChange,
57
+ handleValidate,
58
+ handleResend,
59
+ reset: resetOtp,
60
+ } = useOtpVerification({
61
+ onSuccess: handleSuccess,
62
+ successMessage: 'Telefone alterado',
63
+ validateFn: async (token) => {
64
+ const result = await confirmPhoneChangeAction(token, phoneValue);
65
+ return result.success;
66
+ },
67
+ resendFn: async () => {
68
+ const { requestPhoneChangeAction } = await import('../../../modules/accounts/actions/account-management.action');
69
+ const result = await requestPhoneChangeAction(phoneValue);
70
+ if (!result.success) {
71
+ throw new Error(result.error);
72
+ }
73
+ },
74
+ });
75
+
76
+ const handleClose = () => {
77
+ resetForm();
78
+ resetOtp();
79
+ onClose();
80
+ };
81
+
82
+ const handleBackFromVerification = () => {
83
+ resetOtp();
84
+ handleBack();
85
+ };
86
+
87
+ return (
88
+ <Dialog open={open} onOpenChange={handleClose}>
89
+ <DialogContent
90
+ showCloseButton={false}
91
+ className="flex flex-col p-0 gap-0 max-w-full! border-0 rounded-t-2xl rounded-b-none h-dvh top-0! bottom-0! left-0! right-0! translate-x-0! translate-y-0! lg:max-w-125! lg:h-auto! lg:rounded-lg lg:border lg:top-[50%]! lg:left-[50%]! lg:right-auto! lg:bottom-auto! lg:translate-x-[-50%]! lg:translate-y-[-50%]!"
92
+ >
93
+ {showVerification ? (
94
+ <DialogHeader className="flex flex-row items-center justify-between px-4 py-3 border-b border-gray-200 lg:border-b-0">
95
+ <Button variant="ghost" className="size-8! p-0" onClick={handleBackFromVerification}>
96
+ <ChevronLeft size={20} className="text-gray-950" />
97
+ </Button>
98
+ <DialogTitle className="paragraph-medium-semibold text-gray-950 text-center flex-1">
99
+ Alterar telefone
100
+ </DialogTitle>
101
+ <Button variant="ghost" className="size-8! p-0" onClick={handleClose}>
102
+ <X size={18} className="text-gray-500" />
103
+ </Button>
104
+ </DialogHeader>
105
+ ) : (
106
+ <>
107
+ <DialogHeader className="sr-only">
108
+ <DialogTitle>Alterar telefone</DialogTitle>
109
+ </DialogHeader>
110
+ <div className="flex items-center justify-center px-4 py-3 relative border-b border-gray-200 lg:border-b-0">
111
+ <span className="paragraph-medium-semibold text-gray-950 text-center">
112
+ Alterar telefone
113
+ </span>
114
+ <Button
115
+ variant="ghost"
116
+ className="absolute right-2 size-8! p-0"
117
+ onClick={handleClose}
118
+ >
119
+ <X size={18} className="text-gray-500" />
120
+ </Button>
121
+ </div>
122
+ </>
123
+ )}
124
+
125
+ <div className={`${showVerification ? 'w-full' : 'w-1/2'} h-0.75 bg-pages-300`} />
126
+
127
+ {showVerification ? (
128
+ <form
129
+ onSubmit={(e) => {
130
+ e.preventDefault();
131
+ handleValidate();
132
+ }}
133
+ className="flex flex-col flex-1 lg:flex-none"
134
+ >
135
+ <div className="px-4 lg:px-5 py-5 lg:py-6 flex flex-col gap-4 flex-1 lg:flex-none">
136
+ <div className="flex items-center justify-center size-12 bg-white rounded-lg shadow-xs border border-gray-200">
137
+ <MessageSquare size={20} className="text-gray-950" />
138
+ </div>
139
+
140
+ <div className="flex flex-col gap-1">
141
+ <h3 className="paragraph-medium-semibold text-gray-950">Código de validação</h3>
142
+ <p className="paragraph-small-regular text-gray-600">
143
+ Enviamos um código de validação para o número atual{' '}
144
+ <span className="font-semibold text-gray-950">{currentPhoneDisplay}.</span>
145
+ </p>
146
+ </div>
147
+
148
+ <div className="flex flex-col gap-2">
149
+ <InputOTP maxLength={OTP_CODE_LENGTH} value={code} onChange={handleCodeChange}>
150
+ <InputOTPGroup className="flex flex-row gap-2">
151
+ {[...Array(OTP_CODE_LENGTH)].map((_, i) => (
152
+ <InputOTPSlot
153
+ key={i}
154
+ index={i}
155
+ className={`uppercase text-2xl font-medium w-11 lg:w-12 h-13 lg:h-14 rounded-lg border bg-white shadow-none [&_div.animate-caret-blink]:bg-gray-950 ${hasError ? 'border-red-500' : 'border-gray-200'}`}
156
+ />
157
+ ))}
158
+ </InputOTPGroup>
159
+ </InputOTP>
160
+ {hasError && (
161
+ <p className="paragraph-small-regular text-red-500">Código incorreto</p>
162
+ )}
163
+ </div>
164
+
165
+ {canResend ? (
166
+ <button
167
+ type="button"
168
+ onClick={handleResend}
169
+ className="paragraph-small-medium text-gray-950 underline cursor-pointer hover:text-gray-700 w-fit"
170
+ >
171
+ Reenviar código
172
+ </button>
173
+ ) : (
174
+ <p className="paragraph-small-regular text-gray-500">
175
+ Reenviar em{' '}
176
+ <span className="paragraph-small-semibold text-gray-950">
177
+ {formatTimer(timer)}
178
+ </span>
179
+ </p>
180
+ )}
181
+ </div>
182
+
183
+ <Separator />
184
+
185
+ <div className="flex items-center justify-between px-4 lg:px-5 py-4 lg:py-5 mt-auto lg:mt-0">
186
+ <Button variant="secondary" className="h-10!" type="button" onClick={handleClose}>
187
+ Cancelar
188
+ </Button>
189
+ <Button className="h-10!" type="submit" disabled={!isValidLength || isLoading}>
190
+ {isLoading ? 'Validando...' : 'Alterar telefone'}
191
+ </Button>
192
+ </div>
193
+ </form>
194
+ ) : (
195
+ <form onSubmit={handleContinue} className="flex flex-col flex-1 lg:flex-none">
196
+ <div className="flex flex-col gap-5 lg:gap-6 px-4 lg:px-5 py-5 lg:py-6 flex-1 lg:flex-none">
197
+ <div className="flex flex-col gap-1">
198
+ <span className="paragraph-medium-semibold text-gray-950">
199
+ Atualizar telefone de contato
200
+ </span>
201
+ <span className="paragraph-small-regular text-gray-600">
202
+ Informe o novo número para manter seu contato atualizado.
203
+ </span>
204
+ </div>
205
+
206
+ <PhoneInput
207
+ label="Novo número"
208
+ placeholder="(00) 0 0000-0000"
209
+ value={phoneValue}
210
+ onChange={handlePhoneChange}
211
+ error={!!errors.phone}
212
+ errorMessage={errors.phone?.message}
213
+ />
214
+ </div>
215
+
216
+ <div className="flex items-center justify-between p-4 lg:p-5 border-t border-gray-200 mt-auto lg:mt-0">
217
+ <Button variant="secondary" className="h-10!" type="button" onClick={handleClose}>
218
+ Cancelar
219
+ </Button>
220
+ <Button className="h-10!" type="submit" disabled={isSubmitting}>
221
+ {isSubmitting ? 'Enviando...' : 'Continuar'}
222
+ </Button>
223
+ </div>
224
+ </form>
225
+ )}
226
+ </DialogContent>
227
+ </Dialog>
228
+ );
229
+ }