Skip to content

Commit b3c40f8

Browse files
Merge pull request #7 from osama-22/fix/262_disable_save_button
fix: disabled button on save and before any changes, handled errors
2 parents 8fa13cf + 2fd6ddf commit b3c40f8

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

components/editor.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ interface EditorProps {
2424
type FormData = z.infer<typeof postPatchSchema>
2525

2626
export function Editor({ post }: EditorProps) {
27-
const { register, handleSubmit } = useForm<FormData>({
27+
const { register, handleSubmit, formState: {errors, isSubmitting, isDirty} } = useForm<FormData>({
2828
resolver: zodResolver(postPatchSchema),
2929
})
3030
const ref = React.useRef<EditorJS>()
3131
const router = useRouter()
32-
const [isSaving, setIsSaving] = React.useState<boolean>(false)
3332
const [isMounted, setIsMounted] = React.useState<boolean>(false)
33+
const [editorContentChanged, setEditorContentChanged] = React.useState<boolean>(false);
3434

3535
const initializeEditor = React.useCallback(async () => {
3636
const EditorJS = (await import("@editorjs/editorjs")).default
@@ -50,6 +50,9 @@ export function Editor({ post }: EditorProps) {
5050
onReady() {
5151
ref.current = editor
5252
},
53+
onChange: () => {
54+
setEditorContentChanged(true);
55+
},
5356
placeholder: "Type here to write your post...",
5457
inlineToolbar: true,
5558
data: body.content,
@@ -84,7 +87,6 @@ export function Editor({ post }: EditorProps) {
8487
}, [isMounted, initializeEditor])
8588

8689
async function onSubmit(data: FormData) {
87-
setIsSaving(true)
8890

8991
const blocks = await ref.current?.save()
9092

@@ -99,8 +101,6 @@ export function Editor({ post }: EditorProps) {
99101
}),
100102
})
101103

102-
setIsSaving(false)
103-
104104
if (!response?.ok) {
105105
return toast({
106106
title: "Something went wrong.",
@@ -138,8 +138,8 @@ export function Editor({ post }: EditorProps) {
138138
{post.published ? "Published" : "Draft"}
139139
</p>
140140
</div>
141-
<button type="submit" className={cn(buttonVariants())}>
142-
{isSaving && (
141+
<button disabled={isSubmitting || !(isDirty || editorContentChanged)} type="submit" className={cn(buttonVariants())}>
142+
{isSubmitting && (
143143
<Icons.spinner className="mr-2 h-4 w-4 animate-spin" />
144144
)}
145145
<span>Save</span>
@@ -154,6 +154,9 @@ export function Editor({ post }: EditorProps) {
154154
className="w-full resize-none appearance-none overflow-hidden bg-transparent text-5xl font-bold focus:outline-none"
155155
{...register("title")}
156156
/>
157+
{errors.title && (
158+
<p className="text-red-500">{`${errors.title.message}`}</p>
159+
)}
157160
<div id="editor" className="min-h-[500px]" />
158161
<p className="text-sm text-gray-500">
159162
Use{" "}

lib/validations/post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as z from "zod"
22

33
export const postPatchSchema = z.object({
4-
title: z.string().min(3).max(128).optional(),
4+
title: z.string().min(3, "Title must be at least 3 characters long").max(128).optional(),
55

66
// TODO: Type this properly from editorjs block types?
77
content: z.any().optional(),

0 commit comments

Comments
 (0)