dovršen rendering homepage-a

This commit is contained in:
2024-01-04 14:46:31 +01:00
parent 496814d039
commit f11987dd3a
8 changed files with 102 additions and 72 deletions

View File

@@ -1,12 +1,16 @@
export type Location = {
id: number;
import { ObjectId } from "mongodb";
export interface Location {
id: ObjectId;
name: string;
bills: Bill[];
/** the value is encoded as yyyymm (i.e. 202301) */
yearMonth: number;
};
export type Bill = {
id: number;
export interface Bill {
id: ObjectId;
name: string;
paid: boolean;
amount?: number;
description?: string;
document?: string|null;
};

6
app/lib/format.ts Normal file
View File

@@ -0,0 +1,6 @@
export const formatYearMonth = (yearMonth: number): string => {
const year = Math.floor(yearMonth / 100);
const month = yearMonth % 100;
return `${year}-${month<10?"0":""}${month}`;
}

8
app/lib/global.d.ts vendored Normal file
View File

@@ -0,0 +1,8 @@
import { MongoClient } from "mongodb";
declare global {
namespace globalThis {
/** global Mongo Client used in development */
var _mongoClientPromise: Promise<MongoClient>
}
}

34
app/lib/mongodb.ts Normal file
View File

@@ -0,0 +1,34 @@
import { MongoClient } from 'mongodb'
if (!process.env.MONGODB_URI) {
throw new Error('Invalid environment variable: "MONGODB_URI"')
}
const uri = process.env.MONGODB_URI
const options = { }
let client
let clientPromise: Promise<MongoClient>
if (!process.env.MONGODB_URI) {
throw new Error('Please add your Mongo URI to .env.local')
}
if (process.env.NODE_ENV === 'development') {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options)
global._mongoClientPromise = client.connect()
}
clientPromise = global._mongoClientPromise
} else {
// In production mode, it's best to not use a global variable.
client = new MongoClient(uri, options)
clientPromise = client.connect()
}
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise