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; user: UserDTO;
signIn: (email: string, password: string) => Promise<void>; signIn: (email: string, password: string) => Promise<void>;
signOut: () => Promise<void>; signOut: () => Promise<void>;
updateUserData: (userData: UserDTO) => Promise<void>;
isLoadingUserStorageData: boolean; 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() { async function loadUserData() {
try { try {
setIsLoadingUserStorageData(true); setIsLoadingUserStorageData(true);
@ -93,7 +103,8 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
}, []); }, []);
return ( return (
<AuthContext.Provider value={{ user, signIn, signOut, isLoadingUserStorageData }}> <AuthContext.Provider
value={{ user, signIn, signOut, updateUserData, isLoadingUserStorageData }}>
{children} {children}
</AuthContext.Provider> </AuthContext.Provider>
); );

View file

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