Skip to content

Commit

Permalink
fix: prevented on save memo endpoint request when first loaded memo
Browse files Browse the repository at this point in the history
  • Loading branch information
vattcarter7 committed Mar 23, 2024
1 parent a02065f commit 3379bcc
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/components/tiptap-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const TipTapEditor = ({ memo }: Props) => {
const [content, setContentState] = useState<string>(
memo.content || `<h1>${memo.name}</h1>`
);
const [initialLoaded, setInitialLoaded] = useState<boolean>(true);

const saveMemo = useMutation({
mutationFn: async () => {
Expand All @@ -34,6 +35,7 @@ const TipTapEditor = ({ memo }: Props) => {
extensions: [StarterKit],
content: content,
onUpdate: ({ editor }) => {
setInitialLoaded(false);
setContentState(editor.getHTML());
},
});
Expand All @@ -42,19 +44,19 @@ const TipTapEditor = ({ memo }: Props) => {

useEffect(() => {
// save to db
if (debouncedContent === '') return;
if (debouncedContent === '' || initialLoaded) return;
saveMemo.mutate(undefined, {
onSuccess: (data) => {
console.log('success update!', data);
},
onError: (err) => {
console.error(err);
window.alert("something went wrong")
window.alert('something went wrong');
},
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedContent]);
}, [debouncedContent, initialLoaded]);

return (
<>
<div className="flex">
Expand Down

0 comments on commit 3379bcc

Please sign in to comment.