WIP: add lifestyle block
This commit is contained in:
@@ -37,8 +37,8 @@ async function OrderAnalysisPage() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<HomeLayoutPageHeader
|
<HomeLayoutPageHeader
|
||||||
title={<Trans i18nKey={'order-analysis:title'} />}
|
title={<Trans i18nKey="order-analysis:title" />}
|
||||||
description={<Trans i18nKey={'order-analysis:description'} />}
|
description={<Trans i18nKey="order-analysis:description" />}
|
||||||
/>
|
/>
|
||||||
<PageBody>
|
<PageBody>
|
||||||
<OrderAnalysesCards analyses={analyses} countryCode={countryCode} />
|
<OrderAnalysesCards analyses={analyses} countryCode={countryCode} />
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import { Suspense } from 'react';
|
|
||||||
|
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
import { toTitleCase } from '@/lib/utils';
|
import { toTitleCase } from '@/lib/utils';
|
||||||
@@ -12,11 +10,9 @@ import { createUserAnalysesApi } from '@kit/user-analyses/api';
|
|||||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
|
|
||||||
|
import AIBlocks from '../_components/ai/ai-blocks';
|
||||||
import Dashboard from '../_components/dashboard';
|
import Dashboard from '../_components/dashboard';
|
||||||
import DashboardCards from '../_components/dashboard-cards';
|
import DashboardCards from '../_components/dashboard-cards';
|
||||||
import Recommendations from '../_components/recommendations';
|
|
||||||
import RecommendationsSkeleton from '../_components/recommendations-skeleton';
|
|
||||||
import { isValidOpenAiEnv } from '../_lib/server/is-valid-open-ai-env';
|
|
||||||
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';
|
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';
|
||||||
|
|
||||||
export const generateMetadata = async () => {
|
export const generateMetadata = async () => {
|
||||||
@@ -53,16 +49,13 @@ async function UserHomePage() {
|
|||||||
/>
|
/>
|
||||||
<PageBody>
|
<PageBody>
|
||||||
<Dashboard account={account} bmiThresholds={bmiThresholds} />
|
<Dashboard account={account} bmiThresholds={bmiThresholds} />
|
||||||
{(await isValidOpenAiEnv()) && (
|
|
||||||
<>
|
|
||||||
<h4>
|
<h4>
|
||||||
<Trans i18nKey="dashboard:recommendations.title" />
|
<Trans i18nKey="dashboard:recommendations.title" />
|
||||||
</h4>
|
</h4>
|
||||||
<Suspense fallback={<RecommendationsSkeleton />}>
|
<div className="mt-4 grid gap-6 sm:grid-cols-3">
|
||||||
<Recommendations account={account} />
|
<AIBlocks account={account} />
|
||||||
</Suspense>
|
</div>
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</PageBody>
|
</PageBody>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
42
app/home/(user)/_components/ai/ai-blocks.tsx
Normal file
42
app/home/(user)/_components/ai/ai-blocks.tsx
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import React, { Suspense } from 'react';
|
||||||
|
|
||||||
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
|
|
||||||
|
import { isValidOpenAiEnv } from '../../_lib/server/is-valid-open-ai-env';
|
||||||
|
import { loadAnalyses } from '../../_lib/server/load-analyses';
|
||||||
|
import LifeStyleCard from './life-style-card';
|
||||||
|
import OrderAnalysesPackageCard from './order-analyses-package-card';
|
||||||
|
import Recommendations from './recommendations';
|
||||||
|
import RecommendationsSkeleton from './recommendations-skeleton';
|
||||||
|
|
||||||
|
const AIBlocks = async ({ account }: { account: AccountWithParams }) => {
|
||||||
|
const isOpenAiAvailable = await isValidOpenAiEnv();
|
||||||
|
|
||||||
|
if (!isOpenAiAvailable) {
|
||||||
|
return <OrderAnalysesPackageCard />;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { analyses, countryCode } = await loadAnalyses();
|
||||||
|
|
||||||
|
if (analyses.length === 0) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<OrderAnalysesPackageCard />
|
||||||
|
<Suspense fallback={<RecommendationsSkeleton />}>
|
||||||
|
<LifeStyleCard />
|
||||||
|
</Suspense>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Suspense fallback={<RecommendationsSkeleton />}>
|
||||||
|
<LifeStyleCard />
|
||||||
|
<Recommendations account={account} />
|
||||||
|
</Suspense>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AIBlocks;
|
||||||
19
app/home/(user)/_components/ai/life-style-card.tsx
Normal file
19
app/home/(user)/_components/ai/life-style-card.tsx
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import { Card } from '@kit/ui/shadcn/card';
|
||||||
|
|
||||||
|
import { loadLifeStyle } from '../../_lib/server/load-life-style';
|
||||||
|
|
||||||
|
const LifeStyleCard = async () => {
|
||||||
|
const data = await loadLifeStyle();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card variant="gradient-success" className="flex flex-col justify-between">
|
||||||
|
Test
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default LifeStyleCard;
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import Link from 'next/link';
|
||||||
|
|
||||||
|
import { pathsConfig } from '@/packages/shared/src/config';
|
||||||
|
import { ChevronRight, HeartPulse } from 'lucide-react';
|
||||||
|
|
||||||
|
import { Trans } from '@kit/ui/makerkit/trans';
|
||||||
|
import { Button } from '@kit/ui/shadcn/button';
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
CardDescription,
|
||||||
|
CardFooter,
|
||||||
|
CardHeader,
|
||||||
|
} from '@kit/ui/shadcn/card';
|
||||||
|
|
||||||
|
const OrderAnalysesPackageCard = () => {
|
||||||
|
return (
|
||||||
|
<Card
|
||||||
|
variant="gradient-success"
|
||||||
|
className="xs:w-1/2 flex w-full flex-col justify-between sm:w-auto"
|
||||||
|
>
|
||||||
|
<CardHeader className="flex-row sm:pb-0">
|
||||||
|
<div
|
||||||
|
className={
|
||||||
|
'bg-primary/10 mb-6 flex size-8 items-center-safe justify-center-safe rounded-full text-white'
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<HeartPulse className="size-4 fill-green-500" />
|
||||||
|
</div>
|
||||||
|
<div className="bg-warning ml-auto flex size-8 items-center-safe justify-center-safe rounded-full text-white">
|
||||||
|
<Link href={pathsConfig.app.orderAnalysisPackage}>
|
||||||
|
<Button size="icon" variant="outline" className="px-2 text-black">
|
||||||
|
<ChevronRight className="size-4 stroke-2" />
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</CardHeader>
|
||||||
|
<CardFooter className="flex flex-col items-start gap-2">
|
||||||
|
<h5>
|
||||||
|
<Trans i18nKey="dashboard:heroCard.orderPackage.title" />
|
||||||
|
</h5>
|
||||||
|
<CardDescription className="text-primary">
|
||||||
|
<Trans i18nKey="dashboard:heroCard.orderPackage.description" />
|
||||||
|
</CardDescription>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default OrderAnalysesPackageCard;
|
||||||
@@ -4,9 +4,9 @@ import React from 'react';
|
|||||||
|
|
||||||
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
|
|
||||||
import { loadAnalyses } from '../_lib/server/load-analyses';
|
import { loadAnalyses } from '../../_lib/server/load-analyses';
|
||||||
import { loadRecommendations } from '../_lib/server/load-recommendations';
|
import { loadRecommendations } from '../../_lib/server/load-recommendations';
|
||||||
import OrderAnalysesCards from './order-analyses-cards';
|
import OrderAnalysesCards from '../order-analyses-cards';
|
||||||
|
|
||||||
export default async function Recommendations({
|
export default async function Recommendations({
|
||||||
account,
|
account,
|
||||||
@@ -25,6 +25,8 @@ export default async function Recommendations({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<>
|
||||||
<OrderAnalysesCards analyses={orderAnalyses} countryCode={countryCode} />
|
<OrderAnalysesCards analyses={orderAnalyses} countryCode={countryCode} />
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -57,9 +57,7 @@ export default function OrderAnalysesCards({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return analyses.map(({ title, variant, description, subtitle, price }) => {
|
||||||
<div className="xs:grid-cols-3 mt-4 grid gap-6">
|
|
||||||
{analyses.map(({ title, variant, description, subtitle, price }) => {
|
|
||||||
const formattedPrice =
|
const formattedPrice =
|
||||||
typeof price === 'number'
|
typeof price === 'number'
|
||||||
? formatCurrency({
|
? formatCurrency({
|
||||||
@@ -75,11 +73,7 @@ export default function OrderAnalysesCards({
|
|||||||
className="flex flex-col justify-between"
|
className="flex flex-col justify-between"
|
||||||
>
|
>
|
||||||
<CardHeader className="flex-row">
|
<CardHeader className="flex-row">
|
||||||
<div
|
<div className="bg-primary/10 mb-6 flex size-8 items-center-safe justify-center-safe rounded-full text-white">
|
||||||
className={
|
|
||||||
'bg-primary/10 mb-6 flex size-8 items-center-safe justify-center-safe rounded-full text-white'
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<HeartPulse className="size-4 fill-green-500" />
|
<HeartPulse className="size-4 fill-green-500" />
|
||||||
</div>
|
</div>
|
||||||
<div className="bg-warning ml-auto flex size-8 items-center-safe justify-center-safe rounded-full text-white">
|
<div className="bg-warning ml-auto flex size-8 items-center-safe justify-center-safe rounded-full text-white">
|
||||||
@@ -122,7 +116,5 @@ export default function OrderAnalysesCards({
|
|||||||
</CardFooter>
|
</CardFooter>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
})}
|
});
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
41
app/home/(user)/_lib/server/load-life-style.ts
Normal file
41
app/home/(user)/_lib/server/load-life-style.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { cache } from 'react';
|
||||||
|
|
||||||
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
|
import OpenAI from 'openai';
|
||||||
|
|
||||||
|
import PersonalCode from '~/lib/utils';
|
||||||
|
|
||||||
|
const failedResponse = {
|
||||||
|
lifeStyle: null,
|
||||||
|
summary: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
async function lifeStyleLoader(account: AccountWithParams) {
|
||||||
|
if (!account?.personal_code) {
|
||||||
|
return failedResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
const openAIClient = new OpenAI();
|
||||||
|
const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code);
|
||||||
|
try {
|
||||||
|
const response = await openAIClient.responses.create({
|
||||||
|
store: false,
|
||||||
|
prompt: {
|
||||||
|
id: analysesRecommendationsPromptId,
|
||||||
|
variables: {
|
||||||
|
analyses: JSON.stringify(formattedAnalyses),
|
||||||
|
results: JSON.stringify(formattedAnalysisResponses),
|
||||||
|
gender: gender.value,
|
||||||
|
age: age.toString(),
|
||||||
|
weight: weight.toString(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error calling OpenAI: ', error);
|
||||||
|
return failedResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const loadLifeStyle = cache(lifeStyleLoader);
|
||||||
@@ -21,6 +21,10 @@
|
|||||||
"benefits": {
|
"benefits": {
|
||||||
"title": "Sinu Medreport konto seis",
|
"title": "Sinu Medreport konto seis",
|
||||||
"validUntil": "Kehtiv kuni {{date}}"
|
"validUntil": "Kehtiv kuni {{date}}"
|
||||||
|
},
|
||||||
|
"orderPackage": {
|
||||||
|
"title": "Telli analüüside pakett",
|
||||||
|
"description": "Võrdle erinevate pakettide vahel ja vali endale sobiv"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"recommendations": {
|
"recommendations": {
|
||||||
|
|||||||
Reference in New Issue
Block a user