From 2d48d62b1929306fc47f63882872beace8969516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Viejo=20HUerta?= Date: Sat, 30 Nov 2024 12:12:22 +0100 Subject: [PATCH] arreglos --- .env.example | 2 ++ .gitignore | 3 +++ src/app/actions.ts | 30 +++++++++++++++++------------- src/app/addHouse/page.tsx | 3 +-- src/app/editHouse/[id]/page.tsx | 2 +- src/components/HouseForm.tsx | 13 +++++++++++++ src/components/ui/calendar.tsx | 4 ++-- src/lib/mongodb.ts | 16 ++++++++++++---- 8 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..b11bd52 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +MONGODB_URI=mongodb://localhost:27017 +MONGODB_DB=casa \ No newline at end of file diff --git a/.gitignore b/.gitignore index fd3dbb5..80a4c73 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts + +# environment variables +.env diff --git a/src/app/actions.ts b/src/app/actions.ts index ce1cabb..c88e71d 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -1,54 +1,58 @@ 'use server' -import { connectToDatabase } from '@/lib/mongodb'; +import { connectToDatabase, getDbName } from '@/lib/mongodb'; import { House } from '@/types/house'; import { ObjectId } from 'mongodb'; -export async function getHouses() { +export async function getHouses(): Promise { const client = await connectToDatabase(); - const collection = client.db('casa').collection('houses'); - return collection.find({}).toArray(); + const collection = client.db(getDbName()).collection('houses'); + const houses = await collection.find({}).toArray(); + return houses as unknown as House[]; } export async function addHouse(house: House) { const client = await connectToDatabase(); - const collection = client.db('casa').collection('houses'); + const collection = client.db(getDbName()).collection('houses'); // Calcular la puntuaciĆ³n const score = calculateScore(house); const houseWithScore = { ...house, score }; - const result = await collection.insertOne(houseWithScore); + const { _id, ...houseWithoutId } = houseWithScore; + console.log(_id); + const result = await collection.insertOne(houseWithoutId); return result; } export async function updateHouse(id: string, house: House) { const client = await connectToDatabase(); - const collection = client.db('casa').collection('houses'); + const collection = client.db(getDbName()).collection('houses'); const score = calculateScore(house); - const houseWithScore = { ...house, score }; + const { ...houseWithoutId } = { ...house, score }; const result = await collection.updateOne( { _id: new ObjectId(id) }, - { $set: houseWithScore } + { $set: houseWithoutId } ); return result; } export async function deleteHouse(id: string) { const client = await connectToDatabase(); - const collection = client.db('casa').collection('houses'); + const collection = client.db(getDbName()).collection('houses'); const result = await collection.deleteOne({ _id: new ObjectId(id) }); return result; } -export async function getHouse(id: string) { +export async function getHouse(id: string): Promise { const client = await connectToDatabase(); - const collection = client.db('casa').collection('houses'); + const collection = client.db(getDbName()).collection('houses'); - return collection.findOne({ _id: new ObjectId(id) }); + const house = await collection.findOne({ _id: new ObjectId(id) }); + return house as House | null; } function calculateScore(house: House): number { diff --git a/src/app/addHouse/page.tsx b/src/app/addHouse/page.tsx index e29f191..49046d8 100644 --- a/src/app/addHouse/page.tsx +++ b/src/app/addHouse/page.tsx @@ -2,12 +2,11 @@ import { useRouter } from 'next/navigation'; import HouseForm from '@/components/house/HouseForm'; -import { House } from '@/types/house'; export default function AddHousePage() { const router = useRouter(); - const handleSave = async (house: House) => { + const handleSave = async () => { router.push('/'); }; diff --git a/src/app/editHouse/[id]/page.tsx b/src/app/editHouse/[id]/page.tsx index b874da7..8d7ca32 100644 --- a/src/app/editHouse/[id]/page.tsx +++ b/src/app/editHouse/[id]/page.tsx @@ -18,7 +18,7 @@ export default function EditHousePage({ params }: { params: { id: string } }) { loadHouse(); }, [params.id]); - const handleSave = async (house: House) => { + const handleSave = async () => { router.push('/'); }; diff --git a/src/components/HouseForm.tsx b/src/components/HouseForm.tsx index 8f30bde..e533b6c 100644 --- a/src/components/HouseForm.tsx +++ b/src/components/HouseForm.tsx @@ -31,6 +31,19 @@ export default function HouseForm({ house, onClose, onSave }: HouseFormProps) { mobileSignal: 0, noiseLevel: 0, hasBalcony: false, + customLighting: false, + ergonomicFurniture: false, + professionalSetup: false, + relaxationAreas: false, + screenReflectionFree: false, + plantsSpace: false, + electricalPower: false, + fiberOptic: false, + internetBackup: false, + neighborWorkStyle: false, + nearbyCoworking: false, + privateVideocalls: false, + storageSpace: false, }); const handleSubmit = async (e: React.FormEvent) => { diff --git a/src/components/ui/calendar.tsx b/src/components/ui/calendar.tsx index 6978798..15169ae 100644 --- a/src/components/ui/calendar.tsx +++ b/src/components/ui/calendar.tsx @@ -60,8 +60,8 @@ function Calendar({ ...classNames, }} components={{ - IconLeft: ({ ...props }) => , - IconRight: ({ ...props }) => , + IconLeft: ({ }) => , + IconRight: ({ }) => , }} {...props} /> diff --git a/src/lib/mongodb.ts b/src/lib/mongodb.ts index c98fe85..511aacf 100644 --- a/src/lib/mongodb.ts +++ b/src/lib/mongodb.ts @@ -1,10 +1,14 @@ import { MongoClient } from 'mongodb'; -const MONGODB_URI = 'mongodb://localhost:27017'; -const MONGODB_DB = 'casa'; +const MONGODB_URI = process.env.MONGODB_URI; +const MONGODB_DB = process.env.MONGODB_DB; if (!MONGODB_URI) { - throw new Error('Please define the MONGODB_URI environment variable'); + throw new Error('Please define the MONGODB_URI environment variable inside .env'); +} + +if (!MONGODB_DB) { + throw new Error('Please define the MONGODB_DB environment variable inside .env'); } let cachedClient: MongoClient | null = null; @@ -14,7 +18,11 @@ export async function connectToDatabase() { return cachedClient; } - const client = await MongoClient.connect(MONGODB_URI); + const client = await MongoClient.connect(process.env.MONGODB_URI as string); cachedClient = client; return client; +} + +export function getDbName() { + return MONGODB_DB; } \ No newline at end of file