fix(news): restore publication flow and deduplicate dashboard friends
This commit is contained in:
1 parent
ef94646d60
commit
9b0ea2fb16
22 files changed
+638
-195
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import type { ActionResult } from "./safe-action-shared";
|
||||
export type ArticleSaveResult = ActionResult<{ redirectTo: string }>;
|
||||
export class ArticleInputError extends Error {
|
||||
constructor(
|
||||
public readonly code:
|
||||
| "titleRequired"
|
||||
| "titleTooLong"
|
||||
| "summaryTooLong"
|
||||
| "imageTooLong"
|
||||
| "invalidStatus"
|
||||
| "publishDateRequired",
|
||||
) {
|
||||
super(code);
|
||||
}
|
||||
}
|
||||
export function readArticleInput(form: FormData) {
|
||||
const text = (key: string) =>
|
||||
String(form.get(key) ?? "")
|
||||
.normalize("NFC")
|
||||
.trim();
|
||||
const title = text("title"),
|
||||
shortStory = text("shortStory"),
|
||||
fullStory = text("fullStory"),
|
||||
image = text("image");
|
||||
if (!title) throw new ArticleInputError("titleRequired");
|
||||
if (title.length > 255) throw new ArticleInputError("titleTooLong");
|
||||
if (shortStory.length > 255) throw new ArticleInputError("summaryTooLong");
|
||||
if (image.length > 255) throw new ArticleInputError("imageTooLong");
|
||||
const status = text("status") || "published";
|
||||
if (status !== "published" && status !== "draft" && status !== "scheduled")
|
||||
throw new ArticleInputError("invalidStatus");
|
||||
const publishAt = status === "scheduled" ? new Date(text("publishAt")) : null;
|
||||
if (publishAt && Number.isNaN(publishAt.getTime()))
|
||||
throw new ArticleInputError("publishDateRequired");
|
||||
return {
|
||||
title,
|
||||
shortStory,
|
||||
fullStory,
|
||||
image,
|
||||
status,
|
||||
publishAt,
|
||||
rawSlug: text("slug"),
|
||||
};
|
||||
}
|
||||
export function articleSlug(value: string | string[]): string {
|
||||
return Array.isArray(value) ? value.join("/") : value;
|
||||
}
|
||||
Reference in new issue
Block a user