fix: Add mutate instead of revalidate

This commit is contained in:
Alessandro Casazza 2021-09-29 12:33:30 +02:00
parent 1cb1824af1
commit 88dcc56995
No known key found for this signature in database
GPG Key ID: 3AF41B06C6495D3D
4 changed files with 43 additions and 33 deletions

View File

@ -30,7 +30,9 @@ export const handler: MutationHook<any> = {
{ username: email, password: password } { username: email, password: password }
) )
token && 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) Cookies.set('CL_CUSTOMER_ID', token.data.owner_id as string)
alert(`User "${email}" has successfully been logged in.`) alert(`User "${email}" has successfully been logged in.`)
return token return token
@ -43,15 +45,14 @@ export const handler: MutationHook<any> = {
useHook: useHook:
({ fetch }) => ({ fetch }) =>
() => { () => {
const { revalidate } = useCustomer() const { mutate } = useCustomer()
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 mutate()
return data return data
}, },
[fetch, revalidate] [fetch]
) )
}, },
} }

View File

@ -2,6 +2,7 @@ import { useCallback } from 'react'
import { MutationHook } from '@commerce/utils/types' import { MutationHook } from '@commerce/utils/types'
import useLogout, { UseLogout } from '@commerce/auth/use-logout' import useLogout, { UseLogout } from '@commerce/auth/use-logout'
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
import { useCustomer } from '@framework/customer'
export default useLogout as UseLogout<typeof handler> export default useLogout as UseLogout<typeof handler>
@ -12,13 +13,16 @@ export const handler: MutationHook<any> = {
async fetcher() { async fetcher() {
return null return null
}, },
useHook: ({ fetch }) => () => { useHook:
({ fetch }) =>
() => {
const { mutate } = useCustomer()
return useCallback( return useCallback(
async function logout() { async function logout() {
Cookies.remove('CL_CUSTOMER_ID') Cookies.remove('CL_CUSTOMER_ID')
Cookies.remove('CL_CUSTOMER_TOKEN') Cookies.remove('CL_CUSTOMER_TOKEN')
alert("Logout successful!") mutate()
alert('Logout successful!')
}, },
[fetch] [fetch]
) )

View File

@ -40,7 +40,9 @@ export const handler: MutationHook<any> = {
{ username: email, password: password } { username: email, password: password }
) )
token && 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) Cookies.set('CL_CUSTOMER_ID', data.id)
alert(`User "${email}" has successfully been created.`) alert(`User "${email}" has successfully been created.`)
return data return data
@ -53,15 +55,15 @@ export const handler: MutationHook<any> = {
useHook: useHook:
({ fetch }) => ({ fetch }) =>
() => { () => {
const { revalidate } = useCustomer() const { mutate } = useCustomer()
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 mutate()
return data return data
}, },
[fetch, revalidate] [fetch]
) )
}, },
} }

View File

@ -6,15 +6,18 @@ import { ENDPOINT } from '../const'
export default useCustomer as UseCustomer<typeof handler> export default useCustomer as UseCustomer<typeof handler>
const customerId = Cookies.get('CL_CUSTOMER_ID')
export const handler: SWRHook<CustomerHook> = { export const handler: SWRHook<CustomerHook> = {
fetchOptions: { fetchOptions: {
url: `${ENDPOINT}/api/customers/${customerId}`, url: `${ENDPOINT}/api/customers/`,
method: 'GET', method: 'GET',
}, },
async fetcher({ options, fetch }) { 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 return data
? ({ ? ({