From 88dcc569951b12bbd5587b2f54a9cf27701aaac5 Mon Sep 17 00:00:00 2001 From: Alessandro Casazza Date: Wed, 29 Sep 2021 12:33:30 +0200 Subject: [PATCH] fix: Add mutate instead of revalidate --- framework/commercelayer/auth/use-login.tsx | 29 ++++++++++--------- framework/commercelayer/auth/use-logout.tsx | 26 ++++++++++------- framework/commercelayer/auth/use-signup.tsx | 10 ++++--- .../commercelayer/customer/use-customer.tsx | 11 ++++--- 4 files changed, 43 insertions(+), 33 deletions(-) diff --git a/framework/commercelayer/auth/use-login.tsx b/framework/commercelayer/auth/use-login.tsx index bcea24225..2f0b0db51 100644 --- a/framework/commercelayer/auth/use-login.tsx +++ b/framework/commercelayer/auth/use-login.tsx @@ -30,7 +30,9 @@ export const handler: MutationHook = { { 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 = { } }, 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] + ) + }, } diff --git a/framework/commercelayer/auth/use-logout.tsx b/framework/commercelayer/auth/use-logout.tsx index fd553ecd5..1aeca85aa 100644 --- a/framework/commercelayer/auth/use-logout.tsx +++ b/framework/commercelayer/auth/use-logout.tsx @@ -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 @@ -12,15 +13,18 @@ export const handler: MutationHook = { 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] + ) + }, } diff --git a/framework/commercelayer/auth/use-signup.tsx b/framework/commercelayer/auth/use-signup.tsx index 66a9eae20..6ef662f0e 100644 --- a/framework/commercelayer/auth/use-signup.tsx +++ b/framework/commercelayer/auth/use-signup.tsx @@ -40,7 +40,9 @@ export const handler: MutationHook = { { 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 = { 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] ) }, } diff --git a/framework/commercelayer/customer/use-customer.tsx b/framework/commercelayer/customer/use-customer.tsx index b1feb76a3..14663c40a 100644 --- a/framework/commercelayer/customer/use-customer.tsx +++ b/framework/commercelayer/customer/use-customer.tsx @@ -6,15 +6,18 @@ import { ENDPOINT } from '../const' export default useCustomer as UseCustomer -const customerId = Cookies.get('CL_CUSTOMER_ID') - export const handler: SWRHook = { 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 ? ({