{"size": 865, "ext": "js", "max_stars_repo_path": "shop/src/utils/useOrder.js", "max_stars_repo_name": "teameclipsestaff/dshop", "max_stars_repo_stars_event_min_datetime": "2020-05-15T18:29:44.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-05T20:05:40.000Z", "max_issues_repo_path": "shop/src/utils/useOrder.js", "max_issues_repo_name": "teameclipsestaff/dshop", "max_issues_repo_issues_event_min_datetime": "2020-05-07T23:37:24.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T06:13:25.000Z", "max_forks_repo_path": "shop/src/utils/useOrder.js", "max_forks_repo_name": "teameclipsestaff/dshop", "max_forks_repo_forks_event_min_datetime": "2020-06-12T21:33:12.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-26T01:27:48.000Z"}
{"max_stars_count": 139, "max_issues_count": 564, "max_forks_count": 75, "avg_line_length": 24.7142857143, "max_line_length": 64, "alphanum_fraction": 0.6046242775}
import { useState, useEffect } from 'react'

import useOrigin from 'utils/useOrigin'
import { useStateValue } from 'data/state'

function useOrder({ tx, password }) {
  const { getOffer, status } = useOrigin()
  const [cart, setCart] = useState()
  const [error, setError] = useState()
  const [loading, setLoading] = useState()
  const [, dispatch] = useStateValue()

  useEffect(() => {
    async function go() {
      const result = await getOffer({ tx, password })
      if (result) {
        setCart(result.cart)
        setError(false)
        dispatch({ type: 'orderComplete' })
      } else {
        setError(true)
      }
      setLoading(false)
    }
    if (getOffer && !cart && !loading && status !== 'loading') {
      setLoading(true)
      go()
    }
  }, [tx, password, status])

  return { order: cart, loading, error }
}

export default useOrder
