Skip to content

Commit 1306d44

Browse files
Merge pull request #62 from binarapps/feat/password-form
Feat/ProfilePasswordForm added
2 parents 79505b3 + 5411034 commit 1306d44

6 files changed

Lines changed: 131 additions & 17 deletions

File tree

src/components/screens/profile/ProfileDeleteAccountButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const ProfileDeleteAccountButton = () => {
5858

5959
return (
6060
<Box>
61-
<Box borderColor="border.secondary" borderTopWidth={1} my={4} py={6} alignItems="flex-start">
61+
<Box borderColor="border.secondary" borderTopWidth={1} py={6} alignItems="flex-start">
6262
<Button
6363
leftIconName="delete-bin-line"
6464
variant="SecondaryDestructive"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Box, Button, Row } from '@baca/design-system'
2+
import { useUpdatePasswordForm } from '@baca/hooks/forms/useUpdatePasswordForm'
3+
import React from 'react'
4+
import { useTranslation } from 'react-i18next'
5+
6+
import { ProfileControlledInput } from './ProfileControlledInput'
7+
8+
export const ProfilePasswordForm = () => {
9+
const { t } = useTranslation()
10+
const { control, errors, submit, isSubmitting } = useUpdatePasswordForm()
11+
12+
return (
13+
<Box>
14+
<Box borderColor="border.secondary" borderTopWidth={1} py={6}>
15+
<ProfileControlledInput
16+
label={t('form.labels.old_password')}
17+
name="oldPassword"
18+
placeholder={t('form.placeholders.old_password')}
19+
control={control}
20+
errors={errors}
21+
/>
22+
<ProfileControlledInput
23+
label={t('form.labels.new_password')}
24+
name="password"
25+
placeholder={t('form.placeholders.new_password')}
26+
control={control}
27+
errors={errors}
28+
/>
29+
<Row maxW={800} justifyContent="flex-end">
30+
<Button
31+
disabled={isSubmitting}
32+
loading={isSubmitting}
33+
onPress={submit}
34+
testID="changePasswordButton"
35+
>
36+
{t('common.change')}
37+
</Button>
38+
</Row>
39+
</Box>
40+
</Box>
41+
)
42+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { useAuthControllerUpdate } from '@baca/api/query/auth/auth'
2+
import { AuthUpdateDto } from '@baca/api/types'
3+
import { handleFormError, hapticImpact, showSuccessToast } from '@baca/utils'
4+
import { useMemo } from 'react'
5+
import { useForm } from 'react-hook-form'
6+
import { useTranslation } from 'react-i18next'
7+
8+
export const useUpdatePasswordForm = () => {
9+
const { t } = useTranslation()
10+
const { mutate: updatePasswordMutation, isLoading } = useAuthControllerUpdate()
11+
12+
const defaultValues: AuthUpdateDto = useMemo(
13+
() => ({
14+
oldPassword: '',
15+
password: '',
16+
}),
17+
[]
18+
)
19+
20+
const {
21+
control,
22+
formState: { errors },
23+
handleSubmit,
24+
reset,
25+
setError: setFormError,
26+
} = useForm<AuthUpdateDto>({
27+
mode: 'onTouched',
28+
defaultValues,
29+
})
30+
31+
const onSubmit = (data: AuthUpdateDto) => {
32+
updatePasswordMutation(
33+
{ data },
34+
{
35+
onSuccess: () => {
36+
showSuccessToast({ description: t('toast.success.profile_updated') })
37+
reset(defaultValues)
38+
},
39+
onError: (e) => {
40+
handleFormError<keyof AuthUpdateDto>(
41+
e as unknown as keyof AuthUpdateDto,
42+
({ field, description }) => {
43+
setFormError(field, { message: description })
44+
}
45+
)
46+
hapticImpact()
47+
},
48+
}
49+
)
50+
}
51+
52+
return {
53+
control,
54+
errors,
55+
isSubmitting: isLoading,
56+
submit: handleSubmit(onSubmit),
57+
}
58+
}

src/i18n/translations/en.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"go_back": "Go back",
2828
"remove": "Remove",
2929
"save": "Save",
30+
"change": "Change",
3031
"search": "Search",
3132
"try_again_later": "Please try again later",
3233
"try_again": "Try again"
@@ -56,15 +57,19 @@
5657
"email": "E-mail address",
5758
"first_name": "First name",
5859
"last_name": "Last name",
59-
"password": "Password"
60+
"password": "Password",
61+
"old_password": "Old password",
62+
"new_password": "New password"
6063
},
6164
"placeholders": {
6265
"confirm_password": "Confirm password",
6366
"create_password": "Create a password",
6467
"email": "Enter your email",
6568
"first_name": "Enter your first name",
6669
"last_name": "Enter your last name",
67-
"password": "Password"
70+
"password": "Password",
71+
"old_password": "Enter current password",
72+
"new_password": "Enter new password"
6873
},
6974
"select": {},
7075
"validation": {

src/i18n/translations/pl.json

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"go_back": "Cofnij",
2828
"remove": "Usuń",
2929
"save": "Zapisz",
30+
"change": "Zmień",
3031
"search": "Szukaj",
3132
"try_again_later": "Proszę spróbuj ponownie później",
3233
"try_again": "Spróbuj ponownie"
@@ -56,15 +57,19 @@
5657
"email": "Adres e-mail",
5758
"first_name": "Imię",
5859
"last_name": "Nazwisko",
59-
"password": "Hasło"
60+
"password": "Hasło",
61+
"old_password": "Stare hasło",
62+
"new_password": "Nowe hasło"
6063
},
6164
"placeholders": {
6265
"confirm_password": "Potwierdź hasło",
6366
"create_password": "Utwórz nowe hasło",
6467
"email": "Podaj adres e-mail",
6568
"first_name": "Podaj swoje imię",
6669
"last_name": "Podaj swoje nazwisko",
67-
"password": "Hasło"
70+
"password": "Hasło",
71+
"old_password": "Podaj stare hasło",
72+
"new_password": "Podaj nowe hasło"
6873
},
6974
"select": {},
7075
"validation": {
@@ -126,18 +131,6 @@
126131
"colors_label": "Kolory"
127132
},
128133
"components_screen": {
129-
"test_notification": "Test notification",
130-
"typography": {
131-
"label": "Typography",
132-
"xs": "xs",
133-
"sm": "sm",
134-
"md": "md",
135-
"lg": "lg",
136-
"xl": "xl",
137-
"2xl": "2xl",
138-
"3xl": "3xl",
139-
"4xl": "4xl"
140-
},
141134
"button_variants": {
142135
"disabled": "Button disabled",
143136
"header": "Button variants",
@@ -166,6 +159,18 @@
166159
"notification": {
167160
"description": "by react-native-notificated 🎉",
168161
"title": "In-app notification example"
162+
},
163+
"test_notification": "Test notification",
164+
"typography": {
165+
"label": "Typography",
166+
"xs": "xs",
167+
"sm": "sm",
168+
"md": "md",
169+
"lg": "lg",
170+
"xl": "xl",
171+
"2xl": "2xl",
172+
"3xl": "3xl",
173+
"4xl": "4xl"
169174
}
170175
},
171176
"confirm_email_screen": {

src/screens/ProfileScreen.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ProfileDeleteAccountButton } from '@baca/components/screens/profile/ProfileDeleteAccountButton'
22
import { ProfileDetailsForm } from '@baca/components/screens/profile/ProfileDetailsForm'
33
import { ProfileHeader } from '@baca/components/screens/profile/ProfileHeader'
4+
import { ProfilePasswordForm } from '@baca/components/screens/profile/ProfilePasswordForm'
45
import { Box, Spacer } from '@baca/design-system'
56
import { useTranslation, useScreenOptions } from '@baca/hooks'
67

@@ -16,6 +17,9 @@ export const ProfileScreen = () => {
1617
<ProfileHeader />
1718
<Spacer y={4} />
1819
<ProfileDetailsForm />
20+
<Spacer y={4} />
21+
<ProfilePasswordForm />
22+
<Spacer y={4} />
1923
<ProfileDeleteAccountButton />
2024
</Box>
2125
)

0 commit comments

Comments
 (0)