From b96082d44516e8ddc20c64b797735c5c9dd624c2 Mon Sep 17 00:00:00 2001 From: Bram Adams <3282661+bramses@users.noreply.github.com> Date: Sat, 17 Aug 2024 12:56:50 -0400 Subject: [PATCH] feat: Add button to call /api/random and populate textarea --- src/app/[locale]/(auth)/api/random/route.ts | 33 +++++++++++++++++++++ src/components/SearchBox.tsx | 19 ++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/app/[locale]/(auth)/api/random/route.ts diff --git a/src/app/[locale]/(auth)/api/random/route.ts b/src/app/[locale]/(auth)/api/random/route.ts new file mode 100644 index 0000000..5eb728c --- /dev/null +++ b/src/app/[locale]/(auth)/api/random/route.ts @@ -0,0 +1,33 @@ +import { NextResponse } from 'next/server'; + +import { logger } from '@/libs/Logger'; + +// import env variables + +export const POST = async (_: Request) => { + const { CLOUD_URL, DATABASE_URL } = process.env; + + const resp = await fetch(`${CLOUD_URL}/random`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + dbPath: DATABASE_URL, + }), + }); + logger.info('resp:', resp); + const data = await resp.json(); + + try { + logger.info(`A new random has been created ${JSON.stringify(data)}`); + + return NextResponse.json({ + data, + }); + } catch (error) { + logger.error(error, 'An error occurred while creating a search'); + + return NextResponse.json({}, { status: 500 }); + } +}; diff --git a/src/components/SearchBox.tsx b/src/components/SearchBox.tsx index 0b3d66c..267d3dd 100644 --- a/src/components/SearchBox.tsx +++ b/src/components/SearchBox.tsx @@ -315,6 +315,25 @@ const SearchBox = () => { Search the Web + {/* a btn to call /api/random and put the text in the textarea */} + + {showLoading && (