Prevent registers/logins multiple times when spamming buttons

This commit is contained in:
Keannu Bernasol 2023-10-15 11:56:39 +08:00
parent be21689639
commit b82b9d332f
2 changed files with 42 additions and 38 deletions

View file

@ -27,6 +27,7 @@ import { RootState } from "../../features/redux/Store/Store";
export default function Login() { export default function Login() {
const navigation = useNavigation<RootDrawerParamList>(); const navigation = useNavigation<RootDrawerParamList>();
const status = useSelector((state: RootState) => state.status); const status = useSelector((state: RootState) => state.status);
const [logging_in, setLoggingIn] = useState(false);
const dispatch = useDispatch(); const dispatch = useDispatch();
const [creds, setCreds] = useState({ const [creds, setCreds] = useState({
username: "", username: "",
@ -76,49 +77,52 @@ export default function Login() {
<View style={{ paddingVertical: 4 }} /> <View style={{ paddingVertical: 4 }} />
<Button <Button
onPress={async () => { onPress={async () => {
await UserLogin({ if (logging_in) {
username: creds.username, await UserLogin({
password: creds.password, username: creds.username,
}).then(async (result) => { password: creds.password,
if (result[0]) { }).then(async (result) => {
setUser({ ...creds, username: "", password: "", error: "" }); if (result[0]) {
let user_info = await GetUserInfo(); setUser({ ...creds, username: "", password: "", error: "" });
dispatch(login()); let user_info = await GetUserInfo();
dispatch(setUser(user_info[1])); dispatch(login());
// Redirect to onboarding if no year level, course, or semester specified dispatch(setUser(user_info[1]));
if ( // Redirect to onboarding if no year level, course, or semester specified
user_info[1].year_level == null || if (
user_info[1].course == null || user_info[1].year_level == null ||
user_info[1].semester == null user_info[1].course == null ||
) { user_info[1].semester == null
dispatch(setOnboarding()); ) {
navigation.navigate("Onboarding"); dispatch(setOnboarding());
toast.show("Successfully logged in", { navigation.navigate("Onboarding");
type: "success", toast.show("Successfully logged in", {
placement: "top", type: "success",
duration: 2000, placement: "top",
animationType: "slide-in", duration: 2000,
}); animationType: "slide-in",
});
} else {
dispatch(unsetOnboarding());
toast.show("Successfully logged in", {
type: "success",
placement: "top",
duration: 2000,
animationType: "slide-in",
});
navigation.navigate("Home");
}
console.log(JSON.stringify(user_info));
} else { } else {
dispatch(unsetOnboarding()); toast.show(JSON.stringify(result[1]), {
toast.show("Successfully logged in", { type: "warning",
type: "success",
placement: "top", placement: "top",
duration: 2000, duration: 2000,
animationType: "slide-in", animationType: "slide-in",
}); });
navigation.navigate("Home");
} }
console.log(JSON.stringify(user_info)); setLoggingIn(false);
} else { });
toast.show(JSON.stringify(result[1]), { }
type: "warning",
placement: "top",
duration: 2000,
animationType: "slide-in",
});
}
});
}} }}
> >
<Text style={styles.text_white_small}>Login</Text> <Text style={styles.text_white_small}>Login</Text>

View file

@ -170,7 +170,6 @@ export default function Register() {
animationType: "slide-in", animationType: "slide-in",
} }
); );
setRegistering(false);
setTimeout(() => { setTimeout(() => {
navigation.navigate("Login"); navigation.navigate("Login");
}, 10000); }, 10000);
@ -182,6 +181,7 @@ export default function Register() {
animationType: "slide-in", animationType: "slide-in",
}); });
} }
setRegistering(false);
}); });
} else { } else {
toast.show( toast.show(