| 84 | }, [isMounted, initializeEditor]) |
| 85 | |
| 86 | async function onSubmit(data: FormData) { |
| 87 | setIsSaving(true) |
| 88 | |
| 89 | const blocks = await ref.current?.save() |
| 90 | |
| 91 | const response = await fetch(`/api/posts/${post.id}`, { |
| 92 | method: "PATCH", |
| 93 | headers: { |
| 94 | "Content-Type": "application/json", |
| 95 | }, |
| 96 | body: JSON.stringify({ |
| 97 | title: data.title, |
| 98 | content: blocks, |
| 99 | }), |
| 100 | }) |
| 101 | |
| 102 | setIsSaving(false) |
| 103 | |
| 104 | if (!response?.ok) { |
| 105 | return toast({ |
| 106 | title: "Something went wrong.", |
| 107 | description: "Your post was not saved. Please try again.", |
| 108 | variant: "destructive", |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | router.refresh() |
| 113 | |
| 114 | return toast({ |
| 115 | description: "Your post has been saved.", |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | if (!isMounted) { |
| 120 | return null |