|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useState, |
| 4 | +} from 'react'; |
| 5 | +import { useRouter } from 'expo-router'; |
| 6 | +import { updateProfile } from 'firebase/auth'; |
| 7 | +import { |
| 8 | + ref, |
| 9 | + update, |
| 10 | +} from 'firebase/database'; |
| 11 | + |
| 12 | +import BlockListView from '@/components/BlockListView'; |
| 13 | +import Button from '@/components/Button'; |
| 14 | +import Page from '@/components/Page'; |
| 15 | +import PageHeader from '@/components/PageHeader'; |
| 16 | +import TextInput from '@/components/TextInput'; |
| 17 | +import { showAlert } from '@/components/Toast'; |
| 18 | +import useAuth from '@/hooks/useAuth'; |
| 19 | +import useFirebaseMutation from '@/hooks/useFirebaseMutation'; |
| 20 | +import { |
| 21 | + MIN_USERNAME_LENGTH, |
| 22 | + usernameExists, |
| 23 | + validateUserName, |
| 24 | +} from '@/utils/common'; |
| 25 | +import { firebaseDatabase } from '@/utils/firebase'; |
| 26 | + |
| 27 | +const usernameErrorText = 'Username must be at least 4 characters long and cannot contain space and uppercase'; |
| 28 | +const usernameAlreadyExists = 'Username must be at least 4 characters long and cannot contain space and uppercase'; |
| 29 | +const errorTitle = 'Failed to Change s UsernameUsername must be at least 4 characters long and cannot contain space and uppercase'; |
| 30 | +const successTitle = 'Username updated successfully!'; |
| 31 | + |
| 32 | +export default function ChangePassword() { |
| 33 | + const { user, setUser } = useAuth(); |
| 34 | + const [oldUsername, setOldUsername] = useState<string>(''); |
| 35 | + const [newUserName, setNewUserName] = useState<string>(''); |
| 36 | + const router = useRouter(); |
| 37 | + const { mutate, isLoading } = useFirebaseMutation(); |
| 38 | + |
| 39 | + const handleUpdateProfile = useCallback(async () => { |
| 40 | + const isCurrentUsernameCorrect = user?.displayName === oldUsername; |
| 41 | + const isSame = newUserName === oldUsername; |
| 42 | + const isValid = validateUserName(newUserName) && !isSame && isCurrentUsernameCorrect; |
| 43 | + if (!isValid) { |
| 44 | + showAlert({ |
| 45 | + title: errorTitle, |
| 46 | + message: usernameErrorText, |
| 47 | + alertType: 'error', |
| 48 | + }); |
| 49 | + return; |
| 50 | + } |
| 51 | + const userNameAlreadyExist = await usernameExists(newUserName); |
| 52 | + if (userNameAlreadyExist) { |
| 53 | + showAlert({ |
| 54 | + title: errorTitle, |
| 55 | + message: usernameAlreadyExists, |
| 56 | + alertType: 'error', |
| 57 | + shouldHideAfterDelay: false, |
| 58 | + }); |
| 59 | + return; |
| 60 | + } |
| 61 | + mutate(newUserName, async (name) => { |
| 62 | + if (!user) return; |
| 63 | + await updateProfile(user, { displayName: name as string }); |
| 64 | + await update(ref(firebaseDatabase, `users/${user.uid}`), { username: name }); |
| 65 | + await user.reload(); |
| 66 | + setUser({ ...user }); |
| 67 | + setNewUserName(''); |
| 68 | + setOldUsername(''); |
| 69 | + }).then(() => { |
| 70 | + showAlert({ |
| 71 | + title: 'Success', |
| 72 | + message: successTitle, |
| 73 | + alertType: 'success', |
| 74 | + }); |
| 75 | + router.back(); |
| 76 | + }).catch((err) => { |
| 77 | + // eslint-disable-next-line no-console |
| 78 | + console.error(err); |
| 79 | + }); |
| 80 | + }, [oldUsername, newUserName, user, setUser, mutate, router]); |
| 81 | + |
| 82 | + return ( |
| 83 | + <Page title="Change Username"> |
| 84 | + <PageHeader heading="Change Username" /> |
| 85 | + <BlockListView |
| 86 | + withPadding |
| 87 | + > |
| 88 | + <TextInput |
| 89 | + variant="normal" |
| 90 | + labelText="Current Username" |
| 91 | + value={oldUsername} |
| 92 | + onChangeText={setOldUsername} |
| 93 | + /> |
| 94 | + <TextInput |
| 95 | + variant="normal" |
| 96 | + labelText="New Username" |
| 97 | + value={newUserName} |
| 98 | + onChangeText={setNewUserName} |
| 99 | + maxLength={128} |
| 100 | + editable={!isLoading} |
| 101 | + /> |
| 102 | + <Button |
| 103 | + name="change-username" |
| 104 | + title={isLoading |
| 105 | + ? ('Updating Username') |
| 106 | + : ('Confirm UserName Change')} |
| 107 | + disabled={ |
| 108 | + isLoading |
| 109 | + || (newUserName?.length ?? 0) < MIN_USERNAME_LENGTH |
| 110 | + } |
| 111 | + onPress={handleUpdateProfile} |
| 112 | + |
| 113 | + /> |
| 114 | + </BlockListView> |
| 115 | + </Page> |
| 116 | + ); |
| 117 | +} |
0 commit comments