-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ildecimo/develop
email generator
- Loading branch information
Showing
7 changed files
with
274 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { NextResponse, type NextRequest } from 'next/server'; | ||
import { type Review } from 'types'; | ||
|
||
import { authorize } from '~/lib/authorize'; | ||
import * as db from '~/lib/db'; | ||
|
||
import { generateAISuggestedEmail } from '~/server/google-ai/generate-email'; | ||
|
||
export async function POST(req: NextRequest) { | ||
const authorized = authorize(); | ||
|
||
if (!authorized) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const accessToken = await db.getStoreToken(authorized.storeHash); | ||
|
||
if (!accessToken) { | ||
return new NextResponse( | ||
'Access token not found. Try to re-install the app.', | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
const reqBody = (await req.json()) as { review: Review; emailType: string }; | ||
|
||
const email = await generateAISuggestedEmail({ | ||
rating: reqBody.review.rating, | ||
title: reqBody.review.title, | ||
text: reqBody.review.text, | ||
customer: reqBody.review.name, | ||
emailType: | ||
reqBody.emailType === 'thank-you' ? 'Thank you email' : 'Follow-up email', | ||
}); | ||
|
||
return NextResponse.json(email); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Badge } from '@bigcommerce/big-design'; | ||
|
||
interface ComplimentBadgesProps { | ||
complimentCategoriesArray?: string[]; | ||
} | ||
|
||
export const ComplimentBadges = ({ | ||
complimentCategoriesArray = [], | ||
}: ComplimentBadgesProps) => { | ||
return ( | ||
<div className="flex flex-wrap items-center"> | ||
<span className="mr-2">Compliments:</span> | ||
|
||
{complimentCategoriesArray.length > 0 ? ( | ||
complimentCategoriesArray.map((compliment) => ( | ||
<Badge key={compliment} label={compliment} variant="success" /> | ||
)) | ||
) : ( | ||
<Badge label="No compliment" variant="warning" /> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Button } from '@bigcommerce/big-design'; | ||
import { EnvelopeIcon, HeartIcon } from '@heroicons/react/24/solid'; | ||
import { useState } from 'react'; | ||
|
||
import { type Review } from 'types'; | ||
import { type AnalyzeEmailOutputOptions } from '~/server/google-ai/generate-email'; | ||
|
||
interface GenerateEmailButtonProps { | ||
variant: 'thank-you' | 'follow-up'; | ||
review: Review; | ||
} | ||
|
||
export const GenerateEmailButton = ({ | ||
variant, | ||
review, | ||
}: GenerateEmailButtonProps) => { | ||
const [isGenerating, setIsGenerating] = useState(false); | ||
const isThankYou = variant === 'thank-you'; | ||
|
||
const handleGenerateEmail = () => { | ||
console.log('Generating email...'); | ||
|
||
setIsGenerating(true); | ||
fetch('/api/generate-email', { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
review, | ||
emailType: isThankYou ? 'thank-you' : 'follow-up', | ||
}), | ||
}) | ||
.then((res) => res.json() as Promise<AnalyzeEmailOutputOptions>) | ||
.then((res) => { | ||
const { body, subject } = res; | ||
|
||
if (body && subject) { | ||
window.open( | ||
`mailto:${review.email}?subject=${subject}&body=${body}`, | ||
'_blank' | ||
); | ||
} else { | ||
console.warn('Email not generated!', res); | ||
} | ||
}) | ||
.catch((err) => console.log(err)) | ||
.finally(() => setIsGenerating(false)); | ||
}; | ||
|
||
return ( | ||
<Button | ||
iconLeft={ | ||
isThankYou ? ( | ||
<HeartIcon className="h-6 w-6" /> | ||
) : ( | ||
<EnvelopeIcon className="h-6 w-6" /> | ||
) | ||
} | ||
variant="secondary" | ||
onClick={handleGenerateEmail} | ||
isLoading={isGenerating} | ||
> | ||
{isThankYou ? 'Thank You Email' : 'Follow Up Email'} | ||
</Button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.