Skip to content

Commit

Permalink
feat: handle register loading and timeout in RegisterMenu | [AntonioM…
Browse files Browse the repository at this point in the history
  • Loading branch information
gigigimay committed Oct 18, 2024
1 parent cdaf4e6 commit adc1f27
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions Electron/src/pages/StartMenu/RegisterMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
PropsInfoPopover,
} from 'components/AdvancedUIComponents/InfoPopOver/types/InfoPopover';
import timeout from 'utils/timeout';
import Global from 'global/global';
import LoadingCircle from 'components/AdvancedUIComponents/LoadingCircle/LoadingCircle';
import styles from './startMenu.module.css';
import SpotifyElectronLogo from '../../assets/imgs/SpotifyElectronLogo.png';
import { UsersService } from '../../swagger/api/services/UsersService';
Expand Down Expand Up @@ -39,6 +41,9 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
null,
);

/* Loading state */
const [loading, setLoading] = useState(false);

const handleRegister = async (e: MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
if (
Expand Down Expand Up @@ -77,15 +82,20 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
}

try {
setLoading(true);

const createUserPromise = UsersService.createUserUsersPost(
formData.name,
formData.photo,
formData.password,
);

await Promise.race([createUserPromise, timeout(3000)]);
setisOpenPopover(true);
await Promise.race([
createUserPromise,
timeout(Global.coldStartRequestTimeout),
]);

setisOpenPopover(true);
setPropsPopOver({
title: 'Usuario registrado',
description: 'El usuario ha sido registrado con éxito',
Expand All @@ -97,6 +107,7 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
},
});
} catch (error: unknown) {
setLoading(false);
console.log('Unable to register');

let title: string;
Expand Down Expand Up @@ -130,6 +141,11 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {

return (
<div className={`${styles.mainModalContainer}`}>
{loading && (
<div className={`${styles.loadingCircleWrapper}`}>
<LoadingCircle />
</div>
)}
<div className={`${styles.contentWrapper}`}>
<div className={`d-flex flex-row ${styles.titleContainer}`}>
<img
Expand All @@ -155,6 +171,7 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
id="name"
placeholder="Nombre de usuario"
onChange={handleChange}
disabled={loading}
spellCheck={false}
required
/>
Expand All @@ -170,6 +187,7 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
id="photo"
placeholder="Foto de perfil"
onChange={handleChange}
disabled={loading}
spellCheck={false}
required
/>
Expand All @@ -187,6 +205,7 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
id="password"
placeholder="Contraseña"
onChange={handleChange}
disabled={loading}
spellCheck={false}
required
/>
Expand All @@ -203,6 +222,7 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
id="confirmpassword"
placeholder="Confirma tu contraseña"
onChange={handleChange}
disabled={loading}
spellCheck={false}
required
/>
Expand All @@ -215,6 +235,7 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
backgroundColor: 'var(--app-logo-color)',
}}
className={`${styles.registerButton}`}
disabled={loading}
onClick={handleRegister}
>
Registrar
Expand All @@ -231,6 +252,7 @@ export default function RegisterMenu({ setIsSigningUp }: PropsRegisterMenu) {
</p>
<button
onClick={handleClickLogin}
disabled={loading}
type="button"
style={{
color: 'var(--pure-white)',
Expand Down

0 comments on commit adc1f27

Please sign in to comment.