Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DOTNET_SERVER_URL=http://localhost:5000
4 changes: 3 additions & 1 deletion src/api/axiosConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const AXIOS_BASE = axios.create({
baseURL: BASE_URL,
})

const JSON_CLIENT = isHyperlink(BASE_URL) ? AXIOS_BASE : false
// const JSON_CLIENT = isHyperlink(BASE_URL) ? AXIOS_BASE : false
const JSON_CLIENT = BASE_URL ? AXIOS_BASE : axios.create({ baseURL: 'http://localhost:5000' })


export default JSON_CLIENT
194 changes: 194 additions & 0 deletions src/pages/posts/new.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import { useState } from 'react'
import { useRouter } from 'next/router'
import Head from 'next/head'
import { SimpleLayout } from '@/components/SimpleLayout'
import { Container } from '@/components/Container'
import axios from '@/api/axiosConfig'

export default function CreatePostPage() {
const router = useRouter()

const [title, setTitle] = useState('')
const [description, setDescription] = useState('')
const [errors, setErrors] = useState([])

const handleSubmit = async (e) => {
e.preventDefault()
setErrors([])

// Custom client-side validation
if (!title.trim()) {
setErrors(['Title cannot be blank'])
return
}

try {
const response = await axios.post('/posts', { title, description })
const { post } = response.data
router.push(`/posts/${post.slug}`)
} catch (error) {
if (error.response?.data?.errors) {
setErrors(error.response.data.errors)
} else {
setErrors(['Something went wrong. Please try again.'])
}
}
}

return (
<>
<Head>
<title>Create Post - MyFirstBlog</title>
<meta name="description" content="Create a new blog post." />
</Head>

<SimpleLayout
title="Create a new post"
intro="Fill out the form below to create a new blog post."
>
<Container>
<form onSubmit={handleSubmit} className="space-y-6 max-w-xl">
{errors.length > 0 && (
<div className="rounded-md bg-red-100 p-4 text-red-700">
<ul className="list-disc list-inside">
{errors.map((err, idx) => (
<li key={idx}>{err}</li>
))}
</ul>
</div>
)}

<div>
<label className="block text-sm font-medium text-zinc-800">
Title
</label>
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
className="mt-1 block w-full rounded-md border border-zinc-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
/>
</div>

<div>
<label className="block text-sm font-medium text-zinc-800">
Description
</label>
<textarea
value={description}
onChange={(e) => setDescription(e.target.value)}
className="mt-1 block w-full rounded-md border border-zinc-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
rows="5"
/>
</div>

<button
type="submit"
className="inline-flex items-center px-4 py-2 bg-indigo-600 text-white text-sm font-medium rounded-md shadow-sm hover:bg-indigo-700 focus:outline-none"
>
Create Post
</button>
</form>
</Container>
</SimpleLayout>
</>
)
}








// import { useState } from 'react'
// import { useRouter } from 'next/router'
// import Head from 'next/head'
// import { SimpleLayout } from '@/components/SimpleLayout'
// import { Container } from '@/components/Container'
// import axios from '@/api/axiosConfig'

// export default function CreatePostPage() {
// const router = useRouter()

// const [title, setTitle] = useState('')
// const [description, setDescription] = useState('')
// const [errors, setErrors] = useState([])

// const handleSubmit = async (e) => {
// e.preventDefault()

// try {
// const response = await axios.post('/posts', { title, description })
// const { post } = response.data
// router.push(`/posts/${post.slug}`)
// } catch (error) {
// if (error.response?.data?.errors) {
// setErrors(error.response.data.errors)
// } else {
// setErrors(['Something went wrong. Please try again.'])
// }
// }
// }

// return (
// <>
// <Head>
// <title>Create Post - MyFirstBlog</title>
// <meta name="description" content="Create a new blog post." />
// </Head>

// <SimpleLayout
// title="Create a new post"
// intro="Fill out the form below to create a new blog post."
// >
// <Container>
// <form onSubmit={handleSubmit} className="space-y-6 max-w-xl">
// {errors.length > 0 && (
// <div className="rounded-md bg-red-100 p-4 text-red-700">
// <ul className="list-disc list-inside">
// {errors.map((err, idx) => (
// <li key={idx}>{err}</li>
// ))}
// </ul>
// </div>
// )}

// <div>
// <label className="block text-sm font-medium text-zinc-800">
// Title
// </label>
// <input
// type="text"
// value={title}
// onChange={(e) => setTitle(e.target.value)}
// className="mt-1 block w-full rounded-md border border-zinc-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
// required
// />
// </div>

// <div>
// <label className="block text-sm font-medium text-zinc-800">
// Description
// </label>
// <textarea
// value={description}
// onChange={(e) => setDescription(e.target.value)}
// className="mt-1 block w-full rounded-md border border-zinc-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
// rows="5"
// />
// </div>

// <button
// type="submit"
// className="inline-flex items-center px-4 py-2 bg-indigo-600 text-white text-sm font-medium rounded-md shadow-sm hover:bg-indigo-700 focus:outline-none"
// >
// Create Post
// </button>
// </form>
// </Container>
// </SimpleLayout>
// </>
// )
// }