feat(MED-105): sort analysis result elements by date
This commit is contained in:
@@ -9,12 +9,10 @@ import { Button } from '@kit/ui/shadcn/button';
|
||||
|
||||
import { loadUserAnalysis } from '../../_lib/server/load-user-analysis';
|
||||
import Analysis from './_components/analysis';
|
||||
import { listProductTypes } from '@lib/data/products';
|
||||
import pathsConfig from '~/config/paths.config';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getOrders } from '~/lib/services/order.service';
|
||||
import { AnalysisElement, getAnalysisElements } from '~/lib/services/analysis-element.service';
|
||||
import type { UserAnalysisElement } from '@kit/accounts/types/accounts';
|
||||
import { getAnalysisElements } from '~/lib/services/analysis-element.service';
|
||||
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
|
||||
import { createPageViewLog } from '~/lib/services/audit/pageView.service';
|
||||
|
||||
@@ -33,12 +31,12 @@ async function AnalysisResultsPage() {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
const analysisList = await loadUserAnalysis();
|
||||
const analysisResponses = await loadUserAnalysis();
|
||||
const analysisResponseElements = analysisResponses?.flatMap(({ elements }) => elements);
|
||||
|
||||
const orders = await getOrders().catch(() => null);
|
||||
const { productTypes } = await listProductTypes();
|
||||
|
||||
if (!orders || !productTypes) {
|
||||
if (!orders) {
|
||||
redirect(pathsConfig.auth.signIn);
|
||||
}
|
||||
|
||||
@@ -51,17 +49,20 @@ async function AnalysisResultsPage() {
|
||||
...new Set(orders?.flatMap((order) => order.analysis_element_ids).filter(Boolean) as number[]),
|
||||
];
|
||||
const analysisElements = await getAnalysisElements({ ids: analysisElementIds });
|
||||
const analysisElementsWithResults = analysisElements.reduce((acc, curr) => {
|
||||
const analysisResponseWithElementResults = analysisList?.find((result) => result.elements.some((element) => element.analysis_element_original_id === curr.analysis_id_original));
|
||||
const elementResults = analysisResponseWithElementResults?.elements.find((element) => element.analysis_element_original_id === curr.analysis_id_original) as UserAnalysisElement | undefined;
|
||||
return {
|
||||
...acc,
|
||||
[curr.id]: {
|
||||
analysisElement: curr,
|
||||
results: elementResults,
|
||||
},
|
||||
}
|
||||
}, {} as Record<number, { analysisElement: AnalysisElement; results: UserAnalysisElement | undefined }>);
|
||||
const analysisElementsWithResults = analysisResponseElements
|
||||
?.sort((a, b) => {
|
||||
if (!a.updated_at || !b.updated_at) {
|
||||
return 0;
|
||||
}
|
||||
return new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime();
|
||||
})
|
||||
.map((results) => {
|
||||
return {
|
||||
results,
|
||||
}
|
||||
});
|
||||
const analysisElementsWithoutResults = analysisElements
|
||||
.filter((element) => !analysisElementsWithResults?.some(({ results }) => results.analysis_element_original_id === element.analysis_id_original));
|
||||
|
||||
return (
|
||||
<PageBody>
|
||||
@@ -71,7 +72,7 @@ async function AnalysisResultsPage() {
|
||||
<Trans i18nKey="analysis-results:pageTitle" />
|
||||
</h4>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{analysisList && analysisList.length > 0 ? (
|
||||
{analysisResponses && analysisResponses.length > 0 ? (
|
||||
<Trans i18nKey="analysis-results:description" />
|
||||
) : (
|
||||
<Trans i18nKey="analysis-results:descriptionEmpty" />
|
||||
@@ -85,9 +86,18 @@ async function AnalysisResultsPage() {
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex flex-col gap-2">
|
||||
{Object.entries(analysisElementsWithResults).map(([id, { analysisElement, results }]) => {
|
||||
{analysisElementsWithResults?.map(({ results }) => {
|
||||
const analysisElement = analysisElements.find((element) => element.analysis_id_original === results.analysis_element_original_id);
|
||||
if (!analysisElement) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<Analysis key={id} analysisElement={analysisElement} results={results} />
|
||||
<Analysis key={results.id} analysisElement={analysisElement} results={results} />
|
||||
);
|
||||
})}
|
||||
{analysisElementsWithoutResults?.map((element) => {
|
||||
return (
|
||||
<Analysis key={element.analysis_id_original} analysisElement={element} />
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
@@ -3,7 +3,6 @@ import { redirect } from 'next/navigation';
|
||||
import { listOrders } from '~/medusa/lib/data/orders';
|
||||
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
||||
import { listProductTypes } from '@lib/data/products';
|
||||
import { retrieveCustomer } from '@lib/data/customer';
|
||||
import { PageBody } from '@kit/ui/makerkit/page';
|
||||
import pathsConfig from '~/config/paths.config';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
@@ -21,11 +20,10 @@ export async function generateMetadata() {
|
||||
}
|
||||
|
||||
async function OrdersPage() {
|
||||
const customer = await retrieveCustomer();
|
||||
const orders = await listOrders().catch(() => null);
|
||||
const { productTypes } = await listProductTypes();
|
||||
|
||||
if (!customer || !orders || !productTypes) {
|
||||
if (!orders || !productTypes) {
|
||||
redirect(pathsConfig.auth.signIn);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user