Skip to content

Commit 9fca37c

Browse files
authored
Merge pull request #162 from Lemoncode/feature/#141-get-embalses-by-province
feature #141 get embalses by province
2 parents a8ca0db + aca491c commit 9fca37c

File tree

6 files changed

+77
-22
lines changed

6 files changed

+77
-22
lines changed
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { PROVINCIAS } from "@/core/constants";
22
import { EmbalseProvinciaPod } from "@/pods/embalse-provincia";
3+
import { mapEmbalseListFromApiToLookup } from "@/pods/embalse-provincia/embalse-provincia.mapper";
4+
import { getEmbalsesByProvince } from "@/pods/embalse-provincia/embalse-provincia.repository";
35
import { Metadata } from "next";
46

57
interface Props {
68
params: Promise<{ provincia: string }>;
79
}
810

11+
const getNombreProvincia = (id: string): string =>
12+
PROVINCIAS.find((p) => p.id === id)?.name ?? "Provincia";
13+
914
export async function generateMetadata({ params }: Props): Promise<Metadata> {
1015
const { provincia } = await params;
1116

12-
const nombreProvincia = PROVINCIAS.find(
13-
(province) => province.id === provincia,
14-
)?.name;
17+
const nombreProvincia = getNombreProvincia(provincia);
1518

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

24-
const nombreProvincia = PROVINCIAS.find(
25-
(province) => province.id === provincia,
26-
)?.name;
27+
const nombreProvincia = getNombreProvincia(provincia);
28+
const embalsesByProvinceFromApi =
29+
await getEmbalsesByProvince(nombreProvincia);
2730

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

3835
return (
3936
<EmbalseProvinciaPod
4037
nombreProvincia={nombreProvincia}
41-
embalses={reservoirs}
38+
embalses={embalsesByProvinceLookup}
4239
/>
4340
);
4441
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface EmbalseApi {
2+
_id: string;
3+
name: string;
4+
}

front/src/pods/embalse-provincia/embalse-provincia.component.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export const EmbalseProvincia: React.FC<Props> = (props) => {
1212
const { nombreProvincia, embalses } = props;
1313
return (
1414
<Card>
15-
<h2>Embalses de {nombreProvincia}</h2>
15+
{embalses.length === 0 ? (
16+
<h2>No se encontraron embalses para {nombreProvincia}</h2>
17+
) : (
18+
<h2>Embalses de {nombreProvincia}</h2>
19+
)}
1620

1721
{embalses.map(({ id, name }) => (
1822
<Link key={id} href={`/embalse/${id}`} className="link-accessible">
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Lookup } from "@/common/models";
2+
import { EmbalseApi } from "./embalse-provincia.api-model";
3+
4+
export const mapEmbalseListFromApiToLookup = (
5+
embalses: EmbalseApi[]
6+
): Lookup[] =>
7+
Array.isArray(embalses)
8+
? embalses.map((embalse) => ({
9+
id: embalse._id,
10+
text: embalse.name,
11+
name: embalse.name,
12+
}))
13+
: [];
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use server";
2+
3+
import { getDb } from "@/lib/mongodb";
4+
import type { EmbalseApi } from "./embalse-provincia.api-model";
5+
6+
export async function getEmbalsesByProvince(
7+
provincia: string,
8+
): Promise<EmbalseApi[]> {
9+
try {
10+
const db = await getDb();
11+
const docs = await db
12+
.collection("embalses")
13+
.find(
14+
{ provincia },
15+
{
16+
projection: {
17+
_id: 1,
18+
nombre: 1,
19+
slug: 1,
20+
},
21+
},
22+
)
23+
.toArray();
24+
25+
console.log(docs);
26+
return docs.map((doc) => ({
27+
_id: doc.slug ?? String(doc._id),
28+
name: doc.nombre ?? "",
29+
}));
30+
} catch (error) {
31+
console.warn(
32+
"getEmbalsesByProvince: MongoDB not available (build time?), returning empty array.",
33+
"Error:",
34+
error instanceof Error ? error.message : error,
35+
);
36+
return [];
37+
}
38+
}

package-lock.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)