Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Import  { useState, useEffect } from 'react';
import axios from 'axios';


const ConfirmBooking = () => {
  //track state
  const [data,setData] = useState([])

  const Style  =  {
   color: 'rgb(97, 113, 154)',
   padding: '5px'

  }

//GET data
 useEffect(() => {
  axios
    .get('http://localhost:5000/api/bookings')
    .then(res => {
      console.log(res)
      setData(res.data)
    })
    .catch(err => {
      console.log(err)
    })
 }, [])
  //DELETE data
 const deleteHandler =(id) =>{
    axios
       .delete(`http://localhost:5000/api/bookings/${id}`)
       .then(res => {
        console.log('deleted',res)
        setData(res.data)
       })
       .catch(error =>{
        console.log(error)
       })
    }

 if(!data?.length) return <div>loading...</div>

return (
  <div className='bookings'>
    <h4 style={Style}>Name:{" "}{data.at(-1).name}</h4> 
    <h4 style={Style} >Service:{" "}{data.at(-1).service}</h4>
    <h4 style={Style} >Date:{" "}{data.at(-1).date}</h4>
    <h4 style={Style} >Cost:{" "}{data.at(-1).cost}</h4><br></br>
    <button  >Edit</button>
    <button onClick={ () => deleteHandler(data.at(-1).id)} >Delete</button>
  </div>
 )
  
  }
  
export default ConfirmBooking;


What I have tried:

The above shows what I have tried, but it doesn't work. Please I need some help
Posted
Updated 28-Nov-22 8:05am

1 solution

Import  { useState, useEffect } from 'react';
import axios from 'axios';


const ConfirmBooking = () => {
  //track state
  const [data,setData] = useState([])

  const Style  =  {
   color: 'rgb(97, 113, 154)',
   padding: '5px'

  }

//GET data
 useEffect(() => {
  axios
    .get('http://localhost:5000/api/bookings')
    .then(res => {
      console.log(res)
      setData(res.data)
    })
    .catch(err => {
      console.log(err)
    })
 }, [])
  //DELETE data
 const deleteHandler =(id) =>{
    axios
       .delete(`http://localhost:5000/api/bookings/${id}`)
       .then(res => {
        console.log('deleted',res)
        setData(res.data)
       })
       .catch(error =>{
        console.log(error)
       })
    }

 if(!data?.length) return <div>loading...</div>

return (
  <div className='bookings'>
    <h4 style={Style}>Name:{" "}{data.at(-1).name}</h4> 
    <h4 style={Style} >Service:{" "}{data.at(-1).service}</h4>
    <h4 style={Style} >Date:{" "}{data.at(-1).date}</h4>
    <h4 style={Style} >Cost:{" "}{data.at(-1).cost}</h4><br></br>
    <button  >Edit</button>
    <button onClick={ () => deleteHandler(data.at(-1).id)} >Delete</button>
  </div>
 )
  
  }
  
export default ConfirmBooking;
 
Share this answer
 
Comments
Richard Deeming 29-Nov-22 4:15am    
In what way is an identical copy of the code from your question meant to be a "solution" to your question?

Delete this non-answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900