feat: VerifyCustomerAccountMutation

:%s
This commit is contained in:
lytrankieio123
2021-09-28 17:50:55 +07:00
parent 68adb9026b
commit cdad66b56b
9 changed files with 89 additions and 61 deletions

View File

@@ -1,15 +1,17 @@
import React from "react";
import React from 'react'
import s from './LoadingCommon.module.scss'
const LoadingCommon = () => {
return (
<div className={s.wrapper}>
<div className={s.loadingCommon}>
</div>
<p className={s.text}>Loading...</p>
</div>
)
interface Props {
description?: string
}
export default LoadingCommon
const LoadingCommon = ({ description = 'Loading...' }: Props) => {
return (
<div className={s.wrapper}>
<div className={s.loadingCommon}></div>
<p className={s.text}>{description}</p>
</div>
)
}
export default LoadingCommon

View File

@@ -0,0 +1 @@
// here

View File

@@ -1,4 +1,4 @@
import { LoginMutation } from '@framework/schema'
import { VerifyCustomerAccountMutation } from '@framework/schema'
import { useState } from 'react'
import { CommonError } from 'src/domains/interfaces/CommonError'
import rawFetcher from 'src/utils/rawFetcher'
@@ -13,28 +13,40 @@ interface VerifyInput {
const useVerifyCustomer = () => {
const [loading, setLoading] = useState(false)
const [error, setError] = useState<CommonError | null>(null)
// const { mutate } = useActiveCustomer()
const { mutate } = useActiveCustomer()
const verify = (options: VerifyInput) => {
const verify = (
options: VerifyInput,
fCallBack?: (isSuccess: boolean) => void
) => {
setError(null)
setLoading(true)
rawFetcher<LoginMutation>({
rawFetcher<VerifyCustomerAccountMutation>({
query: VERIFY_CUSTOMER_ACCOUNT,
variables: options,
})
.then(({ data, headers }) => {
console.log("data: ", data)
// if (data.login.__typename !== 'CurrentUser') {
// throw CommonError.create(data.login.message, data.login.errorCode)
// }
// const authToken = headers.get('vendure-auth-token')
// if (authToken != null) {
// localStorage.setItem('token', authToken)
// return mutate()
// }
if (data.verifyCustomerAccount.__typename !== 'CurrentUser') {
throw CommonError.create(
data.verifyCustomerAccount.message,
data.verifyCustomerAccount.errorCode
)
}
fCallBack && fCallBack(true)
const authToken = headers.get('vendure-auth-token')
if (authToken != null) {
localStorage.setItem('token', authToken)
return mutate()
}
})
.catch((err) => {
setError(err)
fCallBack && fCallBack(false)
})
.finally(() => {
setLoading(false)
})
.catch(setError)
.finally(() => setLoading(false))
}
return { loading, verify, error }

View File

@@ -0,0 +1,2 @@
export { default as VerifyCustomerAccount } from './VerifyCustomerAccount/VerifyCustomerAccount'

View File

@@ -2,15 +2,19 @@ import { gql } from 'graphql-request'
export const VERIFY_CUSTOMER_ACCOUNT = gql`
mutation verifyCustomerAccount(token: String!, password: String) {
mutation verifyCustomerAccount($token: String!, $password: String) {
verifyCustomerAccount( token: $token, password: $password) {
__typename
...on CurrentUser {
id
identifier
}
...ErrorResult
... on ErrorResult {
errorCode
message
}
}
}
}
`

View File

@@ -10,11 +10,11 @@ interface QueryOptions {
const fetcher = async <T>(options: QueryOptions): Promise<T> => {
const { query, variables } = options
console.log('query')
console.log(options)
// console.log('query')
// console.log(options)
const token = localStorage.getItem('token')
console.log('token')
console.log(token)
// console.log('token')
// console.log(token)
const res = await request<T>(
process.env.NEXT_PUBLIC_VENDURE_SHOP_API_URL as string,
query,