mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
fix: Add mutate instead of revalidate
This commit is contained in:
parent
1cb1824af1
commit
88dcc56995
@ -30,7 +30,9 @@ export const handler: MutationHook<any> = {
|
||||
{ username: email, password: password }
|
||||
)
|
||||
token &&
|
||||
setCookie('CL_CUSTOMER_TOKEN', token.accessToken, { expires: token.expires })
|
||||
setCookie('CL_CUSTOMER_TOKEN', token.accessToken, {
|
||||
expires: token.expires,
|
||||
})
|
||||
Cookies.set('CL_CUSTOMER_ID', token.data.owner_id as string)
|
||||
alert(`User "${email}" has successfully been logged in.`)
|
||||
return token
|
||||
@ -41,17 +43,16 @@ export const handler: MutationHook<any> = {
|
||||
}
|
||||
},
|
||||
useHook:
|
||||
({ fetch }) =>
|
||||
() => {
|
||||
const { revalidate } = useCustomer()
|
||||
|
||||
return useCallback(
|
||||
async function login(input) {
|
||||
const data = await fetch({ input })
|
||||
await revalidate()
|
||||
return data
|
||||
},
|
||||
[fetch, revalidate]
|
||||
)
|
||||
},
|
||||
({ fetch }) =>
|
||||
() => {
|
||||
const { mutate } = useCustomer()
|
||||
return useCallback(
|
||||
async function login(input) {
|
||||
const data = await fetch({ input })
|
||||
await mutate()
|
||||
return data
|
||||
},
|
||||
[fetch]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ import { useCallback } from 'react'
|
||||
import { MutationHook } from '@commerce/utils/types'
|
||||
import useLogout, { UseLogout } from '@commerce/auth/use-logout'
|
||||
import Cookies from 'js-cookie'
|
||||
import { useCustomer } from '@framework/customer'
|
||||
|
||||
export default useLogout as UseLogout<typeof handler>
|
||||
|
||||
@ -12,15 +13,18 @@ export const handler: MutationHook<any> = {
|
||||
async fetcher() {
|
||||
return null
|
||||
},
|
||||
useHook: ({ fetch }) => () => {
|
||||
|
||||
return useCallback(
|
||||
async function logout() {
|
||||
Cookies.remove('CL_CUSTOMER_ID')
|
||||
Cookies.remove('CL_CUSTOMER_TOKEN')
|
||||
alert("Logout successful!")
|
||||
},
|
||||
[fetch]
|
||||
)
|
||||
},
|
||||
useHook:
|
||||
({ fetch }) =>
|
||||
() => {
|
||||
const { mutate } = useCustomer()
|
||||
return useCallback(
|
||||
async function logout() {
|
||||
Cookies.remove('CL_CUSTOMER_ID')
|
||||
Cookies.remove('CL_CUSTOMER_TOKEN')
|
||||
mutate()
|
||||
alert('Logout successful!')
|
||||
},
|
||||
[fetch]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@ -40,7 +40,9 @@ export const handler: MutationHook<any> = {
|
||||
{ username: email, password: password }
|
||||
)
|
||||
token &&
|
||||
setCookie('CL_CUSTOMER_TOKEN', token.accessToken, { expires: token.expires })
|
||||
setCookie('CL_CUSTOMER_TOKEN', token.accessToken, {
|
||||
expires: token.expires,
|
||||
})
|
||||
Cookies.set('CL_CUSTOMER_ID', data.id)
|
||||
alert(`User "${email}" has successfully been created.`)
|
||||
return data
|
||||
@ -53,15 +55,15 @@ export const handler: MutationHook<any> = {
|
||||
useHook:
|
||||
({ fetch }) =>
|
||||
() => {
|
||||
const { revalidate } = useCustomer()
|
||||
const { mutate } = useCustomer()
|
||||
|
||||
return useCallback(
|
||||
async function signup(input) {
|
||||
const data = await fetch({ input })
|
||||
await revalidate()
|
||||
await mutate()
|
||||
return data
|
||||
},
|
||||
[fetch, revalidate]
|
||||
[fetch]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@ -6,15 +6,18 @@ import { ENDPOINT } from '../const'
|
||||
|
||||
export default useCustomer as UseCustomer<typeof handler>
|
||||
|
||||
const customerId = Cookies.get('CL_CUSTOMER_ID')
|
||||
|
||||
export const handler: SWRHook<CustomerHook> = {
|
||||
fetchOptions: {
|
||||
url: `${ENDPOINT}/api/customers/${customerId}`,
|
||||
url: `${ENDPOINT}/api/customers/`,
|
||||
method: 'GET',
|
||||
},
|
||||
async fetcher({ options, fetch }) {
|
||||
const data: any = customerId ? await fetch({ ...options }) : null
|
||||
let data: any = null
|
||||
const customerId = Cookies.get('CL_CUSTOMER_ID')
|
||||
if (customerId) {
|
||||
options.url = `${options.url}/${customerId}`
|
||||
data = await fetch({ ...options })
|
||||
}
|
||||
|
||||
return data
|
||||
? ({
|
||||
|
Loading…
x
Reference in New Issue
Block a user