From 59543386296a6e17a6ad16b06b13fee02c23e408 Mon Sep 17 00:00:00 2001 From: Juraj Uhlar Date: Thu, 19 Sep 2024 15:35:31 +0100 Subject: [PATCH 1/5] chore: move loan risk client code to `app` --- .../index.tsx => app/loan-risk/LoanRisk.tsx} | 8 ++++---- src/app/loan-risk/embed/page.tsx | 9 +++++++++ src/{pages => app}/loan-risk/loanRisk.module.scss | 0 src/app/loan-risk/page.tsx | 9 +++++++++ src/pages/loan-risk/embed.tsx | 13 ------------- 5 files changed, 22 insertions(+), 17 deletions(-) rename src/{pages/loan-risk/index.tsx => app/loan-risk/LoanRisk.tsx} (97%) create mode 100644 src/app/loan-risk/embed/page.tsx rename src/{pages => app}/loan-risk/loanRisk.module.scss (100%) create mode 100644 src/app/loan-risk/page.tsx delete mode 100644 src/pages/loan-risk/embed.tsx diff --git a/src/pages/loan-risk/index.tsx b/src/app/loan-risk/LoanRisk.tsx similarity index 97% rename from src/pages/loan-risk/index.tsx rename to src/app/loan-risk/LoanRisk.tsx index 151c5afc..43c6f927 100644 --- a/src/pages/loan-risk/index.tsx +++ b/src/app/loan-risk/LoanRisk.tsx @@ -1,6 +1,7 @@ +'use client'; + import { UseCaseWrapper } from '../../client/components/common/UseCaseWrapper/UseCaseWrapper'; import { FunctionComponent, useCallback, useMemo, useState } from 'react'; - import { loanDurationValidation, loanValueValidation, @@ -10,7 +11,6 @@ import { useRequestLoan } from '../../client/api/loan-risk/use-request-loan'; import { calculateMonthInstallment } from '../../shared/loan-risk/calculate-month-installment'; import React from 'react'; import { USE_CASES } from '../../client/components/common/content'; -import { CustomPageProps } from '../_app'; import Button from '../../client/components/common/Button/Button'; import { Alert } from '../../client/components/common/Alert/Alert'; import formStyles from '../../styles/forms.module.scss'; @@ -64,7 +64,7 @@ const SliderField: FunctionComponent = ({ ); }; -export default function LoanRisk({ embed }: CustomPageProps) { +export function LoanRisk() { const { getData, isLoading: isVisitorDataLoading } = useVisitorData( { ignoreCache: true }, { @@ -110,7 +110,7 @@ export default function LoanRisk({ embed }: CustomPageProps) { const isLoading = isVisitorDataLoading || loanRequestMutation.isLoading; return ( - +
diff --git a/src/app/loan-risk/embed/page.tsx b/src/app/loan-risk/embed/page.tsx new file mode 100644 index 00000000..f50b538f --- /dev/null +++ b/src/app/loan-risk/embed/page.tsx @@ -0,0 +1,9 @@ +import { USE_CASES } from '../../../client/components/common/content'; +import { generateUseCaseMetadata } from '../../../client/components/common/seo'; +import { LoanRisk } from '../LoanRisk'; + +export const metadata = generateUseCaseMetadata(USE_CASES.loanRisk); + +export default function VpnDetectionPage() { + return ; +} diff --git a/src/pages/loan-risk/loanRisk.module.scss b/src/app/loan-risk/loanRisk.module.scss similarity index 100% rename from src/pages/loan-risk/loanRisk.module.scss rename to src/app/loan-risk/loanRisk.module.scss diff --git a/src/app/loan-risk/page.tsx b/src/app/loan-risk/page.tsx new file mode 100644 index 00000000..9276e078 --- /dev/null +++ b/src/app/loan-risk/page.tsx @@ -0,0 +1,9 @@ +import { USE_CASES } from '../../client/components/common/content'; +import { generateUseCaseMetadata } from '../../client/components/common/seo'; +import { LoanRisk } from './LoanRisk'; + +export const metadata = generateUseCaseMetadata(USE_CASES.loanRisk); + +export default function VpnDetectionPage() { + return ; +} diff --git a/src/pages/loan-risk/embed.tsx b/src/pages/loan-risk/embed.tsx deleted file mode 100644 index 9b51b282..00000000 --- a/src/pages/loan-risk/embed.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import { GetStaticProps } from 'next'; -import LoanRisk from './index'; -import { CustomPageProps } from '../_app'; - -export default LoanRisk; - -export const getStaticProps: GetStaticProps = async () => { - return { - props: { - embed: true, - }, - }; -}; From f8f9c523334cb4f1d7a782f77af4ee49b7339585 Mon Sep 17 00:00:00 2001 From: Juraj Uhlar Date: Thu, 19 Sep 2024 17:48:12 +0100 Subject: [PATCH 2/5] chore: move loan risk server code to `app` --- e2e/loan-risk.spec.ts | 2 +- src/app/loan-risk/LoanRisk.tsx | 69 +++++++----- .../request-loan}/calculate-loan-values.ts | 10 +- .../loan-risk/api/request-loan}/copy.ts | 0 .../loan-risk/api/request-loan}/database.ts | 2 +- src/app/loan-risk/api/request-loan/route.ts | 91 ++++++++++++++++ src/pages/api/admin/reset.ts | 2 +- src/pages/api/loan-risk/request-loan.ts | 103 ------------------ src/server/loan-risk/loan-risk-endpoint.ts | 63 ----------- 9 files changed, 146 insertions(+), 196 deletions(-) rename src/{server/loan-risk => app/loan-risk/api/request-loan}/calculate-loan-values.ts (70%) rename src/{server/loan-risk => app/loan-risk/api/request-loan}/copy.ts (100%) rename src/{server/loan-risk => app/loan-risk/api/request-loan}/database.ts (94%) create mode 100644 src/app/loan-risk/api/request-loan/route.ts delete mode 100644 src/pages/api/loan-risk/request-loan.ts delete mode 100644 src/server/loan-risk/loan-risk-endpoint.ts diff --git a/e2e/loan-risk.spec.ts b/e2e/loan-risk.spec.ts index 1ae91e8a..4a752b95 100644 --- a/e2e/loan-risk.spec.ts +++ b/e2e/loan-risk.spec.ts @@ -1,7 +1,7 @@ import { Page, expect, test } from '@playwright/test'; import { blockGoogleTagManager, resetScenarios } from './e2eTestUtils'; import { TEST_IDS } from '../src/client/testIDs'; -import { LOAN_RISK_COPY } from '../src/server/loan-risk/copy'; +import { LOAN_RISK_COPY } from '../src/app/loan-risk/api/request-loan/copy'; const testIds = TEST_IDS.loanRisk; diff --git a/src/app/loan-risk/LoanRisk.tsx b/src/app/loan-risk/LoanRisk.tsx index 43c6f927..edba5d81 100644 --- a/src/app/loan-risk/LoanRisk.tsx +++ b/src/app/loan-risk/LoanRisk.tsx @@ -1,13 +1,12 @@ 'use client'; import { UseCaseWrapper } from '../../client/components/common/UseCaseWrapper/UseCaseWrapper'; -import { FunctionComponent, useCallback, useMemo, useState } from 'react'; +import { FunctionComponent, useMemo, useState } from 'react'; import { loanDurationValidation, loanValueValidation, monthlyIncomeValidation, } from '../../client/loan-risk/validation'; -import { useRequestLoan } from '../../client/api/loan-risk/use-request-loan'; import { calculateMonthInstallment } from '../../shared/loan-risk/calculate-month-installment'; import React from 'react'; import { USE_CASES } from '../../client/components/common/content'; @@ -20,6 +19,8 @@ import styles from './loanRisk.module.scss'; import classNames from 'classnames'; import { TEST_IDS } from '../../client/testIDs'; import { useVisitorData } from '@fingerprintjs/fingerprintjs-pro-react'; +import { useMutation } from 'react-query'; +import { LoanRequestPayload, LoanRequestResponse } from './api/request-loan/route'; type SliderFieldProps = { label: string; @@ -64,14 +65,40 @@ const SliderField: FunctionComponent = ({
); }; + export function LoanRisk() { - const { getData, isLoading: isVisitorDataLoading } = useVisitorData( + const { getData: getVisitorData, isLoading: isVisitorDataLoading } = useVisitorData( { ignoreCache: true }, { immediate: false, }, ); - const loanRequestMutation = useRequestLoan(); + + const { + mutate: requestLoan, + isLoading: isLoanRequestLoading, + data: loanRequestResponse, + } = useMutation, unknown>({ + mutationKey: ['request loan'], + mutationFn: async ({ firstName, lastName, loanValue, monthlyIncome, loanDuration }) => { + const { requestId } = await getVisitorData({ ignoreCache: true }); + const response = await fetch('/loan-risk/api/request-loan', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + firstName, + lastName, + loanValue, + monthlyIncome, + loanDuration, + requestId, + } satisfies LoanRequestPayload), + }); + return await response.json(); + }, + }); const [firstName, setFirstName] = useState('John'); const [lastName, setLastName] = useState('Doe'); @@ -88,26 +115,18 @@ export function LoanRisk() { [loanDuration, loanValue], ); - const handleSubmit = useCallback( - async (event: React.FormEvent) => { - event.preventDefault(); - - const fpData = await getData(); - - if (!fpData) { - console.error("Visitor data couldn't be fetched"); - return; - } - - await loanRequestMutation.mutateAsync({ - fpData, - body: { loanValue, monthlyIncome, loanDuration, firstName, lastName }, - }); - }, - [firstName, lastName, loanDuration, loanRequestMutation, loanValue, monthlyIncome, getData], - ); + const handleSubmit = async (event: React.FormEvent) => { + event.preventDefault(); + await requestLoan({ + firstName, + lastName, + loanValue, + monthlyIncome, + loanDuration, + }); + }; - const isLoading = isVisitorDataLoading || loanRequestMutation.isLoading; + const isLoading = isVisitorDataLoading || isLoanRequestLoading; return ( @@ -172,8 +191,8 @@ export function LoanRisk() {
- {loanRequestMutation.data?.message && !loanRequestMutation.isLoading && ( - {loanRequestMutation.data.message} + {loanRequestResponse?.message && !isLoanRequestLoading && ( + {loanRequestResponse.message} )}