Commit 18c39174 authored by bekzat kapan's avatar bekzat kapan

#7 Обернул главный компонент Provider-ом, прописал валидацию полей чтобы не…

#7 Обернул главный компонент Provider-ом, прописал валидацию полей чтобы не отправлять на сервер пустые значения, прописал логику показа ошибки снизу, и показа loader, при отправке данных. Добавил логику отправки сообщений через store. В файле store.ts добавил requestReducer
parent 23c5d165
"use client"; 'use client';
import { Box, Button, Container, Grid2, Typography } from "@mui/material"; import { Box, Button, Container, Grid2, Typography } from '@mui/material';
import ArrowDownwardIcon from "@mui/icons-material/ArrowDownward"; import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward';
import { ChangeEvent, FormEvent, useState } from "react"; import { ChangeEvent, useEffect, useState } from 'react';
import InputField from "./components/InputField"; import InputField from './components/InputField';
import '@/animations/Loader.css';
import { useAppDispatch, useAppSelector } from '@/store/hooks';
import { Provider } from 'react-redux';
import { store } from '@/store/store';
import { encodeMessage, decodeMessage } from '@/features/requestSlice';
interface IFormData { interface IFormData {
decoded: string; decoded: string;
...@@ -11,16 +16,53 @@ interface IFormData { ...@@ -11,16 +16,53 @@ interface IFormData {
} }
export default function Home() { export default function Home() {
const dispatch = useAppDispatch();
const { loading, result, error } = useAppSelector((state) => state.request);
const [formData, setFormData] = useState<IFormData>({ const [formData, setFormData] = useState<IFormData>({
encoded: "", encoded: '',
decoded: "", decoded: '',
password: "", password: '',
}); });
const submitFormHandler = (e: FormEvent<HTMLFormElement>) => { const onCypherHandler = (): void => {
e.preventDefault(); if (!formData.password.trim()) {
alert('Please, type in password!');
return;
}
if (!formData.encoded.trim()) {
alert('Please fill in the "Decoded" field!');
return;
}
dispatch(
encodeMessage({ password: formData.password, message: formData.decoded })
);
};
const onDecypherHandler = (): void => {
if (!formData.password.trim()) {
alert('Please, type in password!');
return;
}
if (!formData.decoded.trim()) {
alert('Please fill in the "Decoded" field!');
return;
}
dispatch(
decodeMessage({ password: formData.password, message: formData.decoded })
);
}; };
useEffect(() => {
if (result) {
if (formData.decoded) {
setFormData((prev) => ({ ...prev, encoded: result }));
} else {
setFormData((prev) => ({ ...prev, decoded: result }));
}
}
}, [result]);
const onInputChangeHandler = (e: ChangeEvent<HTMLInputElement>) => { const onInputChangeHandler = (e: ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target; const { name, value } = e.target;
setFormData((prevData) => ({ setFormData((prevData) => ({
...@@ -30,14 +72,15 @@ export default function Home() { ...@@ -30,14 +72,15 @@ export default function Home() {
}; };
return ( return (
<Provider store={store}>
<div className="App"> <div className="App">
<Container sx={{ marginTop: 2 }} maxWidth="lg"> <Container sx={{ marginTop: 2 }} maxWidth="lg">
<Typography <Typography
variant="h3" variant="h3"
sx={{ sx={{
fontSize: { fontSize: {
xs: "24px", xs: '24px',
sm: "36px", sm: '36px',
}, },
}} }}
> >
...@@ -46,15 +89,14 @@ export default function Home() { ...@@ -46,15 +89,14 @@ export default function Home() {
<Box <Box
sx={{ sx={{
maxWidth: { maxWidth: {
xs: "90%", xs: '90%',
sm: "80%", sm: '80%',
lg: "70%", lg: '70%',
}, },
}} }}
component="form" component="form"
autoComplete="off" autoComplete="off"
onSubmit={submitFormHandler} padding={2}
paddingY={2}
> >
<Grid2 container direction="column" spacing={2}> <Grid2 container direction="column" spacing={2}>
<InputField <InputField
...@@ -67,8 +109,8 @@ export default function Home() { ...@@ -67,8 +109,8 @@ export default function Home() {
alignItems="center" alignItems="center"
sx={{ sx={{
flexDirection: { flexDirection: {
xs: "column", xs: 'column',
md: "row", md: 'row',
}, },
}} }}
> >
...@@ -79,16 +121,17 @@ export default function Home() { ...@@ -79,16 +121,17 @@ export default function Home() {
onChange={onInputChangeHandler} onChange={onInputChangeHandler}
/> />
</Grid2> </Grid2>
<Grid2 container justifyContent={{}}> <Grid2 container>
<Grid2> <Grid2>
<Button <Button
size="small" size="small"
variant="contained" variant="contained"
startIcon={<ArrowDownwardIcon />} startIcon={<ArrowDownwardIcon />}
onClick={onDecypherHandler}
> >
<Typography <Typography
sx={{ sx={{
display: { xs: "none", md: "inline" }, display: { xs: 'none', md: 'inline' },
}} }}
> >
Encode Encode
...@@ -101,12 +144,15 @@ export default function Home() { ...@@ -101,12 +144,15 @@ export default function Home() {
size="small" size="small"
variant="contained" variant="contained"
startIcon={ startIcon={
<ArrowDownwardIcon sx={{ transform: "rotate(180deg)" }} /> <ArrowDownwardIcon
sx={{ transform: 'rotate(180deg)' }}
/>
} }
onClick={onCypherHandler}
> >
<Typography <Typography
sx={{ sx={{
display: { xs: "none", md: "inline" }, display: { xs: 'none', md: 'inline' },
}} }}
> >
Decode Decode
...@@ -123,7 +169,18 @@ export default function Home() { ...@@ -123,7 +169,18 @@ export default function Home() {
/> />
</Grid2> </Grid2>
</Box> </Box>
{loading && (
<div className="loader">
<div className="lds-ripple">
<div></div>
<div></div>
</div>
</div>
)}
{error && <Typography color="error">{error}</Typography>}
</Container> </Container>
</div> </div>
</Provider>
); );
} }
import { configureStore } from "@reduxjs/toolkit"; import { configureStore } from '@reduxjs/toolkit';
import requestReducer from '@/features/requestSlice';
export const store = configureStore({ export const store = configureStore({
reducer: { reducer: {
request: requestReducer,
}, },
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment