@greatapps/common 1.1.170 → 1.1.174

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.
@@ -1,229 +1,234 @@
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
- }
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
+ selectedCountry,
38
+ handlePhoneChange,
39
+ handleCountryChange,
40
+ handleContinue,
41
+ handleBack,
42
+ resetForm,
43
+ } = useChangePhoneForm();
44
+
45
+ const handleSuccess = () => {
46
+ invalidateUser();
47
+ resetForm();
48
+ onClose();
49
+ };
50
+
51
+ const {
52
+ code,
53
+ isLoading,
54
+ hasError,
55
+ timer,
56
+ canResend,
57
+ isValidLength,
58
+ handleCodeChange,
59
+ handleValidate,
60
+ handleResend,
61
+ reset: resetOtp,
62
+ } = useOtpVerification({
63
+ onSuccess: handleSuccess,
64
+ successMessage: 'Telefone alterado',
65
+ validateFn: async (token) => {
66
+ const result = await confirmPhoneChangeAction(token, phoneValue, selectedCountry.code);
67
+ return result.success;
68
+ },
69
+ resendFn: async () => {
70
+ const { requestPhoneChangeAction } = await import('../../../modules/accounts/actions/account-management.action');
71
+ const result = await requestPhoneChangeAction(phoneValue, selectedCountry.code);
72
+ if (!result.success) {
73
+ throw new Error(result.error);
74
+ }
75
+ },
76
+ });
77
+
78
+ const handleClose = () => {
79
+ resetForm();
80
+ resetOtp();
81
+ onClose();
82
+ };
83
+
84
+ const handleBackFromVerification = () => {
85
+ resetOtp();
86
+ handleBack();
87
+ };
88
+
89
+ return (
90
+ <Dialog open={open} onOpenChange={handleClose}>
91
+ <DialogContent
92
+ showCloseButton={false}
93
+ overlayClassName="z-[1002]"
94
+ 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%]! z-[1003]!"
95
+ >
96
+ {showVerification ? (
97
+ <DialogHeader className="flex flex-row items-center justify-between px-4 py-3 border-b border-gray-200 lg:border-b-0">
98
+ <Button variant="ghost" className="size-8! p-0" onClick={handleBackFromVerification}>
99
+ <ChevronLeft size={20} className="text-gray-950" />
100
+ </Button>
101
+ <DialogTitle className="paragraph-medium-semibold text-gray-950 text-center flex-1">
102
+ Alterar telefone
103
+ </DialogTitle>
104
+ <Button variant="ghost" className="size-8! p-0" onClick={handleClose}>
105
+ <X size={18} className="text-gray-500" />
106
+ </Button>
107
+ </DialogHeader>
108
+ ) : (
109
+ <>
110
+ <DialogHeader className="sr-only">
111
+ <DialogTitle>Alterar telefone</DialogTitle>
112
+ </DialogHeader>
113
+ <div className="flex items-center justify-center px-4 py-3 relative border-b border-gray-200 lg:border-b-0">
114
+ <span className="paragraph-medium-semibold text-gray-950 text-center">
115
+ Alterar telefone
116
+ </span>
117
+ <Button
118
+ variant="ghost"
119
+ className="absolute right-2 size-8! p-0"
120
+ onClick={handleClose}
121
+ >
122
+ <X size={18} className="text-gray-500" />
123
+ </Button>
124
+ </div>
125
+ </>
126
+ )}
127
+
128
+ <div className={`${showVerification ? 'w-full' : 'w-1/2'} h-0.75 bg-pages-300`} />
129
+
130
+ {showVerification ? (
131
+ <form
132
+ onSubmit={(e) => {
133
+ e.preventDefault();
134
+ handleValidate();
135
+ }}
136
+ className="flex flex-col flex-1 lg:flex-none"
137
+ >
138
+ <div className="px-4 lg:px-5 py-5 lg:py-6 flex flex-col gap-4 flex-1 lg:flex-none">
139
+ <div className="flex items-center justify-center size-12 bg-white rounded-lg shadow-xs border border-gray-200">
140
+ <MessageSquare size={20} className="text-gray-950" />
141
+ </div>
142
+
143
+ <div className="flex flex-col gap-1">
144
+ <h3 className="paragraph-medium-semibold text-gray-950">Código de validação</h3>
145
+ <p className="paragraph-small-regular text-gray-600">
146
+ Enviamos um código de validação para o número atual{' '}
147
+ <span className="font-semibold text-gray-950">{currentPhoneDisplay}.</span>
148
+ </p>
149
+ </div>
150
+
151
+ <div className="flex flex-col gap-2">
152
+ <InputOTP maxLength={OTP_CODE_LENGTH} value={code} onChange={handleCodeChange}>
153
+ <InputOTPGroup className="flex flex-row gap-2">
154
+ {[...Array(OTP_CODE_LENGTH)].map((_, i) => (
155
+ <InputOTPSlot
156
+ key={i}
157
+ index={i}
158
+ 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'}`}
159
+ />
160
+ ))}
161
+ </InputOTPGroup>
162
+ </InputOTP>
163
+ {hasError && (
164
+ <p className="paragraph-small-regular text-red-500">Código incorreto</p>
165
+ )}
166
+ </div>
167
+
168
+ {canResend ? (
169
+ <button
170
+ type="button"
171
+ onClick={handleResend}
172
+ className="paragraph-small-medium text-gray-950 underline cursor-pointer hover:text-gray-700 w-fit"
173
+ >
174
+ Reenviar código
175
+ </button>
176
+ ) : (
177
+ <p className="paragraph-small-regular text-gray-500">
178
+ Reenviar em{' '}
179
+ <span className="paragraph-small-semibold text-gray-950">
180
+ {formatTimer(timer)}
181
+ </span>
182
+ </p>
183
+ )}
184
+ </div>
185
+
186
+ <Separator />
187
+
188
+ <div className="flex items-center justify-between px-4 lg:px-5 py-4 lg:py-5 mt-auto lg:mt-0">
189
+ <Button variant="secondary" className="h-10!" type="button" onClick={handleClose}>
190
+ Cancelar
191
+ </Button>
192
+ <Button className="h-10!" type="submit" disabled={!isValidLength || isLoading}>
193
+ {isLoading ? 'Validando...' : 'Alterar telefone'}
194
+ </Button>
195
+ </div>
196
+ </form>
197
+ ) : (
198
+ <form onSubmit={handleContinue} className="flex flex-col flex-1 lg:flex-none">
199
+ <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">
200
+ <div className="flex flex-col gap-1">
201
+ <span className="paragraph-medium-semibold text-gray-950">
202
+ Atualizar telefone de contato
203
+ </span>
204
+ <span className="paragraph-small-regular text-gray-600">
205
+ Informe o novo número para manter seu contato atualizado.
206
+ </span>
207
+ </div>
208
+
209
+ <PhoneInput
210
+ label="Novo número"
211
+ placeholder="(00) 0 0000-0000"
212
+ value={phoneValue}
213
+ onChange={handlePhoneChange}
214
+ error={!!errors.phone}
215
+ errorMessage={errors.phone?.message}
216
+ selectedCountry={selectedCountry}
217
+ onCountryChange={handleCountryChange}
218
+ />
219
+ </div>
220
+
221
+ <div className="flex items-center justify-between p-4 lg:p-5 border-t border-gray-200 mt-auto lg:mt-0">
222
+ <Button variant="secondary" className="h-10!" type="button" onClick={handleClose}>
223
+ Cancelar
224
+ </Button>
225
+ <Button className="h-10!" type="submit" disabled={isSubmitting}>
226
+ {isSubmitting ? 'Enviando...' : 'Continuar'}
227
+ </Button>
228
+ </div>
229
+ </form>
230
+ )}
231
+ </DialogContent>
232
+ </Dialog>
233
+ );
234
+ }