diff --git a/src/api/axiosConfig.js b/src/api/axiosConfig.js index bd45cb2..2e72c74 100644 --- a/src/api/axiosConfig.js +++ b/src/api/axiosConfig.js @@ -1,12 +1,17 @@ import axios from "axios" import { isHyperlink } from '@/lib/isHyperlink' +//const BASE_URL = process.env.DOTNET_SERVER_URL -const BASE_URL = process.env.DOTNET_SERVER_URL +// Use NEXT_PUBLIC_DOTNET_SERVER_URL for client-side access +const BASE_URL = process.env.NEXT_PUBLIC_DOTNET_SERVER_URL const AXIOS_BASE = axios.create({ - baseURL: BASE_URL, - }) + baseURL: BASE_URL, + headers: { + 'Content-Type': 'application/json', + }, +}) -const JSON_CLIENT = isHyperlink(BASE_URL) ? AXIOS_BASE : false +const JSON_CLIENT = isHyperlink(BASE_URL) ? AXIOS_BASE : axios export default JSON_CLIENT diff --git a/src/api/postsApi.js b/src/api/postsApi.js index 54f8964..3372baf 100644 --- a/src/api/postsApi.js +++ b/src/api/postsApi.js @@ -1,23 +1,36 @@ import API from './axiosConfig' -export const getPosts = () => { +export const getPosts = async () => { + console.log('Posting to URL:', API.defaults.baseURL) try { - return API.get('/posts/') - .then((res) => res.data) - } - catch (e) { + const res = await API.get('/posts/') + return res.data + } catch (e) { console.error(e) return [] } } -export const getPost = (postSlug) => { +export const getPost = async (postSlug) => { + console.log('Posting to URL:', API.defaults.baseURL) try { - return API.get(`/posts/${postSlug}`) - .then((res) => res.data) - } - catch (e) { + const res = await API.get(`/posts/${postSlug}`) + return res.data + } catch (e) { console.error(e) return {} } } +// Added to handle post creation +export const createPost = async (post) => { +console.log("Posting to:", API.defaults.baseURL + '/posts') + + console.log('Creating post:', post) + try { + const res = await API.post('/posts', post) + return res.data + } catch (e) { + console.error('Error creating post:', e) + throw e + } +} diff --git a/src/pages/posts/index.jsx b/src/pages/posts/index.jsx index c0daf46..194efae 100644 --- a/src/pages/posts/index.jsx +++ b/src/pages/posts/index.jsx @@ -1,9 +1,10 @@ import Head from 'next/head' +import Link from 'next/link' import { Card } from '@/components/Card' import { SimpleLayout } from '@/components/SimpleLayout' import { formatDate } from '@/lib/formatDate' -import { getPosts, getPost } from "@/api/postsApi" +import { getPosts } from "@/api/postsApi" function Post({ post }) { const date = new Date(post.createdDate) @@ -51,11 +52,20 @@ export default function PostsIndex({ posts }) { intro="All of my long-form thoughts on programming, leadership, product design, and more, collected in chronological order." >
+
{posts.map((post) => ( ))}
+
+ + Create Post + +
@@ -63,9 +73,12 @@ export default function PostsIndex({ posts }) { } export async function getStaticProps() { + const posts = await getPosts() || [] + console.log("Fetched posts:", posts) + return { props: { - posts: (await getPosts() || []), + posts, }, } } diff --git a/src/pages/posts/new.jsx b/src/pages/posts/new.jsx new file mode 100644 index 0000000..e166d02 --- /dev/null +++ b/src/pages/posts/new.jsx @@ -0,0 +1,88 @@ +import Head from 'next/head' +import { useState } from 'react' +import { useRouter } from 'next/router' + +import { Card } from '@/components/Card' +import { SimpleLayout } from '@/components/SimpleLayout' + +import { createPost } from '@/api/postsApi' + +export default function CreatePostPage() { + const router = useRouter() + + const [title, setTitle] = useState('') + const [body, setBody] = useState('') + const [loading, setLoading] = useState(false) + const [error, setError] = useState(null) + + const handleSubmit = async (e) => { + e.preventDefault() + setLoading(true) + setError(null) + console.log('Creating post:', { title, body }) + try { + const newPost = await createPost({ title, body }) + if (newPost?.slug) { + await router.push(`/posts/${newPost.slug}`) + } else { + throw new Error('No slug returned from API') + } + } catch (err) { + console.error(err) + setError("Failed to create post") + } finally { + setLoading(false) + } + } + + return ( + <> + + Create Post - Spencer Sharp + + + + +
+ +
+
+ + setTitle(e.target.value)} + className="mt-1 block w-full rounded-md border border-zinc-300 px-3 py-2 text-base shadow-sm focus:border-indigo-500 focus:ring-indigo-500" + required + /> +
+ +
+ +