Compare commits
No commits in common. "fef1751c55ab6f4e4b29b38fb47bce889dfb47f0" and "08ac950b7dd3b68a3f85f5635025f62e05ca42d6" have entirely different histories.
fef1751c55
...
08ac950b7d
|
|
@ -48,27 +48,6 @@ const COMMON_COUNTRIES = [
|
||||||
"Spain",
|
"Spain",
|
||||||
];
|
];
|
||||||
|
|
||||||
export const isValidPhoneNumber = (val?: string): boolean => {
|
|
||||||
if (!val || !val.trim()) return true;
|
|
||||||
const trimmed = val.trim();
|
|
||||||
// Must match valid phone character format (optional '+', digits, spaces, hyphens, parentheses)
|
|
||||||
const phoneRegex = /^\+?[0-9\s\-()]+$/;
|
|
||||||
if (!phoneRegex.test(trimmed)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const digits = trimmed.replace(/\D/g, "");
|
|
||||||
// ITU-T E.164 standard allows 6 to 15 digits
|
|
||||||
return digits.length >= 6 && digits.length <= 15;
|
|
||||||
};
|
|
||||||
|
|
||||||
const cleanPhoneInput = (text: string): string => {
|
|
||||||
const trimmedStart = text.trimStart();
|
|
||||||
const hasPlus = trimmedStart.startsWith("+");
|
|
||||||
const rest = hasPlus ? trimmedStart.slice(1) : text;
|
|
||||||
const filtered = rest.replace(/[^0-9\s\-()]/g, "");
|
|
||||||
return hasPlus ? `+${filtered}` : filtered;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function EditProfileForm({
|
export default function EditProfileForm({
|
||||||
initialName = "User Name",
|
initialName = "User Name",
|
||||||
initialEmail = "user@example.com",
|
initialEmail = "user@example.com",
|
||||||
|
|
@ -78,10 +57,8 @@ export default function EditProfileForm({
|
||||||
isLoading = false,
|
isLoading = false,
|
||||||
}: EditProfileFormProps) {
|
}: EditProfileFormProps) {
|
||||||
const [fullName, setFullName] = useState(initialName);
|
const [fullName, setFullName] = useState(initialName);
|
||||||
const [phone, setPhone] = useState(initialPhone || "");
|
const [phone, setPhone] = useState(initialPhone);
|
||||||
const [country, setCountry] = useState(initialCountry);
|
const [country, setCountry] = useState(initialCountry);
|
||||||
const [nameError, setNameError] = useState("");
|
|
||||||
const [phoneError, setPhoneError] = useState("");
|
|
||||||
const [showCountryModal, setShowCountryModal] = useState(false);
|
const [showCountryModal, setShowCountryModal] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -89,63 +66,23 @@ export default function EditProfileForm({
|
||||||
}, [initialName]);
|
}, [initialName]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setPhone(initialPhone || "");
|
setPhone(initialPhone);
|
||||||
}, [initialPhone]);
|
}, [initialPhone]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setCountry(initialCountry);
|
setCountry(initialCountry);
|
||||||
}, [initialCountry]);
|
}, [initialCountry]);
|
||||||
|
|
||||||
const handleNameChange = (text: string) => {
|
|
||||||
setFullName(text);
|
|
||||||
if (nameError && text.trim()) {
|
|
||||||
setNameError("");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlePhoneChange = (text: string) => {
|
|
||||||
const cleaned = cleanPhoneInput(text);
|
|
||||||
setPhone(cleaned);
|
|
||||||
if (phoneError) {
|
|
||||||
if (!cleaned.trim() || isValidPhoneNumber(cleaned)) {
|
|
||||||
setPhoneError("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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 && !isValidPhoneNumber(trimmedPhone)) {
|
|
||||||
setPhoneError("Please enter a valid phone number");
|
|
||||||
hasError = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasError) return;
|
|
||||||
|
|
||||||
onSave?.(trimmedName, trimmedPhone, country);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<View style={styles.field}>
|
<View style={styles.field}>
|
||||||
<FieldLabel label="Full Name" required />
|
<FieldLabel label="Full Name" required />
|
||||||
<BaseTextInput
|
<BaseTextInput
|
||||||
value={fullName}
|
value={fullName}
|
||||||
onChangeText={handleNameChange}
|
onChangeText={setFullName}
|
||||||
placeholder="Your full name"
|
placeholder="Your full name"
|
||||||
autoCapitalize="words"
|
autoCapitalize="words"
|
||||||
autoCorrect={false}
|
autoCorrect={false}
|
||||||
error={nameError}
|
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
|
|
@ -168,20 +105,12 @@ export default function EditProfileForm({
|
||||||
<FieldLabel label="Phone Number" />
|
<FieldLabel label="Phone Number" />
|
||||||
<BaseTextInput
|
<BaseTextInput
|
||||||
value={phone}
|
value={phone}
|
||||||
onChangeText={handlePhoneChange}
|
onChangeText={setPhone}
|
||||||
placeholder="e.g. +91 9876543210"
|
placeholder="e.g. +91 9876543210"
|
||||||
keyboardType="phone-pad"
|
keyboardType="phone-pad"
|
||||||
maxLength={25}
|
|
||||||
textContentType="telephoneNumber"
|
|
||||||
autoCapitalize="none"
|
autoCapitalize="none"
|
||||||
autoCorrect={false}
|
autoCorrect={false}
|
||||||
error={phoneError}
|
|
||||||
/>
|
/>
|
||||||
{!phoneError && (
|
|
||||||
<Text style={styles.helperText}>
|
|
||||||
Enter mobile number with country code for login and notifications.
|
|
||||||
</Text>
|
|
||||||
)}
|
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
<View style={styles.field}>
|
<View style={styles.field}>
|
||||||
|
|
@ -197,7 +126,9 @@ export default function EditProfileForm({
|
||||||
<PrimaryButton
|
<PrimaryButton
|
||||||
title="Save Changes"
|
title="Save Changes"
|
||||||
loading={isLoading}
|
loading={isLoading}
|
||||||
onPress={handleSave}
|
onPress={() => {
|
||||||
|
onSave?.(fullName, phone, country);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import { router } from "expo-router";
|
||||||
|
|
||||||
import SettingsHeader from "@/features/settings/components/SettingsHeader";
|
import SettingsHeader from "@/features/settings/components/SettingsHeader";
|
||||||
import { Colors } from "@/theme";
|
import { Colors } from "@/theme";
|
||||||
import EditProfileForm, { isValidPhoneNumber } from "@/features/profile/components/EditProfileForm";
|
import EditProfileForm from "@/features/profile/components/EditProfileForm";
|
||||||
import EditProfileAvatar from "@/features/profile/components/EditProfileAvatar";
|
import EditProfileAvatar from "@/features/profile/components/EditProfileAvatar";
|
||||||
import { useAuthStore } from "@/store/auth.store";
|
import { useAuthStore } from "@/store/auth.store";
|
||||||
import { apiClient } from "@/api";
|
import { apiClient } from "@/api";
|
||||||
|
|
@ -36,26 +36,12 @@ export default function EditProfileScreen() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const trimmedName = updatedName.trim();
|
|
||||||
const trimmedPhone = updatedPhone.trim();
|
|
||||||
const trimmedCountry = updatedCountry.trim();
|
|
||||||
|
|
||||||
if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) {
|
|
||||||
const errMsg = "Please enter a valid phone number";
|
|
||||||
if (Platform.OS === "web") {
|
|
||||||
window.alert(errMsg);
|
|
||||||
} else {
|
|
||||||
Alert.alert("Validation Error", errMsg);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
try {
|
try {
|
||||||
await apiClient.put("/auth/profile", {
|
await apiClient.put("/auth/profile", {
|
||||||
full_name: trimmedName,
|
full_name: updatedName.trim(),
|
||||||
phone_number: trimmedPhone || null,
|
phone_number: updatedPhone.trim() || null,
|
||||||
country: trimmedCountry || null,
|
country: updatedCountry.trim() || null,
|
||||||
});
|
});
|
||||||
|
|
||||||
await refreshIdentity();
|
await refreshIdentity();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue