|
| 1 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 2 | +import { useNavigation } from 'expo-router'; |
| 3 | +import React from 'react'; |
| 4 | +import { useForm } from 'react-hook-form'; |
| 5 | +import { useTranslation } from 'react-i18next'; |
| 6 | +import { showMessage } from 'react-native-flash-message'; |
| 7 | +import { KeyboardAvoidingView } from 'react-native-keyboard-controller'; |
| 8 | +import { z } from 'zod'; |
| 9 | + |
| 10 | +import { useUpdatePassword } from '@/api/auth/use-update-password'; |
| 11 | +import { translate } from '@/core'; |
| 12 | +import { Button, ControlledInput, FocusAwareStatusBar, Text, View } from '@/ui'; |
| 13 | + |
| 14 | +type FormValues = { password: string; passwordConfirmation: string }; |
| 15 | +const MIN_CHARS = 6; |
| 16 | + |
| 17 | +const schema = z |
| 18 | + .object({ |
| 19 | + password: z.string().min( |
| 20 | + MIN_CHARS, |
| 21 | + translate('updatePassword.error.shortPassword', { |
| 22 | + minChars: MIN_CHARS, |
| 23 | + }), |
| 24 | + ), |
| 25 | + passwordConfirmation: z.string(), |
| 26 | + }) |
| 27 | + .refine((data) => data.password === data.passwordConfirmation, { |
| 28 | + message: translate('updatePassword.error.passwordsMustMatch'), |
| 29 | + path: ['passwordConfirmation'], |
| 30 | + }); |
| 31 | + |
| 32 | +export default function UpdatePassword() { |
| 33 | + const { t } = useTranslation(); |
| 34 | + const navigate = useNavigation(); |
| 35 | + |
| 36 | + const { mutateAsync: updatePassword } = useUpdatePassword({ |
| 37 | + onSuccess: () => { |
| 38 | + showMessage({ |
| 39 | + message: t('updatePassword.successMessage'), |
| 40 | + type: 'success', |
| 41 | + }); |
| 42 | + navigate.goBack(); |
| 43 | + }, |
| 44 | + onError: () => { |
| 45 | + showMessage({ |
| 46 | + message: t('updatePassword.errorMessage'), |
| 47 | + type: 'danger', |
| 48 | + }); |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + const { handleSubmit, control } = useForm<FormValues>({ |
| 53 | + resolver: zodResolver(schema), |
| 54 | + }); |
| 55 | + const onSubmit = async (data: FormValues) => { |
| 56 | + await updatePassword(data); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <> |
| 61 | + <FocusAwareStatusBar /> |
| 62 | + <KeyboardAvoidingView> |
| 63 | + <View className="gap-8 p-4"> |
| 64 | + <View className="gap-2"> |
| 65 | + <Text className="text-center text-2xl"> |
| 66 | + {t('updatePassword.title')} |
| 67 | + </Text> |
| 68 | + <Text className="text-center text-gray-600"> |
| 69 | + {t('updatePassword.description')} |
| 70 | + </Text> |
| 71 | + </View> |
| 72 | + <View className="gap-2"> |
| 73 | + <ControlledInput |
| 74 | + testID="password-input" |
| 75 | + autoCapitalize="none" |
| 76 | + secureTextEntry |
| 77 | + control={control} |
| 78 | + name="password" |
| 79 | + label={t('updatePassword.passwordLabel')} |
| 80 | + placeholder={t('updatePassword.passwordPlaceholder')} |
| 81 | + /> |
| 82 | + <ControlledInput |
| 83 | + testID="confirm-password-input" |
| 84 | + autoCapitalize="none" |
| 85 | + secureTextEntry |
| 86 | + control={control} |
| 87 | + name="passwordConfirmation" |
| 88 | + label={t('updatePassword.confirmPasswordLabel')} |
| 89 | + placeholder={t('updatePassword.confirmPasswordPlaceholder')} |
| 90 | + /> |
| 91 | + <Button |
| 92 | + testID="update-password-button" |
| 93 | + label={t('updatePassword.buttonLabel')} |
| 94 | + onPress={handleSubmit(onSubmit)} |
| 95 | + /> |
| 96 | + </View> |
| 97 | + </View> |
| 98 | + </KeyboardAvoidingView> |
| 99 | + </> |
| 100 | + ); |
| 101 | +} |
0 commit comments