mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { CreateCustomerInput, SetCustomerForOrderMutation } from '@framework/schema'
|
|
import { setCustomerForOrderMutation } from '@framework/utils/mutations/set-customer-order-mutation'
|
|
import { useState } from 'react'
|
|
import { CommonError } from 'src/domains/interfaces/CommonError'
|
|
import rawFetcher from 'src/utils/rawFetcher'
|
|
import { useGetActiveOrder } from '../cart'
|
|
|
|
|
|
const useSetCustomerForOrder = () => {
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<CommonError | null>(null)
|
|
const { mutate } = useGetActiveOrder()
|
|
|
|
const setCustomerForOrder = (input: CreateCustomerInput,
|
|
fCallBack: (isSuccess: boolean, message?: string) => void
|
|
) => {
|
|
setError(null)
|
|
setLoading(true)
|
|
rawFetcher<SetCustomerForOrderMutation>({
|
|
query: setCustomerForOrderMutation,
|
|
variables: { input },
|
|
})
|
|
.then(({ data }) => {
|
|
if (data.setCustomerForOrder.__typename === 'ActiveOrderCustomerFragment') {
|
|
fCallBack(true)
|
|
mutate()
|
|
} else {
|
|
fCallBack(false, data.setCustomerForOrder.message)
|
|
}
|
|
|
|
})
|
|
.catch((error) => {
|
|
setError(error)
|
|
fCallBack(false, error.message)
|
|
})
|
|
.finally(() => setLoading(false))
|
|
}
|
|
|
|
return { loading, setCustomerForOrder, error }
|
|
}
|
|
|
|
export default useSetCustomerForOrder
|