142 lines
3.7 KiB
TypeScript
142 lines
3.7 KiB
TypeScript
import React, { useState } from "react";
|
|
import {
|
|
Alert,
|
|
KeyboardAvoidingView,
|
|
Platform,
|
|
ScrollView,
|
|
StyleSheet,
|
|
View,
|
|
} from "react-native";
|
|
import { SafeAreaView } from "react-native-safe-area-context";
|
|
import {
|
|
moderateScale,
|
|
verticalScale,
|
|
} from "react-native-size-matters";
|
|
import { router } from "expo-router";
|
|
|
|
import SettingsHeader from "@/features/settings/components/SettingsHeader";
|
|
import { Colors } from "@/theme";
|
|
import EditProfileForm from "@/features/profile/components/EditProfileForm";
|
|
import EditProfileAvatar from "@/features/profile/components/EditProfileAvatar";
|
|
import { useAuthStore } from "@/store/auth.store";
|
|
import { apiClient } from "@/api";
|
|
import { validatePhoneNumber } from "@/constants/countries";
|
|
|
|
export default function EditProfileScreen() {
|
|
const user = useAuthStore((state) => state.user);
|
|
const refreshIdentity = useAuthStore((state) => state.refreshIdentity);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
const handleSave = async (
|
|
updatedName: string,
|
|
updatedPhone: string,
|
|
updatedCountry: string
|
|
) => {
|
|
if (!user) {
|
|
router.back();
|
|
return;
|
|
}
|
|
|
|
const trimmedName = updatedName.trim();
|
|
const trimmedPhone = updatedPhone.trim();
|
|
const trimmedCountry = updatedCountry.trim();
|
|
|
|
if (trimmedPhone) {
|
|
const errMsg = validatePhoneNumber(trimmedPhone, undefined, trimmedCountry);
|
|
if (errMsg) {
|
|
if (Platform.OS === "web") {
|
|
window.alert(errMsg);
|
|
} else {
|
|
Alert.alert("Validation Error", errMsg);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
setSaving(true);
|
|
try {
|
|
await apiClient.put("/auth/profile", {
|
|
full_name: trimmedName,
|
|
phone_number: trimmedPhone || null,
|
|
country: trimmedCountry || null,
|
|
});
|
|
|
|
await refreshIdentity();
|
|
|
|
const successMsg = "Profile updated successfully!";
|
|
if (Platform.OS === "web") {
|
|
window.alert(successMsg);
|
|
router.back();
|
|
} else {
|
|
Alert.alert("Success", successMsg, [
|
|
{ text: "OK", onPress: () => router.back() },
|
|
]);
|
|
}
|
|
} catch (err: any) {
|
|
const errMsg =
|
|
err?.response?.data?.detail ||
|
|
err?.message ||
|
|
"Failed to save profile changes. Please try again.";
|
|
console.warn("Failed to update profile:", err);
|
|
if (Platform.OS === "web") {
|
|
window.alert(errMsg);
|
|
} else {
|
|
Alert.alert("Error", errMsg);
|
|
}
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<SafeAreaView style={styles.safeArea}>
|
|
<KeyboardAvoidingView
|
|
style={styles.keyboardContainer}
|
|
behavior={Platform.OS === "ios" ? "padding" : undefined}
|
|
>
|
|
<ScrollView
|
|
keyboardShouldPersistTaps="handled"
|
|
showsVerticalScrollIndicator={false}
|
|
contentContainerStyle={styles.contentContainer}
|
|
>
|
|
<View style={styles.container}>
|
|
<SettingsHeader title="Edit Profile" />
|
|
|
|
<EditProfileAvatar name={user?.name || "User Name"} />
|
|
|
|
<EditProfileForm
|
|
initialName={user?.name || ""}
|
|
initialEmail={user?.email || ""}
|
|
initialPhone={user?.phone_number || ""}
|
|
initialCountry={user?.country || "India"}
|
|
onSave={handleSave}
|
|
isLoading={saving}
|
|
/>
|
|
</View>
|
|
</ScrollView>
|
|
</KeyboardAvoidingView>
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
safeArea: {
|
|
flex: 1,
|
|
backgroundColor: Colors.background,
|
|
},
|
|
|
|
keyboardContainer: {
|
|
flex: 1,
|
|
},
|
|
|
|
contentContainer: {
|
|
flexGrow: 1,
|
|
paddingBottom: verticalScale(32),
|
|
},
|
|
|
|
container: {
|
|
flex: 1,
|
|
paddingHorizontal: moderateScale(20),
|
|
paddingTop: verticalScale(12),
|
|
},
|
|
}); |