add manager for edit profile service
This commit is contained in:
parent
dc46ba0a4b
commit
cc1a3728e5
91
mobile/src/hooks/useProfileManager.ts
Normal file
91
mobile/src/hooks/useProfileManager.ts
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
import { Alert } from 'react-native';
|
||||||
|
import { useNavigation } from '@react-navigation/native';
|
||||||
|
import * as ImagePicker from 'expo-image-picker';
|
||||||
|
import { useAuthStore } from '../store/useAuthStore';
|
||||||
|
import { authService } from '../services/api';
|
||||||
|
|
||||||
|
export const useProfileManager = () => {
|
||||||
|
const navigation = useNavigation();
|
||||||
|
const { user, updateUser } = useAuthStore();
|
||||||
|
|
||||||
|
const [firstName, setFirstName] = useState(user?.user?.first_name || '');
|
||||||
|
const [lastName, setLastName] = useState(user?.user?.last_name || '');
|
||||||
|
const [phone, setPhone] = useState(user?.user?.phone || '');
|
||||||
|
const [address, setAddress] = useState(user?.user?.address || '');
|
||||||
|
const [avatar, setAvatar] = useState<string | null>(user?.user?.avatar || null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
const pickImage = async () => {
|
||||||
|
const result = await ImagePicker.launchImageLibraryAsync({
|
||||||
|
mediaTypes: 'images',
|
||||||
|
allowsEditing: true,
|
||||||
|
aspect: [1, 1],
|
||||||
|
quality: 0.5,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result.canceled) {
|
||||||
|
setAvatar(result.assets[0].uri);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSave = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
let updatedAvatarUrl = user?.user?.avatar;
|
||||||
|
|
||||||
|
// Upload avatar if it has changed
|
||||||
|
if (avatar && avatar !== user?.user?.avatar) {
|
||||||
|
const avatarResult = await authService.uploadAvatar(avatar);
|
||||||
|
if (avatarResult.ok && avatarResult.data.data.avatar) {
|
||||||
|
updatedAvatarUrl = avatarResult.data.data.avatar;
|
||||||
|
} else {
|
||||||
|
Alert.alert('Warning', 'Failed to upload new profile picture, but proceeding with other updates.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const updates = {
|
||||||
|
first_name: firstName,
|
||||||
|
last_name: lastName,
|
||||||
|
phone,
|
||||||
|
address,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await authService.updateProfile(updates);
|
||||||
|
|
||||||
|
if (result.ok) {
|
||||||
|
const finalUserData = {
|
||||||
|
...(result.data.data || result.data),
|
||||||
|
avatar: updatedAvatarUrl
|
||||||
|
};
|
||||||
|
updateUser(finalUserData);
|
||||||
|
|
||||||
|
Alert.alert('Success', 'Profile updated successfully', [
|
||||||
|
{ text: 'OK', onPress: () => navigation.goBack() }
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
Alert.alert('Error', result.message || 'Failed to update profile');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
Alert.alert('Error', 'An unexpected error occurred.');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
loading,
|
||||||
|
avatar,
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
phone,
|
||||||
|
address,
|
||||||
|
setFirstName,
|
||||||
|
setLastName,
|
||||||
|
setPhone,
|
||||||
|
setAddress,
|
||||||
|
pickImage,
|
||||||
|
handleSave,
|
||||||
|
};
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user