mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
## Description This PR ensures that the default prettier config is used for examples and templates. This config is compatible with `prettier@3` as well (upgrading prettier is bigger change that can be a future PR). ## Changes - Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will be needed upgrading to prettier@3) - Added `examples/.prettierrc.json` with default config (this will change every example) - Added `packages/create-next-app/templates/.prettierrc.json` with default config (this will change every template) ## Related - Fixes #54402 - Closes #54409
106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import { useState } from "react";
|
|
import { useRouter } from "next/router";
|
|
import Link from "next/link";
|
|
import dbConnect from "../../lib/dbConnect";
|
|
import Pet, { Pets } from "../../models/Pet";
|
|
import { GetServerSideProps, GetServerSidePropsContext } from "next";
|
|
import { ParsedUrlQuery } from "querystring";
|
|
|
|
interface Params extends ParsedUrlQuery {
|
|
id: string;
|
|
}
|
|
|
|
type Props = {
|
|
pet: Pets;
|
|
};
|
|
|
|
/* Allows you to view pet card info and delete pet card*/
|
|
const PetPage = ({ pet }: Props) => {
|
|
const router = useRouter();
|
|
const [message, setMessage] = useState("");
|
|
const handleDelete = async () => {
|
|
const petID = router.query.id;
|
|
|
|
try {
|
|
await fetch(`/api/pets/${petID}`, {
|
|
method: "Delete",
|
|
});
|
|
router.push("/");
|
|
} catch (error) {
|
|
setMessage("Failed to delete the pet.");
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div key={pet._id}>
|
|
<div className="card">
|
|
<img src={pet.image_url} />
|
|
<h5 className="pet-name">{pet.name}</h5>
|
|
<div className="main-content">
|
|
<p className="pet-name">{pet.name}</p>
|
|
<p className="owner">Owner: {pet.owner_name}</p>
|
|
|
|
{/* Extra Pet Info: Likes and Dislikes */}
|
|
<div className="likes info">
|
|
<p className="label">Likes</p>
|
|
<ul>
|
|
{pet.likes.map((data, index) => (
|
|
<li key={index}>{data} </li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
<div className="dislikes info">
|
|
<p className="label">Dislikes</p>
|
|
<ul>
|
|
{pet.dislikes.map((data, index) => (
|
|
<li key={index}>{data} </li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
|
|
<div className="btn-container">
|
|
<Link href={`/${pet._id}/edit`}>
|
|
<button className="btn edit">Edit</button>
|
|
</Link>
|
|
<button className="btn delete" onClick={handleDelete}>
|
|
Delete
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{message && <p>{message}</p>}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export const getServerSideProps: GetServerSideProps<Props, Params> = async ({
|
|
params,
|
|
}: GetServerSidePropsContext) => {
|
|
await dbConnect();
|
|
|
|
if (!params?.id) {
|
|
return {
|
|
notFound: true,
|
|
};
|
|
}
|
|
|
|
const pet = await Pet.findById(params.id).lean();
|
|
|
|
if (!pet) {
|
|
return {
|
|
notFound: true,
|
|
};
|
|
}
|
|
|
|
/* Ensures all objectIds and nested objectIds are serialized as JSON data */
|
|
const serializedPet = JSON.parse(JSON.stringify(pet));
|
|
|
|
return {
|
|
props: {
|
|
pet: serializedPet,
|
|
},
|
|
};
|
|
};
|
|
|
|
export default PetPage;
|