the react client now automatically fetches data

This commit is contained in:
Muhammad Azeez 2021-01-10 14:29:31 +03:00
parent 7fd435a217
commit 354c8260a1
2 changed files with 8 additions and 3 deletions

View File

@ -3,6 +3,7 @@ import { signoutRedirect } from '../services/userService'
import { useSelector } from 'react-redux'
import * as apiService from '../services/apiService'
import { prettifyJson } from '../utils/jsonUtils'
import { useEffect } from 'react'
function Home() {
const user = useSelector(state => state.auth.user)
@ -11,6 +12,10 @@ function Home() {
const [color, setColor] = useState('White')
const [type, setType] = useState(1)
useEffect(() => {
getVehicles();
}, []);
const [vehicleData, setVehicleData] = useState(null)
function signOut() {
signoutRedirect()
@ -78,7 +83,7 @@ function Home() {
{
vehicleData ?
<ul>
{ vehicleData.map(v => (<li>{v.color} {v.model} ({v.licensePlate}) - {getType(v.type)}</li>)) }
{ vehicleData.map(v => (<li key={v.id}>{v.color} {v.model} ({v.licensePlate}) - {getType(v.type)}</li>)) }
</ul>
:
<p>No vehicles yet :(</p>

View File

@ -1,13 +1,13 @@
import axios from 'axios'
async function getVehiclesFromApi(access_token) {
const response = await axios.get(`https://localhost:5001/api/Vehicles`, { headers: { 'Authorization': `Bearer ${access_token}` } });
const response = await axios.get(`https://localhost:6001/api/Vehicles`, { headers: { 'Authorization': `Bearer ${access_token}` } });
return response.data;
}
async function registerVehicle(vehicle, access_token) {
console.log(vehicle);
const response = await axios.post(`https://localhost:5001/api/Vehicles`, vehicle, { headers: { 'Authorization': `Bearer ${access_token}` } });
const response = await axios.post(`https://localhost:6001/api/Vehicles`, vehicle, { headers: { 'Authorization': `Bearer ${access_token}` } });
return response.data;
}