Merge pull request #1 from iptq/devin/test

feature/add-write-to-db-add-item
This commit is contained in:
michael 2022-11-06 19:28:43 -06:00 committed by GitHub
commit 398ce7b727
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 2371 additions and 23 deletions

5
.gitignore vendored
View file

@ -18,6 +18,7 @@
# misc
.DS_Store
*.pem
.vscode
# debug
npm-debug.log*
@ -26,7 +27,9 @@ yarn-error.log*
.pnpm-debug.log*
# local env files
.env*.local
.env*
.local
# vercel
.vercel

25
lib/getMongoDBClient.ts Normal file
View file

@ -0,0 +1,25 @@
import { Db, MongoClient } from 'mongodb';
const USERNAME = process.env.MONGO_USERNAME
const PASSWORD = process.env.MONGO_PASSWORD
const HOSTNAME = process.env.MONGO_HOSTNAME
const DATABASE_NAME = process.env.MONGO_DATABASE_NAME
const DATABASE_PORT = process.env.MONGO_DATABASE_PORT
const URI =
`mongodb://${USERNAME}:${PASSWORD}@${HOSTNAME ?? 'localhost'}:${DATABASE_PORT}`;
let db: Db | null = null;
export const getMongoDBClient = async () => {
if (db) {
return db;
}
const client = new MongoClient(URI);
await client.connect();
db = client.db(DATABASE_NAME);
return db;
}

2284
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -12,6 +12,7 @@
"@reduxjs/toolkit": "^1.8.6",
"bootstrap": "^5.2.2",
"jotai": "^1.8.6",
"mongodb": "^4.11.0",
"next": "12.3.1",
"react": "18.2.0",
"react-bootstrap": "^2.5.0",

View file

@ -1,13 +0,0 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'
type Data = {
name: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
res.status(200).json({ name: 'John Doe' })
}

View file

@ -0,0 +1,21 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { getMongoDBClient } from '../../lib/getMongoDBClient';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Record<string, string>[] | Record<string, string>>
) {
if (req.method !== 'POST') {
return res.status(405).json({message: 'Only POST method allowed'});
}
const receipt = req.body;
const client = await getMongoDBClient();
try {
// TODO implement this
res.status(200).json(receipt);
} catch (e) {
res.status(500).json({message: (e as Error).message});
}
}

View file

@ -1,5 +1,6 @@
import { useAtom } from "jotai";
import { useAtom, atom } from "jotai";
import type { NextPage } from "next";
import { useEffect, useRef } from "react";
import { SyntheticEvent, useState } from "react";
import { Form } from "react-bootstrap";
import NumberEditBox from "../components/NumberEditBox";
@ -18,19 +19,59 @@ const Home: NextPage = () => {
const [input, setInput] = useState("");
const [total] = useAtom(totalAtom);
const [calculated] = useAtom(receiptTotalAtom);
const isAddCalled = useRef(false);
const [receiptJson] = useAtom(
atom((get) => {
const receiptJson: any[] = [];
for (const itemAtom of receipt) {
const receiptItemFromAtom = get(itemAtom);
const splitBetweenArray = get(receiptItemFromAtom.splitBetween).map(
(personAtom) => ({
name: get(get(personAtom).name),
})
);
const receiptItemParsed = {
name: get(receiptItemFromAtom.name),
price: get(receiptItemFromAtom.price),
splitBetween: splitBetweenArray,
};
receiptJson.push(receiptItemParsed);
}
return receiptJson;
})
);
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
const add = (e: SyntheticEvent) => {
const add = async (e: SyntheticEvent) => {
e.preventDefault();
isAddCalled.current = true;
addLine(input, receipt, setReceipt);
setInput("");
return false;
};
const receiptJSONString = JSON.stringify(receiptJson);
useEffect(() => {
const updateDb = async () => {
const response = await fetch("/api/createReceipt", {
method: "POST",
body: JSON.stringify({ receipts: receiptJson }),
});
console.log(receiptJSONString);
};
if (isAddCalled.current) {
updateDb();
}
}, [receiptJSONString]);
return (
<main>
<h1>Items</h1>