119 lines
4.1 KiB
TypeScript
119 lines
4.1 KiB
TypeScript
"use client"
|
|
|
|
import { addNewsletter, } from "@/app/actions/NewsletterActions";
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation"
|
|
|
|
export default function CreateNewsletterForm() {
|
|
const [title, setTitle] = useState("");
|
|
const [imageUrl, setImageUrl] = useState("");
|
|
const [content, setContent] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
const [isCreated, setIsCreated] = useState(false);
|
|
|
|
const router = useRouter();
|
|
|
|
const handleSubmit = async (e: React.SubmitEvent<HTMLFormElement>) => {
|
|
e.preventDefault(); // stops default submission to send through the js instead
|
|
setError(""); // clear state
|
|
setIsSubmitting(true);
|
|
setIsCreated(false)
|
|
|
|
try {
|
|
if (
|
|
typeof title !== "string" ||
|
|
typeof imageUrl !== "string" ||
|
|
typeof content !== "string" ||
|
|
!title.trim() ||
|
|
!content.trim()
|
|
) {
|
|
throw new Error("Please provide a title and content");
|
|
}
|
|
|
|
const result = await addNewsletter({
|
|
title,
|
|
imageUrl,
|
|
content,
|
|
});
|
|
|
|
if (!result.success) {
|
|
throw new Error(result.error);
|
|
} else {
|
|
setTitle("");
|
|
setContent("");
|
|
setImageUrl("");
|
|
setIsCreated(true)
|
|
|
|
// if (result.newsletter) { router.push(`/dashboard/${result.newsletter.id}`); };
|
|
}
|
|
} catch {
|
|
setError("Unable to create newsletter. Please try again.")
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} className="mt-4">
|
|
{error && (
|
|
<div className="alert alert-error mb-4">
|
|
<span>{error}</span>
|
|
</div>
|
|
)}
|
|
{isCreated && ( // display if redirect with search param
|
|
<div role="status" className="alert alert-success mb-4">
|
|
Newsletter sucessfully created
|
|
</div>
|
|
)}
|
|
{" "}
|
|
<div className="form-control">
|
|
<label className="label">
|
|
<span className="label-text">Title</span>
|
|
</label>
|
|
<input
|
|
type="text"
|
|
placeholder="Enter your title"
|
|
className="input input-bordered w-full"
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="form-control mt-4">
|
|
<label className="label">
|
|
<span className="label-text">Image URL</span>
|
|
</label>
|
|
<input
|
|
type="url"
|
|
placeholder="Enter your image URL"
|
|
className="input input-bordered w-full"
|
|
value={imageUrl}
|
|
onChange={(e) => setImageUrl(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="form-control mt-4">
|
|
<label className="label">
|
|
<span className="label-text">Content</span>
|
|
</label>
|
|
<textarea
|
|
placeholder="Enter your content"
|
|
className="textarea textarea-bordered w-full"
|
|
value={content}
|
|
onChange={(e) => setContent(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="form-control mt-6">
|
|
<button type="submit" className="btn btn-primary w-full" disabled={isSubmitting}>
|
|
{isSubmitting ? (
|
|
<span className="loading loading-spinner loading-sm"></span>
|
|
) : (
|
|
"Create Newsletter"
|
|
)}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)
|
|
} |