Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
feat(component): display error message for LoginForm after HTTP request
Browse files Browse the repository at this point in the history
  ## what
  - set component state property 'authError' to empty string in constructor
  - set component state property 'authError' to empty string when calling 'handleSubmit'
  - set component state property 'authError' to empty string when calling 'handleSubmit' and has finished HTTP request
  - set component state property 'authError' to error message  when calling 'handleSubmit' and has finished HTTP request and there's an error
  - display error message if there's any

  ## how

  ## why

  ## where

  ## usage
  • Loading branch information
Clumsy-Coder committed Aug 20, 2023
1 parent 31eefad commit 0e9e89c
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/components/LoginForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { ChangeEvent, Component, FormEvent, KeyboardEvent } from 'react';
import LoadingButton from '@mui/lab/LoadingButton';
import Grid from '@mui/material/Grid';
import TextField from '@mui/material/TextField';
import axios from 'axios';
import axios, { AxiosError } from 'axios';
import crypto from 'crypto';
import { Address4 } from 'ip-address';

import { PostRequestData } from '@pages/api/auth/login';
import { PostRequestData, PostResponseData } from '@pages/api/auth/login';
import Typography from '@mui/material/Typography';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IProps {}
Expand All @@ -23,6 +24,7 @@ interface IState extends PostRequestData {
*/
isIpValid: boolean;
isPortValid: boolean;
authError: string;
authLoading: boolean;
}

Expand All @@ -38,6 +40,7 @@ class LoginForm extends Component<IProps, IState> {
port: '80',
isIpValid: true,
isPortValid: true,
authError: '',
authLoading: false,
};
}
Expand Down Expand Up @@ -135,7 +138,7 @@ class LoginForm extends Component<IProps, IState> {

event.preventDefault();

this.setState({ authLoading: true });
this.setState({ authLoading: true, authError: '' });

axios
.post('/api/auth/login', {
Expand All @@ -145,18 +148,19 @@ class LoginForm extends Component<IProps, IState> {
})
.then((res) => {
console.log(res);
this.setState({ authLoading: false });
this.setState({ authLoading: false, authError: '' });
})
.catch((err: AxiosError<PostResponseData>) => {
console.log(err);
this.setState({
authLoading: false,
authError: err.response?.data.message ?? '',
});
});
};

render() {
const { authLoading, isPortValid, isIpValid, password, port } = this.state;
const { authLoading, authError, isPortValid, isIpValid, password, port } = this.state;

return (
<form method='POST' action='/api/auth/login' onSubmit={this.handleSubmit}>
Expand Down Expand Up @@ -223,6 +227,11 @@ class LoginForm extends Component<IProps, IState> {
Log in
</LoadingButton>
</Grid>
<Grid item alignSelf='center' sx={{ display: authError.length ? 12 : 'none' }}>
<Grid>
<Typography color='error'>{authError}</Typography>
</Grid>
</Grid>
</Grid>
</form>
);
Expand Down

0 comments on commit 0e9e89c

Please sign in to comment.