// By JCH, 20211121
// Using the ApolloClient Hook to create our own custom Hook

import { gql, useQuery } from "@apollo/client";

const getOrdersAPI = (orderID) => gql`
    query getOrderInfo {
        getOrderInfo(orderID:${orderID}) {
            orderID
            items {
              productName
              price
            }
            totalAmount
            orderDate
            email
            orderByFirstName
            orderByLastName
          }
    }
`;


// custom hook for query
const useGetOrders = (orderID) => {
  // I omitted the error in the return
  const { loading, data } = useQuery(getOrdersAPI(orderID));
  return loading ? null : data.getOrderInfo;
};

export { useGetOrders };
