Skip to content

Commit 27346cf

Browse files
committed
Enhance ArticleEditor and ArticleRepositoryInput for improved article updates. Add detailed comments for clarity on optional fields and update canonical URL handling to support null values. Refactor debounced handle generation to trigger updates on article save, ensuring better state management.
1 parent b8cabe3 commit 27346cf

File tree

3 files changed

+43
-120
lines changed

3 files changed

+43
-120
lines changed

src/backend/services/inputs/article.input.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,40 @@ export const ArticleRepositoryInput = {
4646
is_published: z.boolean().optional(),
4747
}),
4848
updateMyArticleInput: z.object({
49-
article_id: z.string(),
49+
article_id: z.string(), // Required, unique identifier for the article
50+
51+
// Optional fields for updating
5052
title: z.string().optional(),
5153
handle: z.string().optional(),
5254
excerpt: z.string().optional(),
5355
body: z.string().optional(),
56+
57+
// Optional nested object for cover image
5458
cover_image: z
5559
.object({
5660
key: z.string(),
5761
provider: z.enum(["cloudinary", "direct"]),
5862
alt: z.string().optional(),
5963
})
6064
.optional(),
65+
66+
// Optional boolean flag for publish status
6167
is_published: z.boolean().optional(),
68+
69+
// Optional metadata object
6270
metadata: z
6371
.object({
6472
seo: z
6573
.object({
6674
title: z.string().optional(),
6775
description: z.string().optional(),
6876
keywords: z.array(z.string()).optional(),
69-
canonical_url: z.string().url().optional(),
77+
// canonical_url: z.string().url().nullable().optional(),
78+
canonical_url: z
79+
.union([z.string().url(), z.literal(""), z.null()])
80+
.optional()
81+
.nullable()
82+
.transform((val) => (val === "" ? null : val)),
7083
})
7184
.nullable()
7285
.optional(),

src/components/Editor/ArticleEditorDrawer.tsx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ const ArticleEditorDrawer: React.FC<Props> = ({ article, open, onClose }) => {
3939
const session = useSession();
4040
const { _t } = useTranslation();
4141
const router = useRouter();
42-
const setDebounceHandler = useDebouncedCallback(async (slug: string) => {
43-
const handle = await articleActions.getUniqueArticleHandle(slug);
44-
form.setValue("handle", handle);
45-
}, 2000);
4642
const updateMyArticleMutation = useMutation({
4743
mutationFn: (
4844
input: z.infer<typeof ArticleRepositoryInput.updateMyArticleInput>
@@ -57,13 +53,21 @@ const ArticleEditorDrawer: React.FC<Props> = ({ article, open, onClose }) => {
5753
},
5854
});
5955

60-
const [tags, setTags] = React.useState<string[]>([]);
56+
const setDebounceHandler = useDebouncedCallback(async (slug: string) => {
57+
const handle = await articleActions.getUniqueArticleHandle(slug);
58+
form.setValue("handle", handle);
59+
updateMyArticleMutation.mutate({
60+
article_id: article?.id ?? "",
61+
handle: handle,
62+
});
63+
}, 2000);
6164

6265
const form = useForm<
6366
z.infer<typeof ArticleRepositoryInput.updateMyArticleInput>
6467
>({
6568
defaultValues: {
6669
article_id: article.id,
70+
title: article?.title ?? "",
6771
handle: article?.handle ?? "",
6872
excerpt: article?.excerpt ?? "",
6973
metadata: {
@@ -95,7 +99,7 @@ const ArticleEditorDrawer: React.FC<Props> = ({ article, open, onClose }) => {
9599
},
96100
});
97101
};
98-
//className="m-3 h-[100vh-20px] w-[100vw-20px]"
102+
99103
return (
100104
<Sheet open={open} onOpenChange={onClose}>
101105
<SheetContent>
@@ -127,7 +131,7 @@ const ArticleEditorDrawer: React.FC<Props> = ({ article, open, onClose }) => {
127131
/>
128132
</FormControl>
129133
<FormDescription className="-mt-1">
130-
https://www.techdiary.dev/{session?.user?.username}/
134+
https://www.techdiary.dev/@{session?.user?.username}/
131135
{form.watch("handle")}
132136
</FormDescription>
133137
<FormMessage />
@@ -170,13 +174,28 @@ const ArticleEditorDrawer: React.FC<Props> = ({ article, open, onClose }) => {
170174
click-through-rates.
171175
</FormDescription>
172176
<FormControl>
173-
<Input {...field} placeholder={form.watch("handle")} />
177+
<Input {...field} placeholder={form.watch("title")} />
174178
</FormControl>
175179
<FormMessage />
176180
</FormItem>
177181
)}
178182
/>
179183

184+
{/* <FormField
185+
control={form.control}
186+
name="metadata.seo.canonical_url"
187+
render={({ field }) => (
188+
<FormItem>
189+
<FormLabel>{_t("Are you republishing")}?</FormLabel>
190+
<FormDescription className="text-xs"></FormDescription>
191+
<FormControl>
192+
<Input {...field} />
193+
</FormControl>
194+
<FormMessage />
195+
</FormItem>
196+
)}
197+
/> */}
198+
180199
<FormField
181200
control={form.control}
182201
name="metadata.seo.keywords"
@@ -253,7 +272,7 @@ const ArticleEditorDrawer: React.FC<Props> = ({ article, open, onClose }) => {
253272
/> */}
254273
</div>
255274

256-
<Button>{_t("Save")}</Button>
275+
<Button type="submit">{_t("Save")}</Button>
257276
</form>
258277
</Form>
259278
</div>

src/components/ui/auto.tsx

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)