From cbc4f9a20d3b43e0e1eb0ea56d99e63c7dc2ec68 Mon Sep 17 00:00:00 2001 From: AMAllen15 Date: Sun, 11 Aug 2024 18:04:24 -0600 Subject: [PATCH 1/2] Npm install --- package-lock.json | 12 ++++++------ package.json | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1e1c817..5ca3a9c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "@mapbox/rehype-prism": "^0.8.0", "@mdx-js/loader": "^2.1.5", "@mdx-js/react": "^2.1.5", - "@next/mdx": "^13.0.2", + "@next/mdx": "^13.5.6", "@tailwindcss/typography": "^0.5.4", "autoprefixer": "^10.4.12", "axios": "^1.2.0", @@ -23,8 +23,8 @@ "focus-visible": "^5.2.0", "next": "^13.4.12", "postcss-focus-visible": "^9.0.0", - "react": "18.2.0", - "react-dom": "18.2.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", "remark-gfm": "^3.0.1", "tailwindcss": "^3.2.1" }, @@ -380,9 +380,9 @@ } }, "node_modules/@next/mdx": { - "version": "13.4.12", - "resolved": "https://registry.npmjs.org/@next/mdx/-/mdx-13.4.12.tgz", - "integrity": "sha512-5/MHP/aVBD+wfTRAsZuxWI9h0JOn9n/ygCSy6SBZtaWHT3QZg+r1G4iYj5152uYVMHCD1rk9CfJAvA24LeyT1w==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/mdx/-/mdx-13.5.6.tgz", + "integrity": "sha512-2AMyCrz1SxSWNUpADyLz3RbPbq0GHrchbO7Msvg7IsH8MrTw3VYaZSI1KNa6JzZIoykwtNVSEL+uBmPZi106Jw==", "dependencies": { "source-map": "^0.7.0" }, diff --git a/package.json b/package.json index a26b950..56dc38a 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "@mapbox/rehype-prism": "^0.8.0", "@mdx-js/loader": "^2.1.5", "@mdx-js/react": "^2.1.5", - "@next/mdx": "^13.0.2", + "@next/mdx": "^13.5.6", "@tailwindcss/typography": "^0.5.4", "autoprefixer": "^10.4.12", "axios": "^1.2.0", @@ -25,8 +25,8 @@ "focus-visible": "^5.2.0", "next": "^13.4.12", "postcss-focus-visible": "^9.0.0", - "react": "18.2.0", - "react-dom": "18.2.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", "remark-gfm": "^3.0.1", "tailwindcss": "^3.2.1" }, From 3cc063f2fc0055815e982c3c05fd1eb478d3962d Mon Sep 17 00:00:00 2001 From: AMAllen15 Date: Mon, 12 Aug 2024 16:11:35 -0600 Subject: [PATCH 2/2] Creation of new post form and adding it to routing --- src/pages/posts/index.jsx | 2 + src/pages/posts/new.jsx | 85 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/pages/posts/new.jsx diff --git a/src/pages/posts/index.jsx b/src/pages/posts/index.jsx index c0daf46..27f77eb 100644 --- a/src/pages/posts/index.jsx +++ b/src/pages/posts/index.jsx @@ -4,6 +4,7 @@ import { Card } from '@/components/Card' import { SimpleLayout } from '@/components/SimpleLayout' import { formatDate } from '@/lib/formatDate' import { getPosts, getPost } from "@/api/postsApi" +import NewPost from './new' function Post({ post }) { const date = new Date(post.createdDate) @@ -57,6 +58,7 @@ export default function PostsIndex({ posts }) { ))} + ) diff --git a/src/pages/posts/new.jsx b/src/pages/posts/new.jsx new file mode 100644 index 0000000..8ab797b --- /dev/null +++ b/src/pages/posts/new.jsx @@ -0,0 +1,85 @@ +import { useState } from 'react'; +import { useRouter } from 'next/router'; + +export default function NewPost() { + const [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [errors, setErrors] = useState([]); + const router = useRouter(); + + const handleSubmit = async (e) => { + e.preventDefault(); + + try { + const res = await fetch('http://localhost:5000/posts', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ title, description }), + }); + + if (!res.ok) { + const errorText = await res.text(); + console.error("Server returned an error:", errorText); + + try { + const errorData = JSON.parse(errorText); + setErrors(errorData.errors || ["Unexpected error occurred"]); + } catch (parseError) { + setErrors([errorText]); + } + return; + } + + const data = await res.json(); + router.push(`/posts/${data.slug}`); + } catch (error) { + console.error("Network error or issue with fetch:", error); + setErrors(["Failed to create post. Please try again later."]); + } + }; + + return ( +
+

Create a New Post

+ {errors.length > 0 && ( +
    + {errors.map((error, index) => ( +
  • {error}
  • + ))} +
+ )} +
+
+ + setTitle(e.target.value)} + className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-teal-500 focus:border-teal-500 sm:text-sm" + placeholder="Enter the title" + /> +
+
+ + +
+
+ +
+
+
+ ); +} \ No newline at end of file