feat: update user context

This commit is contained in:
Vinicius Souza 2024-11-07 13:19:58 +00:00
parent 7f9a70e752
commit 065b7fb1a4
2 changed files with 18 additions and 2 deletions

View file

@ -13,6 +13,7 @@ type AuthContextData = {
user: UserDTO;
signIn: (email: string, password: string) => Promise<void>;
signOut: () => Promise<void>;
updateUserData: (userData: UserDTO) => Promise<void>;
isLoadingUserStorageData: boolean;
};
@ -71,6 +72,15 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
}
}
async function updateUserData(userData: UserDTO) {
try {
setUser(userData);
await storageUserSave(userData);
} catch (error) {
throw error;
}
}
async function loadUserData() {
try {
setIsLoadingUserStorageData(true);
@ -93,7 +103,8 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
}, []);
return (
<AuthContext.Provider value={{ user, signIn, signOut, isLoadingUserStorageData }}>
<AuthContext.Provider
value={{ user, signIn, signOut, updateUserData, isLoadingUserStorageData }}>
{children}
</AuthContext.Provider>
);

View file

@ -53,7 +53,7 @@ export function Profile() {
const [userPhoto, setUserPhoto] = useState('https://i.pravatar.cc/200');
const toast = useToast();
const { user } = useAuth();
const { user, updateUserData } = useAuth();
const {
control,
@ -79,6 +79,11 @@ export function Profile() {
password: data.new_password,
old_password: data.old_password,
});
const updatedUserData = user;
updatedUserData.name = data.name;
updateUserData(updatedUserData);
toast.show({
placement: 'top',
render: ({ id }) => (