From 8f5f4c88bc183a61a7b78385a0429752f720ac74 Mon Sep 17 00:00:00 2001 From: Alina Lytovchenko Date: Mon, 9 Dec 2024 22:34:18 -0800 Subject: [PATCH] fix: resolve PR comments --- src/app/api/generateBounty/route.ts | 75 +++++++++++++--------------- src/components/global/FormBounty.tsx | 29 +++++------ src/utils/types.ts | 6 --- 3 files changed, 49 insertions(+), 61 deletions(-) diff --git a/src/app/api/generateBounty/route.ts b/src/app/api/generateBounty/route.ts index 4a652a59..ab66e776 100644 --- a/src/app/api/generateBounty/route.ts +++ b/src/app/api/generateBounty/route.ts @@ -1,48 +1,45 @@ -export async function POST(): Promise { - try { - const OPENAI_API_KEY = process.env.OPENAI_API_KEY; - if (!OPENAI_API_KEY) { - throw new Error('Missing OpenAI API key'); - } - - const response = await fetch('https://api.openai.com/v1/chat/completions', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${OPENAI_API_KEY}`, - }, - body: JSON.stringify({ - model: 'gpt-4-turbo', - messages: [ - { - role: 'system', - content: `Generate unique, creative, and fun bounty ideas for the "Pics or It Didn't Happen" (poidh) website. Each bounty should encourage users to engage in amusing, interesting, or surprising activities that can be easily documented with a photo, screenshot, or video. +const prompt = `Generate unique, creative, and fun bounty ideas for the "Pics or It Didn't Happen" (poidh) website. Each bounty should encourage users to engage in amusing, interesting, or surprising activities that can be easily documented with a photo, screenshot, or video. Ensure the ideas are diverse, spanning different themes such as real-life actions, contributions, playful tasks, or simple creative(could be developer) projects. Ideas must remain achievable and enjoyable for users of all skill levels. A user should share result either in video or in photo. Include: Title: A short, catchy description of the bounty (max 50 characters). Description: A clear and engaging explanation of what the user must do to complete the bounty (max 350 characters). Return the ideas in JSON format like this: - { 'title': '...', 'description': '...' }.`, - }, - { - role: 'user', - content: 'Generate a bounty idea for a person to do.', - }, - ], - max_tokens: 100, - temperature: 1, - response_format: { type: 'json_object' }, - }), - }); + { 'title': '...', 'description': '...' }.`; + +export async function POST(): Promise { + const OPENAI_API_KEY = process.env.OPENAI_API_KEY; + if (!OPENAI_API_KEY) { + throw new Error('Missing OpenAI API key'); + } - if (!response.ok) { - console.log('Error in response from OpenAI API.'); - } + const response = await fetch('https://api.openai.com/v1/chat/completions', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${OPENAI_API_KEY}`, + }, + body: JSON.stringify({ + model: 'gpt-4-turbo', + messages: [ + { + role: 'system', + content: prompt, + }, + { + role: 'user', + content: 'Generate a bounty idea for a person to do.', + }, + ], + max_tokens: 100, + temperature: 1, + response_format: { type: 'json_object' }, + }), + }); - const data = await response.json(); - return Response.json(data.choices[0].message.content); - } catch (error) { - console.error('Error communicating with OpenAI API:', error); - return Response.error(); + if (!response.ok) { + throw new Error(`Error: ${response.status}`); } + + const data = await response.json(); + return Response.json(data.choices[0].message.content); } diff --git a/src/components/global/FormBounty.tsx b/src/components/global/FormBounty.tsx index 1a6104f4..64f80abe 100644 --- a/src/components/global/FormBounty.tsx +++ b/src/components/global/FormBounty.tsx @@ -20,7 +20,11 @@ import { trpcClient } from '@/trpc/client'; import GameButton from '@/components/global/GameButton'; import ButtonCTA from '@/components/ui/ButtonCTA'; import { InfoIcon } from '@/components/global/Icons'; -import { Bounty } from '@/utils/types'; + +type Bounty = { + title: string; + description: string; +}; export default function FormBounty({ open, @@ -118,12 +122,7 @@ export default function FormBounty({ 'Content-Type': 'application/json', }, }); - - if (!res.ok) { - throw new Error(`Error: ${res.status}`); - } - - return JSON.parse(await res.json()); + return JSON.parse(await res.json()) as Bounty; }, onSuccess: (bounty: Bounty) => { setName(bounty.title); @@ -161,7 +160,7 @@ export default function FormBounty({ title @@ -173,14 +172,13 @@ export default function FormBounty({ onChange={(e) => setName(e.target.value)} className={cn( 'border py-2 px-2 rounded-md mb-4 bg-transparent border-[#D1ECFF]', - generateBountyMutation.isPending - ? 'cursor-not-allowed animate-pulse' - : '' + generateBountyMutation.isPending && + 'cursor-not-allowed animate-pulse' )} /> description @@ -192,9 +190,8 @@ export default function FormBounty({ onChange={(e) => setDescription(e.target.value)} className={cn( 'border py-2 px-2 rounded-md mb-4 max-h-28 bg-transparent border-[#D1ECFF]', - generateBountyMutation.isPending - ? 'cursor-not-allowed animate-pulse' - : '' + generateBountyMutation.isPending && + 'cursor-not-allowed animate-pulse' )} > @@ -257,7 +254,7 @@ export default function FormBounty({