mirror of
https://github.com/vercel/commerce.git
synced 2025-07-15 17:01:22 +00:00
create hooks
This commit is contained in:
parent
f92d823d1e
commit
fea72c36f5
@ -1,16 +1,49 @@
|
|||||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
import { MutationHook } from '@vercel/commerce/utils/types'
|
||||||
import useLogin, { UseLogin } from '@vercel/commerce/auth/use-login'
|
import useLogin, { UseLogin } from '@vercel/commerce/auth/use-login'
|
||||||
|
import { useCallback } from 'react'
|
||||||
|
import useCustomer from '@vercel/commerce/customer/use-customer'
|
||||||
|
import { setCustomerToken } from '../utils/token/customer-token'
|
||||||
|
import { setCustomerId } from '../utils/token/customer-id'
|
||||||
|
|
||||||
export default useLogin as UseLogin<typeof handler>
|
export default useLogin as UseLogin<typeof handler>
|
||||||
|
|
||||||
export const handler: MutationHook<any> = {
|
export const handler: MutationHook<any> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: '',
|
url: '/authentication-token',
|
||||||
|
method: 'POST',
|
||||||
},
|
},
|
||||||
async fetcher() {
|
fetcher: async ({ input: { email, password }, options, fetch }) => {
|
||||||
|
const authReturn = await fetch({
|
||||||
|
url: options.url,
|
||||||
|
method: options.method,
|
||||||
|
body: {
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
},
|
||||||
|
variables: {
|
||||||
|
useToken: false,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
setCustomerToken(authReturn.token)
|
||||||
|
console.log(authReturn)
|
||||||
|
const custumerRouteParts = authReturn.customer.split('/')
|
||||||
|
console.log(custumerRouteParts)
|
||||||
|
setCustomerId(custumerRouteParts[5])
|
||||||
return null
|
return null
|
||||||
},
|
},
|
||||||
useHook: () => () => {
|
useHook:
|
||||||
return async function () {}
|
({ fetch }) =>
|
||||||
|
() => {
|
||||||
|
const { mutate } = useCustomer()
|
||||||
|
|
||||||
|
return useCallback(
|
||||||
|
async function login(input) {
|
||||||
|
const data = await fetch({ input })
|
||||||
|
await mutate()
|
||||||
|
return data
|
||||||
|
},
|
||||||
|
[fetch, mutate]
|
||||||
|
)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,32 @@ import { SWRHook } from '@vercel/commerce/utils/types'
|
|||||||
import useCustomer, {
|
import useCustomer, {
|
||||||
UseCustomer,
|
UseCustomer,
|
||||||
} from '@vercel/commerce/customer/use-customer'
|
} from '@vercel/commerce/customer/use-customer'
|
||||||
|
import { getCustomerId } from '../utils/token/customer-id'
|
||||||
|
import { normalizeCustomer } from '../utils/normalize/normalize-customer'
|
||||||
|
|
||||||
export default useCustomer as UseCustomer<typeof handler>
|
export default useCustomer as UseCustomer<typeof handler>
|
||||||
|
|
||||||
export const handler: SWRHook<any> = {
|
export const handler: SWRHook<any> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: '',
|
url: `/customers/`,
|
||||||
|
method: 'GET',
|
||||||
},
|
},
|
||||||
async fetcher({ input, options, fetch }) {},
|
fetcher: async ({ options, fetch }) => {
|
||||||
useHook: () => () => {
|
const syliusCustomer = await fetch({
|
||||||
return async function addItem() {
|
url: options.url! + getCustomerId(),
|
||||||
return {}
|
method: options.method,
|
||||||
}
|
})
|
||||||
|
const customer = normalizeCustomer(syliusCustomer)
|
||||||
|
return customer
|
||||||
|
},
|
||||||
|
useHook:
|
||||||
|
({ useData }) =>
|
||||||
|
(input) => {
|
||||||
|
return useData({
|
||||||
|
swrOptions: {
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
...input?.swrOptions,
|
||||||
|
},
|
||||||
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
12
packages/sylius/src/utils/token/customer-token.ts
Normal file
12
packages/sylius/src/utils/token/customer-token.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { SYLIUS_CUSTOMER_TOKEN } from '../../const'
|
||||||
|
|
||||||
|
export const getCustomerToken = () =>
|
||||||
|
localStorage.getItem(SYLIUS_CUSTOMER_TOKEN)
|
||||||
|
|
||||||
|
export const setCustomerToken = (token: string | null) => {
|
||||||
|
if (!token) {
|
||||||
|
localStorage.removeItem(SYLIUS_CUSTOMER_TOKEN)
|
||||||
|
} else {
|
||||||
|
localStorage.setItem(SYLIUS_CUSTOMER_TOKEN, token)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user