diff --git a/src/pages/posts/[slug].jsx b/src/pages/posts/[slug].jsx index b71f258..71870a9 100644 --- a/src/pages/posts/[slug].jsx +++ b/src/pages/posts/[slug].jsx @@ -27,12 +27,12 @@ export default function Post({ post }) { author: 'Spencer Sharp', date: post.createdDate, title: post.title, - description: post.body, + description: post.description, } return ( - {post.body} + {post.description} ) } diff --git a/src/pages/posts/index.jsx b/src/pages/posts/index.jsx index c0daf46..3a9d22d 100644 --- a/src/pages/posts/index.jsx +++ b/src/pages/posts/index.jsx @@ -22,7 +22,7 @@ function Post({ post }) { > {formatDate(date)} - {post.body} + {post.description} Read post ({})); + let slug = data?.post?.slug; + + if (!slug){ + const loc = res.headers.get("Location"); // "/posts/hello-world" + if (loc) slug = loc.split("/").pop(); + } + + if (!slug) return router.push("/posts"); + + return router.push(`/posts/${slug}`); + } + + if (res.status === 400) { + const data = await res.json(); + setErrors(Array.isArray(data?.errors) ? data.errors : ["Validation failed."]); + return; + } + + setErrors([`Unexpected error: ${res.status}`]); + } catch (err) { + setErrors([`Network error. Is the API running?`]) + } finally { + setSubmitting(false); + } + } + + return ( +
+

Create a Post

+ + {errors.length > 0 && ( + + )} + +
+ + + + + +
+
+ ); +}