Customer Account Initial Commit

This commit is contained in:
SushantJadhav 2021-08-26 15:53:34 +05:30
parent 07b22f5d4d
commit d906a0b3ac
9 changed files with 139 additions and 11 deletions

View File

@ -0,0 +1,62 @@
import type { GetLoggedInCustomerQuery } from '../../../schema'
import type { CustomerEndpoint } from '.'
import { getCustomerAccountQuery } from '../../queries/get-customer-account-query'
// export const getLoggedInCustomerQuery = /* GraphQL */ `
// query getLoggedInCustomer {
// customer {
// entityId
// firstName
// lastName
// email
// company
// customerGroupId
// notes
// phone
// addressCount
// attributeCount
// storeCredit {
// value
// currencyCode
// }
// }
// }
// `
export type Customer = NonNullable<GetLoggedInCustomerQuery['customer']>
const getLoggedInCustomer: CustomerEndpoint['handlers']['getLoggedInCustomer'] = async ({
req,
res,
config,
}) => {
const token = req.cookies[config.customerCookie]
const { accessToken } = JSON.parse(token);
if (accessToken) {
const { data } = await config.fetch(
getCustomerAccountQuery,
undefined,
{
headers: {
'x-vol-user-claims': accessToken
},
}
)
const customer = data?.customerAccount;
if (!customer) {
return res.status(400).json({
data: null,
errors: [{ message: 'Customer not found', code: 'not_found' }],
})
}
return res.status(200).json({ data: { customer } })
}
res.status(200).json({ data: null })
}
export default getLoggedInCustomer

View File

@ -1 +1,18 @@
export default function noopApi(...args: any[]): void {} import { GetAPISchema, createEndpoint } from '@commerce/api'
import customerEndpoint from '@commerce/api/endpoints/customer'
import type { CustomerSchema } from '../../../types/customer'
import type { KiboCommerceAPI } from '../..'
import getLoggedInCustomer from './customer'
export type CustomerAPI = GetAPISchema<KiboCommerceAPI, CustomerSchema>
export type CustomerEndpoint = CustomerAPI['endpoint']
export const handlers: CustomerEndpoint['handlers'] = { getLoggedInCustomer }
const customerApi = createEndpoint<CustomerAPI>({
handler: customerEndpoint,
handlers,
})
export default customerApi

View File

@ -0,0 +1,12 @@
export const getCustomerAccountQuery = `
query getUser {
customerAccount:getCurrentAccount {
id
firstName
lastName
emailAddress
userName
isAnonymous
}
}
`

View File

@ -31,7 +31,7 @@ export const handler: MutationHook<LoginHook> = {
return useCallback( return useCallback(
async function login(input) { async function login(input) {
const data = await fetch({ input }) const data = await fetch({ input })
// await revalidate() await revalidate()
return data return data
}, },
[fetch, revalidate] [fetch, revalidate]

View File

@ -35,7 +35,7 @@ export const handler: MutationHook<SignupHook> = {
return useCallback( return useCallback(
async function signup(input) { async function signup(input) {
const data = await fetch({ input }) const data = await fetch({ input })
// await revalidate() await revalidate()
return data return data
}, },
[fetch, revalidate] [fetch, revalidate]

View File

@ -1,15 +1,24 @@
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
import type { CustomerHook } from '../types/customer'
export default useCustomer as UseCustomer<typeof handler> export default useCustomer as UseCustomer<typeof handler>
export const handler: SWRHook<any> = {
export const handler: SWRHook<CustomerHook> = {
fetchOptions: { fetchOptions: {
query: '', url: '/api/customer',
method: 'GET',
}, },
async fetcher({ input, options, fetch }) {}, async fetcher({ options, fetch }) {
useHook: () => () => { const data = await fetch(options)
return async function addItem() { return data?.customer ?? null
return {} },
} useHook: ({ useData }) => (input) => {
return useData({
swrOptions: {
revalidateOnFocus: false,
...input?.swrOptions,
},
})
}, },
} }

View File

@ -11402,3 +11402,25 @@ export type LoginMutationVariables = Exact<{
email: Scalars['String'] email: Scalars['String']
password: Scalars['String'] password: Scalars['String']
}> }>
export type GetLoggedInCustomerQuery = { __typename?: 'Query' } & {
customer?: Maybe<
{ __typename?: 'Customer' } & Pick<
Customer,
| 'entityId'
| 'firstName'
| 'lastName'
| 'email'
| 'company'
| 'customerGroupId'
| 'notes'
| 'phone'
| 'addressCount'
| 'attributeCount'
> & {
storeCredit: Array<
{ __typename?: 'Money' } & Pick<Money, 'value' | 'currencyCode'>
>
}
>
}

View File

@ -0,0 +1,5 @@
import * as Core from '@commerce/types/customer'
export * from '@commerce/types/customer'
export type CustomerSchema = Core.CustomerSchema

View File

@ -0,0 +1 @@
export * from '@commerce/types/logout'