From f6b89cdd738449b12878f5e86e78f390b1f91bc4 Mon Sep 17 00:00:00 2001 From: Juraj Uhlar Date: Fri, 20 Sep 2024 17:40:17 +0100 Subject: [PATCH] Chore: Refactor and migrate Loan risk to `app` directory INTER-911, INTER-459 (#159) * chore: move loan risk client code to `app` * chore: move loan risk server code to `app` * chore: improve loan request error handling * chore: self review fixes * fix: page component naming --- e2e/loan-risk.spec.ts | 2 +- .../index.tsx => app/loan-risk/LoanRisk.tsx} | 79 ++++++++------ .../loan-risk/api/request-loan}/copy.ts | 0 .../loan-risk/api/request-loan}/database.ts | 2 +- .../api/request-loan/evaluateLoanRequest.ts} | 10 +- src/app/loan-risk/api/request-loan/route.ts | 91 ++++++++++++++++ src/app/loan-risk/embed/page.tsx | 9 ++ .../loan-risk/loanRisk.module.scss | 0 src/app/loan-risk/page.tsx | 9 ++ src/app/playground/embed/page.tsx | 2 +- src/app/playground/page.tsx | 2 +- src/pages/api/admin/reset.ts | 2 +- src/pages/api/loan-risk/request-loan.ts | 103 ------------------ src/pages/loan-risk/embed.tsx | 13 --- src/server/loan-risk/loan-risk-endpoint.ts | 63 ----------- 15 files changed, 169 insertions(+), 218 deletions(-) rename src/{pages/loan-risk/index.tsx => app/loan-risk/LoanRisk.tsx} (74%) 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%) rename src/{server/loan-risk/calculate-loan-values.ts => app/loan-risk/api/request-loan/evaluateLoanRequest.ts} (63%) create mode 100644 src/app/loan-risk/api/request-loan/route.ts 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/api/loan-risk/request-loan.ts delete mode 100644 src/pages/loan-risk/embed.tsx 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/pages/loan-risk/index.tsx b/src/app/loan-risk/LoanRisk.tsx similarity index 74% rename from src/pages/loan-risk/index.tsx rename to src/app/loan-risk/LoanRisk.tsx index 151c5afc..71b16ec6 100644 --- a/src/pages/loan-risk/index.tsx +++ b/src/app/loan-risk/LoanRisk.tsx @@ -1,16 +1,15 @@ -import { UseCaseWrapper } from '../../client/components/common/UseCaseWrapper/UseCaseWrapper'; -import { FunctionComponent, useCallback, useMemo, useState } from 'react'; +'use client'; +import { UseCaseWrapper } from '../../client/components/common/UseCaseWrapper/UseCaseWrapper'; +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'; -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'; @@ -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 { LoanRequestData, LoanRequestPayload, LoanRequestResponse } from './api/request-loan/route'; type SliderFieldProps = { label: string; @@ -64,14 +65,35 @@ const SliderField: FunctionComponent = ({ ); }; -export default function LoanRisk({ embed }: CustomPageProps) { - const { getData, isLoading: isVisitorDataLoading } = useVisitorData( + +export function LoanRisk() { + const { getData: getVisitorData, isLoading: isVisitorDataLoading } = useVisitorData( { ignoreCache: true }, { immediate: false, }, ); - const loanRequestMutation = useRequestLoan(); + + const { + mutate: requestLoan, + isLoading: isLoanRequestLoading, + data: loanRequestResponse, + error: loanRequestNetworkError, + } = useMutation({ + mutationKey: ['request loan'], + mutationFn: async (loanRequest: LoanRequestData) => { + 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({ + ...loanRequest, + requestId, + } satisfies LoanRequestPayload), + }); + return await response.json(); + }, + }); const [firstName, setFirstName] = useState('John'); const [lastName, setLastName] = useState('Doe'); @@ -87,32 +109,24 @@ export default function LoanRisk({ embed }: CustomPageProps) { }), [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 isLoading = isVisitorDataLoading || loanRequestMutation.isLoading; + const isLoading = isVisitorDataLoading || isLoanRequestLoading; return ( - +
-
+ { + event.preventDefault(); + await requestLoan({ + firstName, + lastName, + loanValue, + monthlyIncome, + loanDuration, + }); + }} + className={formStyles.useCaseForm} + >
- {loanRequestMutation.data?.message && !loanRequestMutation.isLoading && ( - {loanRequestMutation.data.message} + {loanRequestNetworkError && {loanRequestNetworkError.message}} + {loanRequestResponse?.message && !isLoanRequestLoading && ( + {loanRequestResponse.message} )}