mirror of
https://github.com/Lissy93/web-check.git
synced 2024-11-26 02:03:24 +01:00
✨ Validate input and submit form
This commit is contained in:
parent
385927c1de
commit
5c44c97a71
@ -1,12 +1,13 @@
|
|||||||
import { useState } from 'react';
|
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
import { ChangeEvent, FormEvent, useState } from 'react';
|
||||||
|
import { useNavigate, NavigateOptions } from 'react-router-dom';
|
||||||
|
|
||||||
import colors from 'styles/colors';
|
|
||||||
import Heading from 'components/Form/Heading';
|
import Heading from 'components/Form/Heading';
|
||||||
import Input from 'components/Form/Input'
|
import Input from 'components/Form/Input'
|
||||||
import Button from 'components/Form/Button';
|
import Button from 'components/Form/Button';
|
||||||
import FancyBackground from 'components/misc/FancyBackground';
|
import FancyBackground from 'components/misc/FancyBackground';
|
||||||
|
|
||||||
|
import colors from 'styles/colors';
|
||||||
import { determineAddressType } from 'utils/address-type-checker';
|
import { determineAddressType } from 'utils/address-type-checker';
|
||||||
|
|
||||||
const HomeContainer = styled.section`
|
const HomeContainer = styled.section`
|
||||||
@ -18,7 +19,7 @@ const HomeContainer = styled.section`
|
|||||||
font-family: 'PTMono';
|
font-family: 'PTMono';
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const UserInputMain = styled.div`
|
const UserInputMain = styled.form`
|
||||||
background: ${colors.backgroundLighter};
|
background: ${colors.backgroundLighter};
|
||||||
box-shadow: 4px 4px 0px ${colors.bgShadowColor};
|
box-shadow: 4px 4px 0px ${colors.bgShadowColor};
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
@ -29,19 +30,47 @@ const UserInputMain = styled.div`
|
|||||||
margin: 1rem;
|
margin: 1rem;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const ErrorMessage = styled.p`
|
||||||
|
color: ${colors.danger};
|
||||||
|
margin: 0.5rem;
|
||||||
|
`;
|
||||||
|
|
||||||
const Home = (): JSX.Element => {
|
const Home = (): JSX.Element => {
|
||||||
const [userInput, setUserInput] = useState('');
|
const [userInput, setUserInput] = useState('');
|
||||||
|
const [errorMsg, setErrMsg] = useState('');
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
/* Check is valid address, either show err or redirect to results page */
|
||||||
const submit = () => {
|
const submit = () => {
|
||||||
// TODO - Submit form, using the following data:
|
const address = userInput;
|
||||||
console.log('Will Submit with: ', userInput);
|
const addressType = determineAddressType(address);
|
||||||
console.log('Address Type: ', determineAddressType(userInput));
|
|
||||||
|
if (addressType === 'empt') {
|
||||||
|
setErrMsg('Field must not be empty');
|
||||||
|
} else if (addressType === 'err') {
|
||||||
|
setErrMsg('Must be a valid URL, IPv4 or IPv6 Address');
|
||||||
|
} else {
|
||||||
|
const resultRouteParams: NavigateOptions = { state: { address, addressType } };
|
||||||
|
navigate('/results', resultRouteParams);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Update user input state, and hide error message if field is valid */
|
||||||
|
const inputChange = (event: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
setUserInput(event.target.value);
|
||||||
|
const isError = ['err', 'empt'].includes(determineAddressType(event.target.value));
|
||||||
|
if (!isError) setErrMsg('');
|
||||||
|
};
|
||||||
|
|
||||||
|
const formSubmitEvent = (event: FormEvent<HTMLFormElement>) => {
|
||||||
|
event.preventDefault();
|
||||||
|
submit();
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<HomeContainer>
|
<HomeContainer>
|
||||||
<FancyBackground />
|
<FancyBackground />
|
||||||
<UserInputMain>
|
<UserInputMain onSubmit={formSubmitEvent}>
|
||||||
<Heading as="h1" size="large" align="center" color={colors.primary}>Web Check</Heading>
|
<Heading as="h1" size="large" align="center" color={colors.primary}>Web Check</Heading>
|
||||||
<Input
|
<Input
|
||||||
id="user-input"
|
id="user-input"
|
||||||
@ -50,8 +79,9 @@ const Home = (): JSX.Element => {
|
|||||||
size="large"
|
size="large"
|
||||||
orientation="vertical"
|
orientation="vertical"
|
||||||
placeholder="e.g. https://duck.com"
|
placeholder="e.g. https://duck.com"
|
||||||
handleChange={(e) => setUserInput(e.target.value)}
|
handleChange={inputChange}
|
||||||
/>
|
/>
|
||||||
|
{ errorMsg && <ErrorMessage>{errorMsg}</ErrorMessage>}
|
||||||
<Button size="large" onClick={submit}>Analyze!</Button>
|
<Button size="large" onClick={submit}>Analyze!</Button>
|
||||||
</UserInputMain>
|
</UserInputMain>
|
||||||
</HomeContainer>
|
</HomeContainer>
|
||||||
|
Loading…
Reference in New Issue
Block a user