Initial commit
This commit is contained in:
81
src/App.jsx
Normal file
81
src/App.jsx
Normal file
@@ -0,0 +1,81 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import reactLogo from './assets/react.svg'
|
||||
import viteLogo from '/vite.svg'
|
||||
import './App.css'
|
||||
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
|
||||
import FoOldal from './components/FoOldal'
|
||||
import AutoLista from './components/AutoLista'
|
||||
import Osszefoglalas from './components/Osszefoglalas'
|
||||
import UjAutoForm from './components/UjAutoForm'
|
||||
|
||||
function App() {
|
||||
const [autok, setAutok] = useState([])
|
||||
|
||||
useEffect(() => {
|
||||
const fetchAutok = async () => {
|
||||
try {
|
||||
const res = await fetch('http://localhost:3000/api/autok')
|
||||
if (!res.ok) throw new Error('Hálózati hiba')
|
||||
const data = await res.json()
|
||||
setAutok(data)
|
||||
} catch (err) {
|
||||
console.error('Autók betöltése sikertelen', err)
|
||||
}
|
||||
}
|
||||
fetchAutok()
|
||||
}, [])
|
||||
|
||||
const hozzaAdas = async (ujAuto) => {
|
||||
try {
|
||||
const res = await fetch('http://localhost:3000/api/autok', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(ujAuto),
|
||||
})
|
||||
if (!res.ok) throw new Error('Hiba a létrehozásnál')
|
||||
const created = await res.json()
|
||||
setAutok((prev) => [...prev, created])
|
||||
return created
|
||||
} catch (err) {
|
||||
console.error('Autó hozzáadása sikertelen', err)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const elemTorles = async (id) => {
|
||||
try {
|
||||
const res = await fetch(`http://localhost:3000/api/autok/${id}`, { method: 'DELETE' })
|
||||
if (!res.ok) throw new Error('Törlés sikertelen')
|
||||
setAutok((prev) => prev.filter((a) => a.id !== id))
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error('Autó törlése sikertelen', err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>
|
||||
<a href="https://vite.dev" target="_blank">
|
||||
<img src={viteLogo} className="logo" alt="Vite logo" />
|
||||
</a>
|
||||
<a href="https://react.dev" target="_blank">
|
||||
<img src={reactLogo} className="logo react" alt="React logo" />
|
||||
</a>
|
||||
</div>
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route path="/" element={<FoOldal />}>
|
||||
<Route index element={<Osszefoglalas autok={autok} />} />
|
||||
<Route path="lista" element={<AutoLista autok={autok} torles={elemTorles} />} />
|
||||
<Route path="uj" element={<UjAutoForm hozzaad={hozzaAdas} />} />
|
||||
<Route path="*" element={<Navigate to="/" replace />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default App
|
||||
Reference in New Issue
Block a user