From d0a8292187980dfa9ae6e0f863b3cbe22af39844 Mon Sep 17 00:00:00 2001 From: KacperKoza343 Date: Thu, 20 Jun 2024 14:38:57 +0200 Subject: [PATCH] refactor(app): refactor according to review --- .../api/servieces/worker/get-kyc-session-id.ts | 15 +++++++-------- .../drawer-menu-items-worker.tsx | 9 ++++----- .../layout/protected/drawer-navigation.tsx | 5 +++-- .../frontend/src/hooks/use-kyc-notification.tsx | 14 ++++++-------- .../src/pages/worker/profile/profile-actions.tsx | 4 ++-- .../apps/human-app/frontend/src/router/router.tsx | 6 ++++-- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/packages/apps/human-app/frontend/src/api/servieces/worker/get-kyc-session-id.ts b/packages/apps/human-app/frontend/src/api/servieces/worker/get-kyc-session-id.ts index 0758d4ffc8..ac9f0f79d5 100644 --- a/packages/apps/human-app/frontend/src/api/servieces/worker/get-kyc-session-id.ts +++ b/packages/apps/human-app/frontend/src/api/servieces/worker/get-kyc-session-id.ts @@ -21,14 +21,6 @@ type KycSessionIdMutationResult = | (KycSessionIdSuccessSchema & { error?: never }) | { session_id?: never; error: KycError }; -// 401 - unauthenticated also means that email not verified -// 400 - bad request also means that KYC already approved - -// normally if app receives 401 status code it tries to obtain -// access token with refresh token, kycSessionIdMutation has to -// implement its own flow to handle that case because 401 that -// can be revived for "kyc/start" doesn't mean that JWT token expired - export function useKycSessionIdMutation(callbacks: { onError?: (error: ResponseError) => void; onSuccess?: () => void; @@ -81,6 +73,13 @@ export function useKycSessionIdMutation(callbacks: { }); return response; } catch (error) { + // 401 - unauthenticated also means that email not verified + // 400 - bad request also means that KYC already approved + + // normally if app receives 401 status code it tries to obtain + // access token with refresh token, kycSessionIdMutation has to + // implement its own flow to handle that case because 401 that + // can be revived for "kyc/start" doesn't mean that JWT token expired if (error instanceof FetchError) { if (error.status === 401) { return { error: 'emailNotVerified' }; diff --git a/packages/apps/human-app/frontend/src/components/layout/drawer-menu-items/drawer-menu-items-worker.tsx b/packages/apps/human-app/frontend/src/components/layout/drawer-menu-items/drawer-menu-items-worker.tsx index 144137f478..d5fa7ae152 100644 --- a/packages/apps/human-app/frontend/src/components/layout/drawer-menu-items/drawer-menu-items-worker.tsx +++ b/packages/apps/human-app/frontend/src/components/layout/drawer-menu-items/drawer-menu-items-worker.tsx @@ -7,10 +7,10 @@ import type { } from '@/components/layout/protected/drawer-navigation'; import { HelpIcon, UserOutlinedIcon, WorkIcon } from '@/components/ui/icons'; import { routerPaths } from '@/router/router-paths'; -import { useAuth } from '@/auth/use-auth'; -export const WorkerDrawerTopMenuItems = (): TopMenuItem[] => { - const { user } = useAuth(); +export const workerDrawerTopMenuItems = ( + disableLabeling: boolean +): TopMenuItem[] => { return [ { { label: t('components.DrawerNavigation.captchaLabelling'), link: routerPaths.worker.enableLabeler, - disabled: Boolean(!user?.address), + disabled: disableLabeling, }, { label: t('components.DrawerNavigation.jobsDiscovery'), @@ -38,7 +38,6 @@ export const WorkerDrawerTopMenuItems = (): TopMenuItem[] => { ]; }; -// eslint-disable-next-line react-refresh/only-export-components -- ... export const workerDrawerBottomMenuItems: BottomMenuItem[] = [ { label: t('components.DrawerNavigation.profile'), diff --git a/packages/apps/human-app/frontend/src/components/layout/protected/drawer-navigation.tsx b/packages/apps/human-app/frontend/src/components/layout/protected/drawer-navigation.tsx index 3b8117ebff..081c42255f 100644 --- a/packages/apps/human-app/frontend/src/components/layout/protected/drawer-navigation.tsx +++ b/packages/apps/human-app/frontend/src/components/layout/protected/drawer-navigation.tsx @@ -91,11 +91,12 @@ export function DrawerNavigation({ const { link, label, disabled } = item; return ( - + { + if (disabled) return; if (link) { - if (disabled) return; navigate(link); } }} diff --git a/packages/apps/human-app/frontend/src/hooks/use-kyc-notification.tsx b/packages/apps/human-app/frontend/src/hooks/use-kyc-notification.tsx index 4996b75f3d..817e139493 100644 --- a/packages/apps/human-app/frontend/src/hooks/use-kyc-notification.tsx +++ b/packages/apps/human-app/frontend/src/hooks/use-kyc-notification.tsx @@ -2,15 +2,13 @@ import { useProtectedLayoutNotification } from '@/hooks/use-protected-layout-not import { defaultErrorMessage } from '@/shared/helpers/default-error-message'; import type { ResponseError } from '@/shared/types/global.type'; -export function useKycNotifications() { +export function useKycErrorNotifications() { const { setTopNotification } = useProtectedLayoutNotification(); - return { - onError: (error: ResponseError) => { - setTopNotification({ - type: 'warning', - content: defaultErrorMessage(error), - }); - }, + return (error: ResponseError) => { + setTopNotification({ + type: 'warning', + content: defaultErrorMessage(error), + }); }; } diff --git a/packages/apps/human-app/frontend/src/pages/worker/profile/profile-actions.tsx b/packages/apps/human-app/frontend/src/pages/worker/profile/profile-actions.tsx index 9c1f194ce9..f0b4df9464 100644 --- a/packages/apps/human-app/frontend/src/pages/worker/profile/profile-actions.tsx +++ b/packages/apps/human-app/frontend/src/pages/worker/profile/profile-actions.tsx @@ -14,13 +14,13 @@ import { RegisterAddressAction } from '@/pages/worker/profile/register-address-a import { RequireWalletConnect } from '@/auth-web3/require-wallet-connect'; import { useResendEmailVerificationWorkerMutation } from '@/api/servieces/worker/resend-email-verification'; import { WalletConnectDone } from '@/pages/worker/profile/wallet-connect-done'; -import { useKycNotifications } from '@/hooks/use-kyc-notification'; +import { useKycErrorNotifications } from '@/hooks/use-kyc-notification'; export function ProfileActions() { const navigation = useNavigate(); const { mutate: resendEmailVerificationMutation } = useResendEmailVerificationWorkerMutation(); - const { onError } = useKycNotifications(); + const onError = useKycErrorNotifications(); const { data: kycSessionIdData, mutate: kycSessionIdMutation, diff --git a/packages/apps/human-app/frontend/src/router/router.tsx b/packages/apps/human-app/frontend/src/router/router.tsx index b642116b0b..895b4caae4 100644 --- a/packages/apps/human-app/frontend/src/router/router.tsx +++ b/packages/apps/human-app/frontend/src/router/router.tsx @@ -12,14 +12,16 @@ import { RequireWalletConnect } from '@/auth-web3/require-wallet-connect'; import { RequireWeb3Auth } from '@/auth-web3/require-web3-auth'; import { DrawerNavigation } from '@/components/layout/protected/drawer-navigation'; import { - WorkerDrawerTopMenuItems, + workerDrawerTopMenuItems, workerDrawerBottomMenuItems, } from '@/components/layout/drawer-menu-items/drawer-menu-items-worker'; import { operatorDrawerBottomMenuItems } from '@/components/layout/drawer-menu-items/drawer-menu-items-operator'; import { browserAuthProvider } from '@/shared/helpers/browser-auth-provider'; import { UserStatsDrawer } from '@/pages/worker/hcaptcha-labeling/hcaptcha-labeling/user-stats-drawer'; +import { useAuth } from '@/auth/use-auth'; export function Router() { + const { user } = useAuth(); return ( }> @@ -54,7 +56,7 @@ export function Router() { window.location.reload(); }); }} - topMenuItems={WorkerDrawerTopMenuItems()} + topMenuItems={workerDrawerTopMenuItems(!user?.address)} /> )} renderHCaptchaStatisticsDrawer={(isOpen) => (