Skip to content

Commit

Permalink
Merge branch 'AccountUpdate'
Browse files Browse the repository at this point in the history
  • Loading branch information
NishilJ committed Nov 15, 2024
1 parent df86012 commit 2e38e7c
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/components/AccountUpdate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import React, { useState, useEffect } from 'react';
import { updateProfile, updateEmail } from 'firebase/auth';
import { auth } from '../firebaseconfig';
import {
Button,
TextField,
Heading,
View,
Flex,
Divider,
Provider,
defaultTheme
} from '@adobe/react-spectrum';

const AccountUpdate: React.FC = () => {
const [firstName, setFirstName] = useState<string>('');
const [lastName, setLastName] = useState<string>('');
const [email, setEmail] = useState<string>('');
const [phone, setPhone] = useState<string>('');

useEffect(() => {
const user = auth.currentUser;
if (user) {
// Populate fields with the current user data
setFirstName(user.displayName?.split(' ')[0] || '');
setLastName(user.displayName?.split(' ')[1] || '');
setEmail(user.email || '');
}
}, []);

const handleUpdate = async () => {
const user = auth.currentUser;
if (user) {
try {
// Update display name
await updateProfile(user, {
displayName: `${firstName} ${lastName}`
});

// Update email
if (email !== user.email) {
await updateEmail(user, email);
}

alert('Account information updated successfully.');
} catch (error) {
console.error("Error updating account information:", error);
alert((error as Error).message);
}
}
};

return (
<Provider theme={defaultTheme}>
<View padding="size-300" width="size-4600">
<Heading level={2}>Account Settings</Heading>
<Divider marginBottom="size-300" />
<Flex direction="column" gap="size-200">
<TextField
label="First name"
value={firstName}
onChange={setFirstName}
/>
<TextField
label="Last name"
value={lastName}
onChange={setLastName}
/>
<TextField
label="Email address"
type="email"
value={email}
onChange={setEmail}
/>
<TextField
label="Phone number"
type="tel"
value={phone}
onChange={setPhone}
/>
<Button variant="cta" onPress={handleUpdate}>Update</Button>
</Flex>
</View>
</Provider>
);
};

export default AccountUpdate;

0 comments on commit 2e38e7c

Please sign in to comment.