From 1299477b04580338023c95fcff87a59bcdd72eee Mon Sep 17 00:00:00 2001 From: dappsar Date: Thu, 31 Oct 2024 17:50:49 -0300 Subject: [PATCH 1/6] [feat] :truck: move transfer-all endpoint to user root endpoint (#65) --- src/app/api/_hooks/api-resolver.ts | 4 ++-- src/app/api/_hooks/use-wallet.ts | 6 ++++-- .../{wallet => user}/[id]/transfer-all/route.ts | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) rename src/app/api/v1/{wallet => user}/[id]/transfer-all/route.ts (76%) diff --git a/src/app/api/_hooks/api-resolver.ts b/src/app/api/_hooks/api-resolver.ts index 4b19cf7..c599961 100644 --- a/src/app/api/_hooks/api-resolver.ts +++ b/src/app/api/_hooks/api-resolver.ts @@ -57,12 +57,12 @@ export const endpoints = { id: (id: string) => getFullUIEndpoint(`user/${id}`), update: (id: string) => getFullUIEndpoint(`user/${id}`), code: (id: string) => getFullUIEndpoint(`user/${id}/code`), - updateEmail: (id: string) => getFullUIEndpoint(`user/${id}/email`) + updateEmail: (id: string) => getFullUIEndpoint(`user/${id}/email`), + transferAll: (id: string) => getFullUIEndpoint(`user/${id}/transfer-all`) }, wallet: { balance: (id: string) => getFullUIEndpoint(`wallet/${id}/balance`), transactions: (id: string) => getFullUIEndpoint(`wallet/${id}/transactions`), - transferAll: (id: string) => getFullUIEndpoint(`wallet/${id}/transferAll`), nfts: { root: (id: string) => getFullUIEndpoint(`wallet/${id}/nfts`), id: (walletId: string, nftId: string) => diff --git a/src/app/api/_hooks/use-wallet.ts b/src/app/api/_hooks/use-wallet.ts index 344080e..b63be56 100644 --- a/src/app/api/_hooks/use-wallet.ts +++ b/src/app/api/_hooks/use-wallet.ts @@ -22,7 +22,9 @@ export function useGetWalletNft(walletId: string, nftId: string) { // ---------------------------------------------------------------------- -export async function transferAll(walletId: string, data: { walletTo: string }) { - const res = await post(endpoints.dashboard.wallet.transferAll(walletId), data, {}) +export async function transferAll(userId: string, data: { walletTo: string }) { + console.log('trasnfer all', userId, data) + const res = await post(endpoints.dashboard.user.transferAll(userId), data, {}) + console.log('x', res) return res } diff --git a/src/app/api/v1/wallet/[id]/transfer-all/route.ts b/src/app/api/v1/user/[id]/transfer-all/route.ts similarity index 76% rename from src/app/api/v1/wallet/[id]/transfer-all/route.ts rename to src/app/api/v1/user/[id]/transfer-all/route.ts index e09895f..0beade5 100644 --- a/src/app/api/v1/wallet/[id]/transfer-all/route.ts +++ b/src/app/api/v1/user/[id]/transfer-all/route.ts @@ -1,6 +1,9 @@ import { NextRequest, NextResponse } from 'next/server' import { transferAll } from 'src/app/api/_data/blk-service' +import { getUserById } from 'src/app/api/_data/data-service' + +import { IAccount } from 'src/types/account' // ---------------------------------------------------------------------- @@ -42,7 +45,18 @@ export async function POST(req: NextRequest, { params }: { params: IParams }) { ) } - const result: boolean = await transferAll(walletTo) + const user: IAccount | undefined = await getUserById(id) + if (!user) { + return new NextResponse( + JSON.stringify({ code: 'USER_NOT_FOUND', error: 'user not found with that id' }), + { + status: 404, + headers: { 'Content-Type': 'application/json' } + } + ) + } + + const result: boolean = await transferAll(user.id, walletTo) if (!result) { return new NextResponse( JSON.stringify({ From 551016936f89b9167a22109888ee5d89cdf75357 Mon Sep 17 00:00:00 2001 From: dappsar Date: Thu, 31 Oct 2024 17:52:03 -0300 Subject: [PATCH 2/6] [feat] :necktie: add form business logic (#65) --- src/sections/banking/banking-balances.tsx | 14 +- src/sections/banking/transfer-all.tsx | 142 ++++-------------- .../banking/view/transfer-all-view.tsx | 4 +- 3 files changed, 39 insertions(+), 121 deletions(-) diff --git a/src/sections/banking/banking-balances.tsx b/src/sections/banking/banking-balances.tsx index 1d82205..fa2164b 100644 --- a/src/sections/banking/banking-balances.tsx +++ b/src/sections/banking/banking-balances.tsx @@ -14,6 +14,8 @@ import { SelectChangeEvent } from '@mui/material' +import { paths } from 'src/routes/paths' + import { useBoolean } from 'src/hooks/use-boolean' import { useResponsive } from 'src/hooks/use-responsive' @@ -40,7 +42,6 @@ export default function BankingBalances({ tableData, ...other }: Props) { - const walletLinkL1 = `${EXPLORER_L1}/address/${tableData?.wallet || ''}` const walletLinkL2 = `${EXPLORER_L2}/address/${tableData?.wallet || ''}` @@ -129,7 +130,6 @@ export default function BankingBalances({ > {t('balances.deposit')} - + ) diff --git a/src/sections/banking/transfer-all.tsx b/src/sections/banking/transfer-all.tsx index 2af7963..c7a1e4f 100644 --- a/src/sections/banking/transfer-all.tsx +++ b/src/sections/banking/transfer-all.tsx @@ -1,7 +1,7 @@ import * as Yup from 'yup' import { useForm } from 'react-hook-form' +import { useMemo, useState } from 'react' import { yupResolver } from '@hookform/resolvers/yup' -import { useMemo, useState, useEffect, useCallback } from 'react' import Box from '@mui/material/Box' import Card from '@mui/material/Card' @@ -14,140 +14,68 @@ import LoadingButton from '@mui/lab/LoadingButton' import { paths } from 'src/routes/paths' import { useRouter } from 'src/routes/hooks' -import { useCountdownSeconds } from 'src/hooks/use-countdown' - import { useTranslate } from 'src/locales' import { useAuthContext } from 'src/auth/hooks' +import { transferAll } from 'src/app/api/_hooks/use-wallet' import { useSnackbar } from 'src/components/snackbar' -import FormProvider, { RHFCode, RHFTextField } from 'src/components/hook-form' - -import { IAccount } from 'src/types/account' +import FormProvider, { RHFTextField } from 'src/components/hook-form' // ---------------------------------------------------------------------- export default function TransferAll() { const { enqueueSnackbar } = useSnackbar() const { t } = useTranslate() - const { generate2faCodeEmail, updateEmail } = useAuthContext() const { user } = useAuthContext() const router = useRouter() - - const [codeSent, setCodeSent] = useState(false) const [errorMsg, setErrorMsg] = useState('') - const [contextUser, setContextUser] = useState(null) - const { counting, countdown, startCountdown } = useCountdownSeconds(60) - - const ChangeEmailSchema = Yup.object().shape({ - oldEmail: Yup.string().email(t('common.must-be-valid-email')).required(t('common.required')), - newEmail: Yup.string().email(t('common.must-be-valid-email')).required(t('common.required')), - confirmEmail: Yup.string() - .oneOf([Yup.ref('newEmail')], t('common.emails-must-match')) - .required(t('common.required')), - // @ts-ignore - code: Yup.string().when('codeSent', { - is: true, - then: Yup.string() - .matches(/^[0-9]{6}$/, t('common.must-be-numeric')) - .required(t('common.required')) - }) + const TransferAllSchema = Yup.object().shape({ + wallet: Yup.string() + .matches(/^0x[a-zA-Z0-9]*$/, t('common.invalid-char')) + .required(t('common.required')) }) const defaultValues = useMemo( () => ({ - oldEmail: user?.email || '', - newEmail: '', - confirmEmail: '', - code: '' + wallet: '' }), - [user?.email] + [] ) const methods = useForm({ // @ts-ignore - resolver: yupResolver(ChangeEmailSchema), + resolver: yupResolver(TransferAllSchema), defaultValues }) const { watch, - setValue, handleSubmit, formState: { isSubmitting, isValid } } = methods - const newEmail = watch('newEmail') - const confirmEmail = watch('confirmEmail') - const codeValue = watch('code') + const wallet = watch('wallet') // ---------------------------------------------------------------------- - // guardar el telefono en estado, por cambios en el contexdto del user - useEffect(() => { - if (!user) return - // @ts-ignore - setContextUser(user) - }, [user]) - - const handleSendCode = useCallback(async () => { - try { - startCountdown() - setErrorMsg('') - - // @ts-ignore - await generate2faCodeEmail?.( - contextUser!.id, - // @ts-ignore - contextUser!.phoneNumber, - t('account.email.code-bot') - ) - - setCodeSent(true) - setValue('code', '') - enqueueSnackbar(`${t('account.email.code-sent')} ${contextUser}`, { - variant: 'info' - }) - } catch (ex) { - console.error(ex) - if (typeof ex === 'string') { - setErrorMsg(ex) - } else if (ex.code === 'USER_NOT_FOUND') { - setErrorMsg(t('login.msg.invalid-user')) - } else { - setErrorMsg(ex.error) - } - } - }, [startCountdown, contextUser, generate2faCodeEmail, t, setValue, enqueueSnackbar]) - const handleCancel = () => { - router.push(paths.dashboard.user.account) + router.push(paths.dashboard.root) } const onSubmit = async (data: any) => { try { - // @ts-ignore - await updateEmail?.(contextUser!.phoneNumber, data.code || '', confirmEmail, contextUser!.id) - router.push(paths.dashboard.user.account) + const walletTo = wallet.startsWith('0x') ? wallet : `0x${wallet}` + await transferAll(user!.id, { walletTo }) + router.push(paths.dashboard.root) setErrorMsg('') - enqueueSnackbar(t('common.msg.update-success')) + enqueueSnackbar(t('transfer-all.msgs.ok')) } catch (ex) { console.error(ex) - if (typeof ex === 'string') { - setErrorMsg(ex) - } else if (ex.code === 'USER_NOT_FOUND') { - setErrorMsg(t('login.msg.invalid-user')) - } else if (ex.code === 'INVALID_CODE') { - setErrorMsg(t('account.email.invalid-code')) - } else { - setErrorMsg(ex.error) - } + setErrorMsg(t('transfer-all.msgs.error')) } } - const isSendCodeDisabled = !newEmail || !confirmEmail || newEmail !== confirmEmail || counting - const isSaveDisabled = !codeSent || !codeValue || codeValue.length !== 6 || !isValid - // ---------------------------------------------------------------------- const renderForm = ( @@ -163,33 +91,15 @@ export default function TransferAll() { sm: 'repeat(1, 1fr)' }} > - - - - - {codeSent && ( - <> - {t('account.email.code-info')} - - - )} - + {t('transfer-all.info')} + {errorMsg && {errorMsg}} - - - - {counting - ? `${t('account.email.resend-code')} (${countdown}s)` - : t('account.email.send-code')} - - -