Skip to content

Commit

Permalink
Added error feedback when choosing invalid name
Browse files Browse the repository at this point in the history
  • Loading branch information
notforrest committed Aug 14, 2024
1 parent aabe222 commit 42bb348
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions app/(tabs)/account/choose-full-name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,14 @@ import { StyleSheet, Text, TextInput, View } from "react-native";
import { Theme, useTheme } from "../../../utils/ThemeProvider";
import { supabase } from "../../../utils/supabase";
import { TouchableOpacity } from "react-native-gesture-handler";
import { ERROR_MESSAGES } from "../../../utils/ErrorMessages";

export default function ChooseFullName() {
const styles = getStyles(useTheme());
const [fullName, setFullName] = useState("");
const { userId } = useLocalSearchParams();

const handleSubmitFullName = async () => {
try {
const { error } = await supabase.from("profiles").upsert([
{
id: userId,
updated_at: new Date(),
full_name: fullName,
},
]);
if (error) {
throw error;
} else {
router.replace("/account/profile");
router.setParams({ userId: userId });
}
} catch (error) {
console.error("Error saving: ", error);
}
};
const [fullName, setFullName] = useState("");
const [errorMessage, setErrorMessage] = useState(" ");

return (
<View style={styles.container}>
Expand All @@ -41,9 +24,10 @@ export default function ChooseFullName() {
style={styles.input}
value={fullName}
/>
<Text style={styles.errorText}>{errorMessage}</Text>
<TouchableOpacity
disabled={!fullName}
onPress={handleSubmitFullName}
onPress={() => handleSubmitFullName(userId, fullName, setErrorMessage)}
style={styles.button}
>
<Text style={fullName ? styles.buttonText : styles.buttonDisabled}>
Expand All @@ -54,6 +38,42 @@ export default function ChooseFullName() {
);
}

const handleSubmitFullName = async (
userId: string | string[],
fullName: string,
setErrorMessage: (message: string) => void,
) => {
try {
if (!isValidName(fullName)) {
setErrorMessage(ERROR_MESSAGES[90001]);
return;
}

const { error } = await supabase.from("profiles").upsert([
{
id: userId,
updated_at: new Date(),
full_name: fullName,
},
]);
if (error) {
throw error;
} else {
router.replace("/account/profile");
router.setParams({ userId: userId });
}
} catch (error) {
const typedError = error as { code: number };
setErrorMessage(ERROR_MESSAGES[typedError.code] || "Error saving");
}
};

// Validate full name with regex pattern
const isValidName = (username: string) => {
const regex = /^(?!\s|-)[a-zA-Z\s-]{3,19}[a-zA-Z]$/;
return regex.test(username);
};

const getStyles = (theme: Theme) =>
StyleSheet.create({
container: {
Expand All @@ -73,7 +93,6 @@ const getStyles = (theme: Theme) =>
button: {
alignItems: "center",
borderRadius: 10,
marginTop: "20%",
padding: 10,
},
buttonDisabled: {
Expand All @@ -86,4 +105,10 @@ const getStyles = (theme: Theme) =>
color: theme.secondary,
fontSize: 24,
},
errorText: {
color: theme.error,
fontSize: 16,
marginTop: "5%",
minHeight: "15%",
},
});

0 comments on commit 42bb348

Please sign in to comment.