import React, { Fragment } from 'react' import { Metadata } from 'next' import Link from 'next/link' import { notFound } from 'next/navigation' import type { Order } from '../../../../payload/payload-types' import { Button } from '../../../__components/Button' import { Gutter } from '../../../_components/Gutter' import { HR } from '../../../_components/HR' import { Media } from '../../../_components/Media' import { Price } from '../../../_components/Price' import { formatDateTime } from '../../../_utilities/formatDateTime' import { getMeUser } from '../../../_utilities/getMeUser' import { mergeOpenGraph } from '../../../_utilities/mergeOpenGraph' import classes from './index.module.scss' export const dynamic = 'force-dynamic' export default async function Order({ params: { id } }) { const { token } = await getMeUser({ nullUserRedirect: `/login?error=${encodeURIComponent( 'Trebuie sa fii logat pentru a putea vedea comanda asta.', )}&redirect=${encodeURIComponent(`/orders/${id}`)}`, }) let order: Order | null = null try { order = await fetch(`${process.env.NEXT_PUBLIC_SERVER_URL}/api/orders/${id}`, { headers: { 'Content-Type': 'application/json', Authorization: `JWT ${token}`, }, })?.then(async res => { if (!res.ok) notFound() const json = await res.json() if ('error' in json && json.error) notFound() if ('errors' in json && json.errors) notFound() return json }) } catch (error) { console.error(error) // eslint-disable-line no-console } if (!order) { // notFound() console.error('Comanda nu a fost gasita') // eslint-disable-line no-console } return (

{`Comanda `} {`${order?.id}`}

{`Numar: ${order?.id}`}

{/*

{`Payment Intent: ${order.stripePaymentIntentID}`}

*/}

{`Plasata la data de: ${formatDateTime(order?.createdAt)}`}

{'Total: '} {new Intl.NumberFormat('ro', { style: 'currency', currency: 'lei', }).format(order.total)}

Status: {` ${order.status}`}


Produse

{order?.items?.map((item, index) => { if (typeof item.product === 'object') { const { quantity, product, product: { slug, title, meta }, } = item const isLast = index === (order?.items?.length || 0) - 1 const metaImage = meta?.image return (
{!metaImage && No image} {metaImage && typeof metaImage !== 'string' && ( )}
{title}

{`Cantitate: ${quantity}`}

{!isLast &&
}
) } return null })}

) } export async function generateMetadata({ params: { id } }): Promise { return { title: `Comanda ${id}`, description: `Detaliie comenzii pentru ${id}.`, openGraph: mergeOpenGraph({ title: `Comanda ${id}`, url: `/orders/${id}`, }), } }