Skip to content

Commit

Permalink
feat: handle loading and server timeout in StartMenu | [AntonioMrtz#261]
Browse files Browse the repository at this point in the history
  • Loading branch information
gigigimay committed Oct 24, 2024
1 parent 6c2e117 commit 67e1467
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Electron/src/global/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Global {
export const songArchitecture: SongArchitecture =
SongArchitecture.BLOB_ARCHITECTURE;

export const coldStartRequestTimeout = 5000;

export interface HandleUrlChangeResponse {
canGoBack: boolean | undefined;
canGoForward: boolean | undefined;
Expand Down
64 changes: 48 additions & 16 deletions Electron/src/pages/StartMenu/StartMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { getToken } from 'utils/token';
// eslint-disable-next-line camelcase
import { Body_login_user_login__post } from 'swagger/api/models/Body_login_user_login__post';
import timeout from 'utils/timeout';
import LoadingCircle from 'components/AdvancedUIComponents/LoadingCircle/LoadingCircle';
import Global from 'global/global';
import styles from './startMenu.module.css';
import SpotifyElectronLogo from '../../assets/imgs/SpotifyElectronLogo.png';
import { LoginService } from '../../swagger/api/services/LoginService';
Expand All @@ -17,6 +19,10 @@ interface PropsStartMenu {
setIsSigningUp: Function;
}

const coldStartTimeoutErrorTitle = 'El servidor esta iniciándose';
const coldStartTimeoutErrorDescription =
'El servidor esta iniciándose (cold-start), inténtelo de nuevo en 1 minuto';

export default function StartMenu({
setIsLogged,
setIsSigningUp,
Expand All @@ -37,6 +43,22 @@ export default function StartMenu({
password: '',
});

const showErrorPopover = ({
title,
description,
}: Pick<PropsInfoPopover, 'title' | 'description'>) => {
setPropsPopOver({
title,
description,
type: InfoPopoverType.ERROR,
triggerOpenConfirmationModal: false,
handleClose: () => {
setisOpenPopover(false);
},
});
setisOpenPopover(true);
};

const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setFormData({
Expand All @@ -61,7 +83,7 @@ export default function StartMenu({
const loginUserPromise = LoginService.loginUserLoginPost(loginData);
const loginResponse = await Promise.race([
loginUserPromise,
timeout(5000),
timeout(Global.coldStartRequestTimeout),
]);
localStorage.setItem('jwt', loginResponse);
setIsLogged(true);
Expand All @@ -73,24 +95,14 @@ export default function StartMenu({
let description: string;

if (error instanceof Error && error.message === 'Timeout') {
title = 'El servidor esta iniciándose';
description =
'El servidor esta iniciándose (cold-start), inténtelo de nuevo en 1 minuto';
title = coldStartTimeoutErrorTitle;
description = coldStartTimeoutErrorDescription;
} else {
title = 'Los credenciales introducidos no son válidos';
description = 'No se ha podido iniciar sesión';
}

setPropsPopOver({
title,
description,
type: InfoPopoverType.ERROR,
triggerOpenConfirmationModal: false,
handleClose: () => {
setisOpenPopover(false);
},
});
setisOpenPopover(true);
showErrorPopover({ title, description });
} finally {
setLoginLoading(false);
}
Expand All @@ -106,10 +118,22 @@ export default function StartMenu({
setAutoLoginLoading(true);
const token = getToken();
if (!token) return;
await LoginService.loginUserWithJwtLoginTokenTokenPost(token);
const autoLoginPromise =
LoginService.loginUserWithJwtLoginTokenTokenPost(token);
await Promise.race([
autoLoginPromise,
timeout(Global.coldStartRequestTimeout),
]);
setIsLogged(true);
} catch (error) {
console.log(`User invalid credentials for auto login with JWT token`);
if (error instanceof Error && error.message === 'Timeout') {
showErrorPopover({
title: coldStartTimeoutErrorTitle,
description: coldStartTimeoutErrorDescription,
});
} else {
console.log(`User invalid credentials for auto login with JWT token`);
}
} finally {
setAutoLoginLoading(false);
}
Expand All @@ -119,6 +143,11 @@ export default function StartMenu({

return (
<div className={`${styles.mainModalContainer}`}>
{(autoLoginLoading || loginLoading) && (
<div className={`${styles.loadingCircleWrapper}`}>
<LoadingCircle />
</div>
)}
{!autoLoginLoading && (
<div className={`${styles.contentWrapper}`}>
<div className={`d-flex flex-row ${styles.titleContainer}`}>
Expand All @@ -145,6 +174,7 @@ export default function StartMenu({
id="nombre"
placeholder="Nombre de usuario"
onChange={handleChange}
disabled={loginLoading}
spellCheck={false}
required
/>
Expand All @@ -160,6 +190,7 @@ export default function StartMenu({
id="password"
placeholder="Contraseña"
onChange={handleChange}
disabled={loginLoading}
spellCheck={false}
required
/>
Expand All @@ -185,6 +216,7 @@ export default function StartMenu({
</p>
<button
onClick={handleClickRegister}
disabled={loginLoading}
type="button"
style={{
color: 'var(--pure-white)',
Expand Down
17 changes: 17 additions & 0 deletions Electron/src/pages/StartMenu/startMenu.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
z-index: 9999;
}

.loadingCircleWrapper {
position: absolute;
z-index: 1;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

.contentWrapper {
background-color: black;
border-radius: 8px;
Expand Down Expand Up @@ -84,6 +93,10 @@
box-shadow: inset 0 0 0 3px var(--pure-white, #000000);
}

.formWrapper input:disabled {
opacity: 50%;
}

.loginButton {
border-radius: 999px;
background-color: var(--app-logo-color-darker);
Expand Down Expand Up @@ -122,6 +135,10 @@
color: var(--app-logo-color-darker) !important;
}

.wrapperRegisterText button:disabled {
opacity: 50%;
}

@media (max-width: 1336px) {
.contentWrapper h1 {
margin-bottom: 2rem !important;
Expand Down

0 comments on commit 67e1467

Please sign in to comment.