Skip to content

Commit a45be44

Browse files
authored
Merge pull request #567 from velopert/fix/temp-save
fix: improve temp save functionality
2 parents 681402b + e54b5da commit a45be44

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/components/write/WriteMarkdownEditor.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,6 @@ const checker = {
11291129
codepen: (text: string) => {
11301130
const regex = /^<iframe.*src="https:\/\/codepen.io\/(.*?)".*/;
11311131
const result = regex.exec(text);
1132-
console.log(result);
11331132
if (!result) return null;
11341133
return result[1];
11351134
},

src/containers/write/MarkdownEditorContainer.tsx

+20-20
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
6161
tags,
6262
} = useSelector((state: RootState) => state.write);
6363
const uncachedClient = useUncachedApolloClient();
64-
const [writePost, { loading: writePostLoading }] =
64+
const [writePost, { loading: isWritePostLoading }] =
6565
useMutation<WritePostResponse>(WRITE_POST, {
6666
client: uncachedClient,
6767
});
@@ -70,12 +70,10 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
7070
const titleRef = useRef(title);
7171
const [createPostHistory] =
7272
useMutation<CreatePostHistoryResponse>(CREATE_POST_HISTORY);
73-
const [editPost, { loading: editPostLoading }] = useMutation<EditPostResult>(
74-
EDIT_POST,
75-
{
73+
const [editPost, { loading: isEditPostLoading }] =
74+
useMutation<EditPostResult>(EDIT_POST, {
7675
client: uncachedClient,
77-
},
78-
);
76+
});
7977

8078
const [lastSavedData, setLastSavedData] = useState({
8179
title: initialTitle,
@@ -152,7 +150,9 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
152150

153151
const onTempSave = useCallback(
154152
async (notify?: boolean) => {
155-
if (writePostLoading || editPostLoading) return;
153+
console.log('onTempSave');
154+
155+
if (isWritePostLoading || isEditPostLoading) return;
156156
if (!title || !markdown) {
157157
toast.error('제목 또는 내용이 비어있습니다.');
158158
return;
@@ -164,6 +164,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
164164
};
165165

166166
if (!postId) {
167+
console.log('writePost');
167168
const response = await writePost({
168169
variables: {
169170
title,
@@ -206,7 +207,6 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
206207
},
207208
});
208209
notifySuccess();
209-
return;
210210
}
211211

212212
// tempsaving released post:
@@ -243,8 +243,8 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
243243
tags,
244244
title,
245245
writePost,
246-
writePostLoading,
247-
editPostLoading,
246+
isWritePostLoading,
247+
isEditPostLoading,
248248
],
249249
);
250250

@@ -308,16 +308,16 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
308308

309309
useEffect(() => {
310310
const changed = !shallowEqual(lastSavedData, { title, body: markdown });
311-
if (changed) {
312-
const timeoutId = setTimeout(() => {
313-
if (!postId && !title && markdown.length < 30) return;
314-
onTempSave(true);
315-
}, 10 * 1000);
316-
317-
return () => {
318-
clearTimeout(timeoutId);
319-
};
320-
}
311+
if (!changed) return;
312+
313+
const timeoutId = setTimeout(() => {
314+
if (!postId && !title && markdown.length < 30) return;
315+
onTempSave(true);
316+
}, 1000 * 10);
317+
318+
return () => {
319+
clearTimeout(timeoutId);
320+
};
321321
}, [title, postId, onTempSave, lastSavedData, markdown]);
322322

323323
useSaveHotKey(() => onTempSave(true));

0 commit comments

Comments
 (0)