Files
commerce/src/components/hooks/account/useEditUserInfo.tsx
2021-10-07 11:01:41 +07:00

52 lines
1.2 KiB
TypeScript

import { useState } from 'react'
import { Customer } from '@framework/schema'
import fetcher from 'src/utils/fetcher'
import { updateCustomer } from '@framework/utils/mutations/update-customer-mutation'
import { useActiveCustomer } from '../auth'
interface Props {
firstName?: string;
lastName?: string,
phoneNumber?:string,
}
const useEditUserInfo = () => {
const [loading, setLoading] = useState(false)
const [error, setError] = useState<Error | null>(null)
const {mutate} = useActiveCustomer();
const editUserInfo = (
{ firstName,lastName,phoneNumber}: Props,
fCallBack: (isSuccess: boolean, message?: string) => void
) => {
setError(null)
setLoading(true)
fetcher<Customer>({
query: updateCustomer,
variables: {
input: {
firstName,
lastName,
phoneNumber
},
},
})
.then((data) => {
if (data.updateCustomer.__typename == 'Customer') {
mutate();
return data
}
})
.catch((error) => {
setError(error)
fCallBack(false, error.message)
})
.finally(() => setLoading(false))
}
return { loading, editUserInfo, error }
}
export default useEditUserInfo