From dd84b9d64fb98746a230cd24233ff50a562c39c9 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 28 Apr 2023 01:36:44 +0800 Subject: --- .../test/with-mongodb-mongoose/.env.local.example | 1 + .../test/with-mongodb-mongoose/.gitignore | 34 ++++ .../test/with-mongodb-mongoose/README.md | 5 + .../test/with-mongodb-mongoose/components/Form.js | 202 +++++++++++++++++++++ .../test/with-mongodb-mongoose/css/form.css | 39 ++++ .../test/with-mongodb-mongoose/css/style.css | 184 +++++++++++++++++++ .../test/with-mongodb-mongoose/lib/dbConnect.js | 40 ++++ .../test/with-mongodb-mongoose/models/Pet.js | 59 ++++++ .../test/with-mongodb-mongoose/next.config.js | 12 ++ .../test/with-mongodb-mongoose/package.json | 20 ++ .../test/with-mongodb-mongoose/pages/[id]/edit.js | 33 ++++ .../test/with-mongodb-mongoose/pages/[id]/index.js | 75 ++++++++ .../test/with-mongodb-mongoose/pages/_app.js | 36 ++++ .../with-mongodb-mongoose/pages/api/pets/[id].js | 56 ++++++ .../with-mongodb-mongoose/pages/api/pets/index.js | 32 ++++ .../test/with-mongodb-mongoose/pages/index.js | 65 +++++++ .../test/with-mongodb-mongoose/pages/new.js | 19 ++ .../test/with-mongodb-mongoose/public/favicon.ico | Bin 0 -> 15086 bytes .../test/with-mongodb-mongoose/public/zeit.svg | 10 + 19 files changed, 922 insertions(+) create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/.env.local.example create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/.gitignore create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/README.md create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/components/Form.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/css/form.css create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/css/style.css create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/lib/dbConnect.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/models/Pet.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/next.config.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/package.json create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/pages/[id]/edit.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/pages/[id]/index.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/pages/_app.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/pages/api/pets/[id].js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/pages/api/pets/index.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/pages/index.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/pages/new.js create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/public/favicon.ico create mode 100644 packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/public/zeit.svg (limited to 'packages/turbo-tracing-next-plugin/test') diff --git a/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/.env.local.example b/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/.env.local.example new file mode 100644 index 0000000..9dead41 --- /dev/null +++ b/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/.env.local.example @@ -0,0 +1 @@ +MONGODB_URI= \ No newline at end of file diff --git a/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/.gitignore b/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/.gitignore new file mode 100644 index 0000000..1437c53 --- /dev/null +++ b/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/.gitignore @@ -0,0 +1,34 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env.local +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel diff --git a/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/README.md b/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/README.md new file mode 100644 index 0000000..1f7110e --- /dev/null +++ b/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/README.md @@ -0,0 +1,5 @@ +# MongoDB and Mongoose with Next.js + +Copied from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb. + +Run `pnpm run --filter @vercel/turbo-tracing-test-app build` to build this application. diff --git a/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/components/Form.js b/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/components/Form.js new file mode 100644 index 0000000..b184d9c --- /dev/null +++ b/packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/components/Form.js @@ -0,0 +1,202 @@ +import { useState } from "react"; +import { useRouter } from "next/router"; +import { mutate } from "swr"; + +const Form = ({ formId, petForm, forNewPet = true }) => { + const router = useRouter(); + const contentType = "application/json"; + const [errors, setErrors] = useState({}); + const [message, setMessage] = useState(""); + + const [form, setForm] = useState({ + name: petForm.name, + owner_name: petForm.owner_name, + species: petForm.species, + age: petForm.age, + poddy_trained: petForm.poddy_trained, + diet: petForm.diet, + image_url: petForm.image_url, + likes: petForm.likes, + dislikes: petForm.dislikes, + }); + + /* The PUT method edits an existing entry in the mongodb database. */ + const putData = async (form) => { + const { id } = router.query; + + try { + const res = await fetch(`/api/pets/${id}`, { + method: "PUT", + headers: { + Accept: contentType, + "Content-Type": contentType, + }, + body: JSON.stringify(form), + }); + + // Throw error with status code in case Fetch API req failed + if (!res.ok) { + throw new Error(res.status); + } + + const { data } = await res.json(); + + mutate(`/api/pets/${id}`, data, false); // Update the local data without a revalidation + router.push("/"); + } catch (error) { + setMessage("Failed to update pet"); + } + }; + + /* The POST method adds a new entry in the mongodb database. */ + const postData = async (form) => { + try { + const res = await fetch("/api/pets", { + method: "POST", + headers: { + Accept: contentType, + "Content-Type": contentType, + }, + body: JSON.stringify(form), + }); + + // Throw error with status code in case Fetch API req failed + if (!res.ok) { + throw new Error(res.status); + } + + router.push("/"); + } catch (error) { + setMessage("Failed to add pet"); + } + }; + + const handleChange = (e) => { + const target = e.target; + const value = + target.name === "poddy_trained" ? target.checked : target.value; + const name = target.name; + + setForm({ + ...form, + [name]: value, + }); + }; + + const handleSubmit = (e) => { + e.preventDefault(); + const errs = formValidate(); + if (Object.keys(errs).length === 0) { + forNewPet ? postData(form) : putData(form); + } else { + setErrors({ errs }); + } + }; + + /* Makes sure pet info is filled for pet name, owner name, species, and image url*/ + const formValidate = () => { + let err = {}; + if (!form.name) err.name = "Name is required"; + if (!form.owner_name) err.owner_name = "Owner is required"; + if (!form.species) err.species = "Species is required"; + if (!form.image_url) err.image_url = "Image URL is required"; + return err; + }; + + return ( + <> +
+ + + + + + + + + + + + + + + + +