Prevent booking if already booked

This post is part of a new series where we build a clone of Airbnb with Next.js. See the first post here.

Now there’s a problem if we try to book some dates that are already booked.

We need to make good use of our /houses/check endpoint to check if in the dates we want to book, some are already booked!

We do this by adding this canReserve() function to pages/houses/[id].js:

pages/houses/[id].js

const canReserve = async (houseId, startDate, endDate) => {
  try {
    const response = await axios.post(
      'http://localhost:3000/api/houses/check',
      { houseId, startDate, endDate }
    )
    if (response.data.status === 'error') {
      alert(response.data.message)
      return
    }

    if (response.data.message === 'busy') return false
    return true
  } catch (error) {
    console.error(error)
    return
  }
}

This function is calling the server-side /api/houses/check endpoint we created previously, and if we get ‘busy’ as a message in the response, we return false.

We can use this boolean return value to disallow going on with the booking, in the “Reserve” button onClick callback function we already wrote.

That function now is:

pages/houses/[id].js

<button
  className="reserve"
  onClick={async () => {
    try {
      const response = await axios.post('/api/reserve', {
        houseId: house.id,
        startDate,
        endDate
      })
      if (response.data.status === 'error') {
        alert(response.data.message)
        return
      }
      console.log(response.data)
    } catch (error) {
      console.log(error)
      return
    }
  }}
>
  Reserve
</button>

We do it as the first thing:

pages/houses/[id].js

<button
  className='reserve'
  onClick={async () => {
	if (!(await canReserve(house.id, startDate, endDate))) {
	  alert('The dates chosen are not valid')
	  return
	}

	try {...

See the code on GitHub


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This post is part of a new series where we build a clone of Airbnb with Next.js. See the first post here.

Now there’s a problem if we try to book some dates that are already booked.

We need to make good use of our /houses/check endpoint to check if in the dates we want to book, some are already booked!

We do this by adding this canReserve() function to pages/houses/[id].js:

pages/houses/[id].js

const canReserve = async (houseId, startDate, endDate) => {
  try {
    const response = await axios.post(
      'http://localhost:3000/api/houses/check',
      { houseId, startDate, endDate }
    )
    if (response.data.status === 'error') {
      alert(response.data.message)
      return
    }

    if (response.data.message === 'busy') return false
    return true
  } catch (error) {
    console.error(error)
    return
  }
}

This function is calling the server-side /api/houses/check endpoint we created previously, and if we get ‘busy’ as a message in the response, we return false.

We can use this boolean return value to disallow going on with the booking, in the “Reserve” button onClick callback function we already wrote.

That function now is:

pages/houses/[id].js

<button
  className="reserve"
  onClick={async () => {
    try {
      const response = await axios.post('/api/reserve', {
        houseId: house.id,
        startDate,
        endDate
      })
      if (response.data.status === 'error') {
        alert(response.data.message)
        return
      }
      console.log(response.data)
    } catch (error) {
      console.log(error)
      return
    }
  }}
>
  Reserve
</button>

We do it as the first thing:

pages/houses/[id].js

<button
  className='reserve'
  onClick={async () => {
	if (!(await canReserve(house.id, startDate, endDate))) {
	  alert('The dates chosen are not valid')
	  return
	}

	try {...

See the code on GitHub


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-12-26T05:00:00+00:00) Prevent booking if already booked. Retrieved from https://www.scien.cx/2021/12/26/prevent-booking-if-already-booked/

MLA
" » Prevent booking if already booked." flaviocopes.com | Sciencx - Sunday December 26, 2021, https://www.scien.cx/2021/12/26/prevent-booking-if-already-booked/
HARVARD
flaviocopes.com | Sciencx Sunday December 26, 2021 » Prevent booking if already booked., viewed ,<https://www.scien.cx/2021/12/26/prevent-booking-if-already-booked/>
VANCOUVER
flaviocopes.com | Sciencx - » Prevent booking if already booked. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/26/prevent-booking-if-already-booked/
CHICAGO
" » Prevent booking if already booked." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/12/26/prevent-booking-if-already-booked/
IEEE
" » Prevent booking if already booked." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/12/26/prevent-booking-if-already-booked/. [Accessed: ]
rf:citation
» Prevent booking if already booked | flaviocopes.com | Sciencx | https://www.scien.cx/2021/12/26/prevent-booking-if-already-booked/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.