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
8 changes: 5 additions & 3 deletions front/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { EmbalseSearch } from "@/pods/embalse-search";
import Link from "next/link";
import { getEmbalsesCollection } from "@/pods/embalse-search/api";

const RootPage = () => {
return <EmbalseSearch />;
const RootPage = async () => {
const embalses = await getEmbalsesCollection();

return <EmbalseSearch embalses={embalses} />;
};

export default RootPage;
4 changes: 2 additions & 2 deletions front/src/pods/embalse-search/api/api.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Embalse {
_id: string;
name: string;
province: string;
nombre: string;
provincia: string;
}
239 changes: 0 additions & 239 deletions front/src/pods/embalse-search/api/embalse-search.mock.ts

This file was deleted.

4 changes: 2 additions & 2 deletions front/src/pods/embalse-search/api/embalse.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getEmbalsesFromDb } from "../embalse-search.repository";
import { Embalse } from "./api.model";
import { embalseSearchMock } from "./embalse-search.mock";

export const getEmbalsesCollection = async (): Promise<Embalse[]> => {
return embalseSearchMock;
return getEmbalsesFromDb();
};
1 change: 0 additions & 1 deletion front/src/pods/embalse-search/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./api.model";
export * from "./embalse.api";
export * from "./embalse-search.mock";
9 changes: 9 additions & 0 deletions front/src/pods/embalse-search/embalse-search.mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as vm from "./embalse-search.vm";
import * as api from "db-model";

export const mapEmbalseToSearch = (
embalse: api.Embalse,
): vm.EmbalseSearchModel => ({
slug: embalse._id,
name: `${embalse.nombre} (${embalse.provincia})`,
});
38 changes: 38 additions & 0 deletions front/src/pods/embalse-search/embalse-search.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 { Embalse } from "./api/api.model";

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

return docs.map((doc) => ({
_id: doc.slug ?? createSlug(doc.nombre ?? ""),
nombre: doc.nombre ?? "",
provincia: doc.provincia ?? "",
}));
}

const createSlug = (nombre: string): string => {
return nombre
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/ñ/g, "n")
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
};
28 changes: 12 additions & 16 deletions front/src/pods/embalse-search/embalse-search.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
"use client";

import { useState, useEffect } from "react";
import { useState } from "react";
import { useCombobox } from "downshift";
import { useRouter } from "next/navigation";
import { SearchIcon } from "./components/search-icon";
import { Embalse, getEmbalsesCollection } from "./api";
import { EmbalseSearchModel } from "./embalse-search.model";
import { Embalse } from "./api";
import { EmbalseSearchModel } from "./embalse-search.vm";
import { mapEmbalseToSearch } from "./embalse-search.mapper";

const mapEmbalseToSearch = (embalse: Embalse): EmbalseSearchModel => ({
slug: embalse._id,
name: `${embalse.name} (${embalse.province})`,
});
interface Props {
embalses: Embalse[];
}

export const EmbalseSearch: React.FC = () => {
const router = useRouter();
const [embalses, setEmbalses] = useState<Embalse[]>([]);
export const EmbalseSearch: React.FC<Props> = (props) => {
const { embalses } = props;
const router = useRouter();
const [filteredEmbalses, setFilteredEmbalses] = useState<
EmbalseSearchModel[]
>([]);

useEffect(() => {
getEmbalsesCollection().then(setEmbalses);
}, []);

const getFilteredEmbalses = (inputValue: string): EmbalseSearchModel[] => {
const lower = inputValue.toLowerCase();

return embalses
.filter(
(e) =>
e.name.toLowerCase().includes(lower) ||
e.province.toLowerCase().includes(lower),
e.nombre.toLowerCase().includes(lower) ||
e.provincia.toLowerCase().includes(lower),
)
.map(mapEmbalseToSearch);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface EmbalseSearchModel {
slug: string; // embalse id used in the URL
slug: string; // Name of the reservoir for the URL
name: string; // Combination of name (province)
}