Reservations
Queries and Mutations listed here are used to send requests to the Admin Reservation API Routes. To use these hooks, make sure to install the @medusajs/inventory module in your Medusa backend.
All hooks listed require user authentication.
Reservations, provided by the Inventory Module, are quantities of an item that are reserved, typically when an order is placed but not yet fulfilled. Reservations can be associated with any resources, but commonly with line items of an order.
Related Guide: How to manage item allocations in orders.
Mutations
useAdminCreateReservation
This hook creates a reservation which can be associated with any resource, such as an order's line item.
Example
import React from "react"
import { useAdminCreateReservation } from "medusa-react"
const CreateReservation = () => {
const createReservation = useAdminCreateReservation()
// ...
const handleCreate = (
locationId: string,
inventoryItemId: string,
quantity: number
) => {
createReservation.mutate({
location_id: locationId,
inventory_item_id: inventoryItemId,
quantity,
}, {
onSuccess: ({ reservation }) => {
console.log(reservation.id)
}
})
}
// ...
}
export default CreateReservation
Mutation Function Parameters
The details of the reservation to create.
Mutation Function Returned Data
The reservation's details.
useAdminUpdateReservation
This hook updates a reservation's details.
Example
import React from "react"
import { useAdminUpdateReservation } from "medusa-react"
type Props = {
reservationId: string
}
const Reservation = ({ reservationId }: Props) => {
const updateReservation = useAdminUpdateReservation(
reservationId
)
// ...
const handleUpdate = (
quantity: number
) => {
updateReservation.mutate({
quantity,
})
}
// ...
}
export default Reservation
Hook Parameters
id
stringRequiredMutation Function Parameters
The details to update of the reservation.
Mutation Function Returned Data
The reservation's details.
useAdminDeleteReservation
This hook deletes a reservation. Associated resources, such as the line item, will not be deleted.
Example
import React from "react"
import { useAdminDeleteReservation } from "medusa-react"
type Props = {
reservationId: string
}
const Reservation = ({ reservationId }: Props) => {
const deleteReservation = useAdminDeleteReservation(
reservationId
)
// ...
const handleDelete = () => {
deleteReservation.mutate(void 0, {
onSuccess: ({ id, object, deleted }) => {
console.log(id)
}
})
}
// ...
}
export default Reservation
Hook Parameters
id
stringRequiredMutation Function Returned Data
The response returned for a
DELETE
request.
DELETE
request.Queries
useAdminReservations
This hook retrieves a list of reservations. The reservations can be filtered by fields such as location_id
or quantity
passed in the query
parameter. The reservations can also be paginated.
Example
To list reservations:
import React from "react"
import { useAdminReservations } from "medusa-react"
const Reservations = () => {
const { reservations, isLoading } = useAdminReservations()
return (
<div>
{isLoading && <span>Loading...</span>}
{reservations && !reservations.length && (
<span>No Reservations</span>
)}
{reservations && reservations.length > 0 && (
<ul>
{reservations.map((reservation) => (
<li key={reservation.id}>{reservation.quantity}</li>
))}
</ul>
)}
</div>
)
}
export default Reservations
To specify relations that should be retrieved within the reservations:
import React from "react"
import { useAdminReservations } from "medusa-react"
const Reservations = () => {
const {
reservations,
isLoading
} = useAdminReservations({
expand: "location"
})
return (
<div>
{isLoading && <span>Loading...</span>}
{reservations && !reservations.length && (
<span>No Reservations</span>
)}
{reservations && reservations.length > 0 && (
<ul>
{reservations.map((reservation) => (
<li key={reservation.id}>{reservation.quantity}</li>
))}
</ul>
)}
</div>
)
}
export default Reservations
By default, only the first 20
records are retrieved. You can control pagination by specifying the limit
and offset
properties:
import React from "react"
import { useAdminReservations } from "medusa-react"
const Reservations = () => {
const {
reservations,
limit,
offset,
isLoading
} = useAdminReservations({
expand: "location",
limit: 10,
offset: 0
})
return (
<div>
{isLoading && <span>Loading...</span>}
{reservations && !reservations.length && (
<span>No Reservations</span>
)}
{reservations && reservations.length > 0 && (
<ul>
{reservations.map((reservation) => (
<li key={reservation.id}>{reservation.quantity}</li>
))}
</ul>
)}
</div>
)
}
export default Reservations
Hook Parameters
Filters and pagination parameters to apply on the retrieved reservations.
Query Returned Data
limit
numberRequiredoffset
numberRequiredcount
numberRequiredAn array of reservations details.
useAdminReservation
This hook retrieves a reservation's details.
Example
import React from "react"
import { useAdminReservation } from "medusa-react"
type Props = {
reservationId: string
}
const Reservation = ({ reservationId }: Props) => {
const { reservation, isLoading } = useAdminReservation(
reservationId
)
return (
<div>
{isLoading && <span>Loading...</span>}
{reservation && (
<span>{reservation.inventory_item_id}</span>
)}
</div>
)
}
export default Reservation
Hook Parameters
id
stringRequired