diff --git a/package-lock.json b/package-lock.json index a153112..d1f2d4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,8 @@ "@types/react-dom": "^18.0.9", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-router": "^6.4.3", + "react-router-dom": "^6.4.3", "react-scripts": "5.0.1", "typescript": "^4.9.3", "web-vitals": "^2.1.4" @@ -3071,6 +3073,14 @@ } } }, + "node_modules/@remix-run/router": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.0.3.tgz", + "integrity": "sha512-ceuyTSs7PZ/tQqi19YZNBc5X7kj1f8p+4DIyrcIYFY9h+hd1OKm4RqtiWldR9eGEvIiJfsqwM4BsuCtRIuEw6Q==", + "engines": { + "node": ">=14" + } + }, "node_modules/@rollup/plugin-babel": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/@rollup/plugin-babel/-/plugin-babel-5.3.1.tgz", @@ -13937,6 +13947,36 @@ "node": ">=0.10.0" } }, + "node_modules/react-router": { + "version": "6.4.3", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.4.3.tgz", + "integrity": "sha512-BT6DoGn6aV1FVP5yfODMOiieakp3z46P1Fk0RNzJMACzE7C339sFuHebfvWtnB4pzBvXXkHP2vscJzWRuUjTtA==", + "dependencies": { + "@remix-run/router": "1.0.3" + }, + "engines": { + "node": ">=14" + }, + "peerDependencies": { + "react": ">=16.8" + } + }, + "node_modules/react-router-dom": { + "version": "6.4.3", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.4.3.tgz", + "integrity": "sha512-MiaYQU8CwVCaOfJdYvt84KQNjT78VF0TJrA17SIQgNHRvLnXDJO6qsFqq8F/zzB1BWZjCFIrQpu4QxcshitziQ==", + "dependencies": { + "@remix-run/router": "1.0.3", + "react-router": "6.4.3" + }, + "engines": { + "node": ">=14" + }, + "peerDependencies": { + "react": ">=16.8", + "react-dom": ">=16.8" + } + }, "node_modules/react-scripts": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-5.0.1.tgz", @@ -18809,6 +18849,11 @@ "source-map": "^0.7.3" } }, + "@remix-run/router": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.0.3.tgz", + "integrity": "sha512-ceuyTSs7PZ/tQqi19YZNBc5X7kj1f8p+4DIyrcIYFY9h+hd1OKm4RqtiWldR9eGEvIiJfsqwM4BsuCtRIuEw6Q==" + }, "@rollup/plugin-babel": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/@rollup/plugin-babel/-/plugin-babel-5.3.1.tgz", @@ -26547,6 +26592,23 @@ "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.11.0.tgz", "integrity": "sha512-F27qZr8uUqwhWZboondsPx8tnC3Ct3SxZA3V5WyEvujRyyNv0VYPhoBg1gZ8/MV5tubQp76Trw8lTv9hzRBa+A==" }, + "react-router": { + "version": "6.4.3", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.4.3.tgz", + "integrity": "sha512-BT6DoGn6aV1FVP5yfODMOiieakp3z46P1Fk0RNzJMACzE7C339sFuHebfvWtnB4pzBvXXkHP2vscJzWRuUjTtA==", + "requires": { + "@remix-run/router": "1.0.3" + } + }, + "react-router-dom": { + "version": "6.4.3", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.4.3.tgz", + "integrity": "sha512-MiaYQU8CwVCaOfJdYvt84KQNjT78VF0TJrA17SIQgNHRvLnXDJO6qsFqq8F/zzB1BWZjCFIrQpu4QxcshitziQ==", + "requires": { + "@remix-run/router": "1.0.3", + "react-router": "6.4.3" + } + }, "react-scripts": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-5.0.1.tgz", diff --git a/package.json b/package.json index f32daa0..704ad45 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "@types/react-dom": "^18.0.9", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-router": "^6.4.3", + "react-router-dom": "^6.4.3", "react-scripts": "5.0.1", "typescript": "^4.9.3", "web-vitals": "^2.1.4" diff --git a/src/App.tsx b/src/App.tsx index a53698a..ec0b7f1 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,26 +1,48 @@ -import React from 'react'; -import logo from './logo.svg'; -import './App.css'; +import { Routes, Route, Outlet, Link } from "react-router-dom"; +import { CheckName } from './pages/CheckName'; +import { Home } from './pages/Home'; -function App() { +export default function App() { return ( -
-
- logo -

- Edit src/App.tsx and save to reload. -

- - Learn React - -
+ + }> + } /> + } /> + } /> + + + ); +} + +function Layout() { + return ( +
+ +
+
); } -export default App; + + + +function NoMatch() { + return ( +
+

Nothing to see here!

+

+ Go to the home page +

+
+ ); +} \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 032464f..2edb089 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,19 +1,15 @@ -import React from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; +import { BrowserRouter } from "react-router-dom"; const root = ReactDOM.createRoot( document.getElementById('root') as HTMLElement ); -root.render( - - - -); +root.render(); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals -reportWebVitals(); +reportWebVitals(); \ No newline at end of file diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts new file mode 100644 index 0000000..0f5b798 --- /dev/null +++ b/src/interfaces/index.ts @@ -0,0 +1,33 @@ +interface ValidationError { + target?: object; + property: string; + value?: any; + constraints?: { + [type: string]: string; + }; + children?: ValidationError[]; + contexts?: { + }; +} + +export interface UpdateInfoRequest { + name: string; +} + +interface BaseResponseInteface { + success: boolean; + data?: any; + errors?: ValidationError[]; +} + +interface BaseResponseSuccess extends BaseResponseInteface { + success: true; + data: any; +} + +interface BaseResponseError extends BaseResponseInteface { + success: false; + errors: ValidationError[]; +} + +export type BaseResponse = BaseResponseSuccess | BaseResponseError; diff --git a/src/pages/CheckName.tsx b/src/pages/CheckName.tsx new file mode 100644 index 0000000..ef93866 --- /dev/null +++ b/src/pages/CheckName.tsx @@ -0,0 +1,73 @@ +import { useEffect, useState } from 'react'; +import { BaseResponse } from '../interfaces'; + +export function CheckName() { + const [status, setStatus] = useState<'INITIAL' | 'SEND_DATA' | 'SENDING_DATA' | 'DATA_SENDED' | 'ERROR_SENDING_DATA'>(); + const [value, setValue] = useState(''); + const [data , setData] = useState(); + + useEffect(() => { + if(status === 'SEND_DATA') { + setStatus('SENDING_DATA'); + fetch('http://localhost:3001/info/validate', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + name: value, + }) + }) + .then((rawResponse) => { + if([200, 201].includes(rawResponse.status)) { + return rawResponse.json(); + } else { + throw new Error(); + } + }) + .then((response: BaseResponse) => { + setStatus('DATA_SENDED'); + setData(response); + }) + .catch(e => { + setStatus('ERROR_SENDING_DATA'); + }) + } + }, [status, value]); + + if (status === 'ERROR_SENDING_DATA') { + return ( +
+

ERRORE INVIO DATI

+ +
+ ); + } + + if(status === 'SEND_DATA' || status === 'SENDING_DATA') { + return ( +
+

INVIO IN CORSO

+ +
+ ); + } + + if(status === 'DATA_SENDED') { + return (
+ {data?.success === true &&

DATI INVIATI VALIDI

} + {data?.success === false &&

DATI INVIATI NON VALIDI

} + +
) + } + + return ( +
+

INSERISCI IL NOME

+ { + setValue(e.target.value); + }}> + +
+ ); +} diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx new file mode 100644 index 0000000..640c627 --- /dev/null +++ b/src/pages/Home.tsx @@ -0,0 +1,7 @@ +export function Home() { + return ( +
+

Home

+
+ ); +} \ No newline at end of file