Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions front/src/app/embalse-provincia/[provincia]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { PROVINCIAS } from "@/core/constants";
import { EmbalseProvinciaPod } from "@/pods/embalse-provincia";
import { mapEmbalseListFromApiToLookup } from "@/pods/embalse-provincia/embalse-provincia.mapper";
import { getEmbalsesByProvince } from "@/pods/embalse-provincia/embalse-provincia.repository";
import { Metadata } from "next";

interface Props {
params: Promise<{ provincia: string }>;
}

const getNombreProvincia = (id: string): string =>
PROVINCIAS.find((p) => p.id === id)?.name ?? "Provincia";

export async function generateMetadata({ params }: Props): Promise<Metadata> {
const { provincia } = await params;

const nombreProvincia = PROVINCIAS.find(
(province) => province.id === provincia,
)?.name;
const nombreProvincia = getNombreProvincia(provincia);

return {
title: `Embalses de ${nombreProvincia}`,
Expand All @@ -21,24 +24,18 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
export default async function EmbalseProvinciaListadoPage({ params }: Props) {
const { provincia } = await params;

const nombreProvincia = PROVINCIAS.find(
(province) => province.id === provincia,
)?.name;
const nombreProvincia = getNombreProvincia(provincia);
const embalsesByProvinceFromApi =
await getEmbalsesByProvince(nombreProvincia);

// TODO: Reemplazar con datos reales obtenidos de la API
const reservoirs = [
{ id: "ullibarri-gamboa", name: "Ullibarri-Gamboa" },
{ id: "zadorra", name: "Zadorra" },
{ id: "urrúnaga", name: "Urrunaga" },
{ id: "maroño", name: "Maroño" },
{ id: "albina", name: "Albina" },
{ id: "santa-engracia", name: "Santa Engracia" },
];
const embalsesByProvinceLookup = mapEmbalseListFromApiToLookup(
embalsesByProvinceFromApi,
);

return (
<EmbalseProvinciaPod
nombreProvincia={nombreProvincia}
embalses={reservoirs}
embalses={embalsesByProvinceLookup}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface EmbalseApi {
_id: string;
name: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export const EmbalseProvincia: React.FC<Props> = (props) => {
const { nombreProvincia, embalses } = props;
return (
<Card>
<h2>Embalses de {nombreProvincia}</h2>
{embalses.length === 0 ? (
<h2>No se encontraron embalses para {nombreProvincia}</h2>
) : (
<h2>Embalses de {nombreProvincia}</h2>
)}

{embalses.map(({ id, name }) => (
<Link key={id} href={`/embalse/${id}`} className="link-accessible">
Expand Down
13 changes: 13 additions & 0 deletions front/src/pods/embalse-provincia/embalse-provincia.mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Lookup } from "@/common/models";
import { EmbalseApi } from "./embalse-provincia.api-model";

export const mapEmbalseListFromApiToLookup = (
embalses: EmbalseApi[]
): Lookup[] =>
Array.isArray(embalses)
? embalses.map((embalse) => ({
id: embalse._id,
text: embalse.name,
name: embalse.name,
}))
: [];
38 changes: 38 additions & 0 deletions front/src/pods/embalse-provincia/embalse-provincia.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use server";

import { getDb } from "@/lib/mongodb";
import type { EmbalseApi } from "./embalse-provincia.api-model";

export async function getEmbalsesByProvince(
provincia: string,
): Promise<EmbalseApi[]> {
try {
const db = await getDb();
const docs = await db
.collection("embalses")
.find(
{ provincia },
{
projection: {
_id: 1,
nombre: 1,
slug: 1,
},
},
)
.toArray();

console.log(docs);
return docs.map((doc) => ({
_id: doc.slug ?? String(doc._id),
name: doc.nombre ?? "",
}));
} catch (error) {
console.warn(
"getEmbalsesByProvince: MongoDB not available (build time?), returning empty array.",
"Error:",
error instanceof Error ? error.message : error,
);
return [];
}
}
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.