369 lines
9.7 KiB
TypeScript
369 lines
9.7 KiB
TypeScript
import React, { useState, useEffect } from "react";
|
|
import {
|
|
Modal,
|
|
Pressable,
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
} from "react-native";
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
import {
|
|
moderateScale,
|
|
scale,
|
|
verticalScale,
|
|
} from "react-native-size-matters";
|
|
|
|
import BaseTextInput from "@/components/forms/BaseTextInput";
|
|
import DropdownField from "@/components/forms/DropdownField";
|
|
import FieldLabel from "@/components/forms/FieldLabel";
|
|
import PrimaryButton from "@/components/buttons/PrimaryButton";
|
|
import { Colors } from "@/theme";
|
|
import {
|
|
getCountryRuleByName,
|
|
updatePhoneDialCode,
|
|
validatePhoneNumber,
|
|
isValidPhoneNumber,
|
|
parsePhoneNumber,
|
|
formatE164Phone,
|
|
cleanPhoneInput,
|
|
} from "@/constants/countries";
|
|
|
|
interface EditProfileFormProps {
|
|
initialName?: string;
|
|
initialEmail?: string;
|
|
initialPhone?: string;
|
|
initialCountry?: string;
|
|
onSave?: (updatedName: string, updatedPhone: string, updatedCountry: string) => void;
|
|
isLoading?: boolean;
|
|
}
|
|
|
|
const COMMON_COUNTRIES = [
|
|
"Australia",
|
|
"Canada",
|
|
"France",
|
|
"Germany",
|
|
"India",
|
|
"Ireland",
|
|
"Malaysia",
|
|
"Netherlands",
|
|
"New Zealand",
|
|
"Singapore",
|
|
"South Africa",
|
|
"Spain",
|
|
"Switzerland",
|
|
"United Arab Emirates",
|
|
"United Kingdom",
|
|
"United States",
|
|
];
|
|
|
|
export default function EditProfileForm({
|
|
initialName = "User Name",
|
|
initialEmail = "user@example.com",
|
|
initialPhone = "",
|
|
initialCountry = "India",
|
|
onSave,
|
|
isLoading = false,
|
|
}: EditProfileFormProps) {
|
|
const [fullName, setFullName] = useState(initialName);
|
|
const [phone, setPhone] = useState(initialPhone || "");
|
|
const [country, setCountry] = useState(initialCountry);
|
|
const [nameError, setNameError] = useState("");
|
|
const [phoneError, setPhoneError] = useState("");
|
|
const [showCountryModal, setShowCountryModal] = useState(false);
|
|
|
|
const activeRule = getCountryRuleByName(country);
|
|
|
|
useEffect(() => {
|
|
setFullName(initialName);
|
|
}, [initialName]);
|
|
|
|
useEffect(() => {
|
|
setPhone(initialPhone || "");
|
|
}, [initialPhone]);
|
|
|
|
useEffect(() => {
|
|
setCountry(initialCountry);
|
|
}, [initialCountry]);
|
|
|
|
const handleNameChange = (text: string) => {
|
|
setFullName(text);
|
|
if (nameError && text.trim()) {
|
|
setNameError("");
|
|
}
|
|
};
|
|
|
|
const handlePhoneChange = (text: string) => {
|
|
const cleaned = cleanPhoneInput(text, activeRule.maxLength + activeRule.code.length + 2);
|
|
setPhone(cleaned);
|
|
if (phoneError) {
|
|
if (!cleaned.trim() || isValidPhoneNumber(cleaned, activeRule.code, country)) {
|
|
setPhoneError("");
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleCountrySelect = (newCountry: string) => {
|
|
if (newCountry !== country) {
|
|
const updatedPhone = updatePhoneDialCode(phone, country, newCountry);
|
|
setPhone(updatedPhone);
|
|
setCountry(newCountry);
|
|
if (phoneError) setPhoneError("");
|
|
}
|
|
setShowCountryModal(false);
|
|
};
|
|
|
|
const handleSave = () => {
|
|
let hasError = false;
|
|
setNameError("");
|
|
setPhoneError("");
|
|
|
|
const trimmedName = fullName.trim();
|
|
if (!trimmedName) {
|
|
setNameError("Full name is required");
|
|
hasError = true;
|
|
}
|
|
|
|
const trimmedPhone = phone.trim();
|
|
if (trimmedPhone) {
|
|
const rule = getCountryRuleByName(country);
|
|
const err = validatePhoneNumber(trimmedPhone, rule.code, country);
|
|
if (err) {
|
|
setPhoneError(err);
|
|
hasError = true;
|
|
}
|
|
}
|
|
|
|
if (hasError) return;
|
|
|
|
// Normalize phone before saving: ensure no spaces or hyphens, formatted as +<dialCode><nationalDigits>
|
|
let normalizedPhone = "";
|
|
if (trimmedPhone) {
|
|
const rule = getCountryRuleByName(country);
|
|
const parsed = parsePhoneNumber(trimmedPhone, country);
|
|
normalizedPhone = formatE164Phone(rule.code, parsed.localDigits, country);
|
|
}
|
|
|
|
onSave?.(trimmedName, normalizedPhone, country);
|
|
};
|
|
|
|
return (
|
|
<View style={styles.container}>
|
|
<View style={styles.field}>
|
|
<FieldLabel label="Full Name" required />
|
|
<BaseTextInput
|
|
value={fullName}
|
|
onChangeText={handleNameChange}
|
|
placeholder="Your full name"
|
|
autoCapitalize="words"
|
|
autoCorrect={false}
|
|
error={nameError}
|
|
/>
|
|
</View>
|
|
|
|
<View style={styles.field}>
|
|
<FieldLabel label="Email" required />
|
|
<BaseTextInput
|
|
value={initialEmail}
|
|
editable={false}
|
|
placeholder="Email"
|
|
keyboardType="email-address"
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
/>
|
|
<Text style={styles.helperText}>
|
|
Email address is tied to your account identity and subscriptions.
|
|
</Text>
|
|
</View>
|
|
|
|
<View style={styles.field}>
|
|
<FieldLabel label="Phone Number" />
|
|
<BaseTextInput
|
|
value={phone}
|
|
onChangeText={handlePhoneChange}
|
|
placeholder={activeRule.placeholder ? `e.g. ${activeRule.code} ${activeRule.placeholder}` : `e.g. ${activeRule.code} 9876543210`}
|
|
keyboardType="phone-pad"
|
|
maxLength={activeRule.maxLength + activeRule.code.length + 2}
|
|
textContentType="telephoneNumber"
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
error={phoneError}
|
|
/>
|
|
{!phoneError && (
|
|
<Text style={styles.helperText}>
|
|
{activeRule.minLength === activeRule.maxLength
|
|
? `${activeRule.country} (${activeRule.code}): Requires exactly ${activeRule.maxLength} digits`
|
|
: `${activeRule.country} (${activeRule.code}): ${activeRule.minLength} to ${activeRule.maxLength} digits`}
|
|
</Text>
|
|
)}
|
|
</View>
|
|
|
|
<View style={styles.field}>
|
|
<FieldLabel label="Country / Region" required />
|
|
<DropdownField
|
|
value={country}
|
|
placeholder="Select country"
|
|
onPress={() => setShowCountryModal(true)}
|
|
/>
|
|
</View>
|
|
|
|
<View style={styles.buttonWrapper}>
|
|
<PrimaryButton
|
|
title="Save Changes"
|
|
loading={isLoading}
|
|
onPress={handleSave}
|
|
/>
|
|
</View>
|
|
|
|
{/* Country Selection Modal */}
|
|
<Modal
|
|
visible={showCountryModal}
|
|
transparent
|
|
animationType="slide"
|
|
onRequestClose={() => setShowCountryModal(false)}
|
|
>
|
|
<Pressable
|
|
style={styles.modalOverlay}
|
|
onPress={() => setShowCountryModal(false)}
|
|
>
|
|
<View style={styles.modalSheet}>
|
|
<View style={styles.modalHeader}>
|
|
<Text style={styles.modalTitle}>Select Country</Text>
|
|
<Pressable
|
|
hitSlop={8}
|
|
onPress={() => setShowCountryModal(false)}
|
|
>
|
|
<Ionicons
|
|
name="close"
|
|
size={moderateScale(22)}
|
|
color={Colors.textSecondary}
|
|
/>
|
|
</Pressable>
|
|
</View>
|
|
|
|
<ScrollView
|
|
showsVerticalScrollIndicator={false}
|
|
contentContainerStyle={styles.countryList}
|
|
>
|
|
{COMMON_COUNTRIES.map((c) => {
|
|
const isSelected = country === c;
|
|
return (
|
|
<Pressable
|
|
key={c}
|
|
style={({ pressed }) => [
|
|
styles.countryItem,
|
|
isSelected && styles.countryItemSelected,
|
|
pressed && styles.countryItemPressed,
|
|
]}
|
|
onPress={() => handleCountrySelect(c)}
|
|
>
|
|
<Text
|
|
style={[
|
|
styles.countryName,
|
|
isSelected && styles.countryNameSelected,
|
|
]}
|
|
>
|
|
{c}
|
|
</Text>
|
|
{isSelected && (
|
|
<Ionicons
|
|
name="checkmark"
|
|
size={moderateScale(18)}
|
|
color={Colors.primary}
|
|
/>
|
|
)}
|
|
</Pressable>
|
|
);
|
|
})}
|
|
</ScrollView>
|
|
</View>
|
|
</Pressable>
|
|
</Modal>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
marginTop: verticalScale(4),
|
|
},
|
|
|
|
field: {
|
|
marginBottom: verticalScale(16),
|
|
},
|
|
|
|
helperText: {
|
|
marginTop: verticalScale(4),
|
|
fontSize: moderateScale(11),
|
|
color: Colors.textSecondary,
|
|
lineHeight: moderateScale(15),
|
|
},
|
|
|
|
buttonWrapper: {
|
|
marginTop: verticalScale(10),
|
|
},
|
|
|
|
modalOverlay: {
|
|
flex: 1,
|
|
backgroundColor: "rgba(0, 0, 0, 0.4)",
|
|
justifyContent: "flex-end",
|
|
},
|
|
|
|
modalSheet: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderTopLeftRadius: moderateScale(20),
|
|
borderTopRightRadius: moderateScale(20),
|
|
maxHeight: "70%",
|
|
paddingTop: verticalScale(16),
|
|
paddingBottom: verticalScale(32),
|
|
},
|
|
|
|
modalHeader: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
paddingHorizontal: scale(20),
|
|
paddingBottom: verticalScale(12),
|
|
borderBottomWidth: 1,
|
|
borderBottomColor: "#F1F5F9",
|
|
},
|
|
|
|
modalTitle: {
|
|
fontSize: moderateScale(17),
|
|
fontWeight: "700",
|
|
color: Colors.textPrimary,
|
|
},
|
|
|
|
countryList: {
|
|
paddingHorizontal: scale(16),
|
|
paddingTop: verticalScale(8),
|
|
},
|
|
|
|
countryItem: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
paddingVertical: verticalScale(12),
|
|
paddingHorizontal: scale(12),
|
|
borderRadius: moderateScale(10),
|
|
},
|
|
|
|
countryItemSelected: {
|
|
backgroundColor: "#DCFCE7",
|
|
},
|
|
|
|
countryItemPressed: {
|
|
backgroundColor: "#F1F5F9",
|
|
},
|
|
|
|
countryName: {
|
|
fontSize: moderateScale(14),
|
|
color: Colors.textPrimary,
|
|
fontWeight: "500",
|
|
},
|
|
|
|
countryNameSelected: {
|
|
fontWeight: "700",
|
|
color: "#15803D",
|
|
},
|
|
}); |