forked from crowetic/commerce
Hook fixes and update the customer after an operation
This commit is contained in:
parent
89e7793d10
commit
dca8f281ab
@ -43,8 +43,6 @@ const getLoggedInCustomer: CustomersHandlers['getLoggedInCustomer'] = async ({
|
|||||||
)
|
)
|
||||||
const { customer } = data
|
const { customer } = data
|
||||||
|
|
||||||
console.log('CUSTOMER', customer)
|
|
||||||
|
|
||||||
if (!customer) {
|
if (!customer) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
data: null,
|
data: null,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { HookFetcher } from '@lib/commerce/utils/types'
|
import type { HookFetcher } from '@lib/commerce/utils/types'
|
||||||
import type { SwrOptions } from '@lib/commerce/utils/use-data'
|
import type { SwrOptions } from '@lib/commerce/utils/use-data'
|
||||||
import useCommerceCustomer from '@lib/commerce/use-customer'
|
import useCommerceCustomer from '@lib/commerce/use-customer'
|
||||||
import type { Customer, CustomerData } from './api/customers'
|
import type { Customer, CustomerData } from './api/customers'
|
||||||
@ -15,8 +15,8 @@ export const fetcher: HookFetcher<Customer | null> = async (
|
|||||||
_,
|
_,
|
||||||
fetch
|
fetch
|
||||||
) => {
|
) => {
|
||||||
const data = await fetch<CustomerData>({ ...defaultOpts, ...options })
|
const data = await fetch<CustomerData | null>({ ...defaultOpts, ...options })
|
||||||
return data.customer
|
return data?.customer ?? null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extendHook(
|
export function extendHook(
|
||||||
|
@ -3,6 +3,7 @@ import type { HookFetcher } from '@lib/commerce/utils/types'
|
|||||||
import { CommerceError } from '@lib/commerce/utils/errors'
|
import { CommerceError } from '@lib/commerce/utils/errors'
|
||||||
import useCommerceLogin from '@lib/commerce/use-login'
|
import useCommerceLogin from '@lib/commerce/use-login'
|
||||||
import type { LoginBody } from './api/customers/login'
|
import type { LoginBody } from './api/customers/login'
|
||||||
|
import useCustomer from './use-customer'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/customers/login',
|
url: '/api/bigcommerce/customers/login',
|
||||||
@ -32,11 +33,13 @@ export const fetcher: HookFetcher<null, LoginBody> = (
|
|||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
const useLogin = () => {
|
const useLogin = () => {
|
||||||
|
const { revalidate } = useCustomer()
|
||||||
const fn = useCommerceLogin<null, LoginInput>(defaultOpts, customFetcher)
|
const fn = useCommerceLogin<null, LoginInput>(defaultOpts, customFetcher)
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function login(input: LoginInput) {
|
async function login(input: LoginInput) {
|
||||||
const data = await fn(input)
|
const data = await fn(input)
|
||||||
|
await revalidate()
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
[fn]
|
[fn]
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import type { HookFetcher } from '@lib/commerce/utils/types'
|
import type { HookFetcher } from '@lib/commerce/utils/types'
|
||||||
import useCommerceLogout from '@lib/commerce/use-logout'
|
import useCommerceLogout from '@lib/commerce/use-logout'
|
||||||
|
import useCustomer from './use-customer'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/customers/logout',
|
url: '/api/bigcommerce/customers/logout',
|
||||||
@ -16,11 +17,13 @@ export const fetcher: HookFetcher<null> = (options, _, fetch) => {
|
|||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
const useLogout = () => {
|
const useLogout = () => {
|
||||||
|
const { mutate } = useCustomer()
|
||||||
const fn = useCommerceLogout<null>(defaultOpts, customFetcher)
|
const fn = useCommerceLogout<null>(defaultOpts, customFetcher)
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function login() {
|
async function login() {
|
||||||
const data = await fn(null)
|
const data = await fn(null)
|
||||||
|
await mutate(null, false)
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
[fn]
|
[fn]
|
||||||
|
@ -3,6 +3,7 @@ import type { HookFetcher } from '@lib/commerce/utils/types'
|
|||||||
import { CommerceError } from '@lib/commerce/utils/errors'
|
import { CommerceError } from '@lib/commerce/utils/errors'
|
||||||
import useCommerceSignup from '@lib/commerce/use-signup'
|
import useCommerceSignup from '@lib/commerce/use-signup'
|
||||||
import type { SignupBody } from './api/customers/signup'
|
import type { SignupBody } from './api/customers/signup'
|
||||||
|
import useCustomer from './use-customer'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/customers/signup',
|
url: '/api/bigcommerce/customers/signup',
|
||||||
@ -32,11 +33,13 @@ export const fetcher: HookFetcher<null, SignupBody> = (
|
|||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
const useSignup = () => {
|
const useSignup = () => {
|
||||||
|
const { revalidate } = useCustomer()
|
||||||
const fn = useCommerceSignup<null, SignupInput>(defaultOpts, customFetcher)
|
const fn = useCommerceSignup<null, SignupInput>(defaultOpts, customFetcher)
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function signup(input: SignupInput) {
|
async function signup(input: SignupInput) {
|
||||||
const data = await fn(input)
|
const data = await fn(input)
|
||||||
|
await revalidate()
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
[fn]
|
[fn]
|
||||||
|
@ -18,21 +18,30 @@ export type UseData = <Result = any, Input = null>(
|
|||||||
|
|
||||||
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
||||||
const { fetcherRef } = useCommerce()
|
const { fetcherRef } = useCommerce()
|
||||||
const fetcher = (
|
const fetcher = async (
|
||||||
url?: string,
|
url?: string,
|
||||||
query?: string,
|
query?: string,
|
||||||
method?: string,
|
method?: string,
|
||||||
...args: any[]
|
...args: any[]
|
||||||
) => {
|
) => {
|
||||||
return fetcherFn(
|
try {
|
||||||
{ url, query, method },
|
return await fetcherFn(
|
||||||
// Transform the input array into an object
|
{ url, query, method },
|
||||||
args.reduce((obj, val, i) => {
|
// Transform the input array into an object
|
||||||
obj[input[i][0]!] = val
|
args.reduce((obj, val, i) => {
|
||||||
return obj
|
obj[input[i][0]!] = val
|
||||||
}, {}),
|
return obj
|
||||||
fetcherRef.current
|
}, {}),
|
||||||
)
|
fetcherRef.current
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
// SWR will not log errors, but any error that's not an instance
|
||||||
|
// of CommerceError is not welcomed by this hook
|
||||||
|
if (!(error instanceof CommerceError)) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const response = useSWR(
|
const response = useSWR(
|
||||||
() => {
|
() => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user