Skip to content

Commit

Permalink
Merge pull request #567 from velopert/fix/temp-save
Browse files Browse the repository at this point in the history
fix: improve temp save functionality
  • Loading branch information
winverse authored Aug 6, 2024
2 parents 681402b + e54b5da commit a45be44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/components/write/WriteMarkdownEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,6 @@ const checker = {
codepen: (text: string) => {
const regex = /^<iframe.*src="https:\/\/codepen.io\/(.*?)".*/;
const result = regex.exec(text);
console.log(result);
if (!result) return null;
return result[1];
},
Expand Down
40 changes: 20 additions & 20 deletions src/containers/write/MarkdownEditorContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
tags,
} = useSelector((state: RootState) => state.write);
const uncachedClient = useUncachedApolloClient();
const [writePost, { loading: writePostLoading }] =
const [writePost, { loading: isWritePostLoading }] =
useMutation<WritePostResponse>(WRITE_POST, {
client: uncachedClient,
});
Expand All @@ -70,12 +70,10 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
const titleRef = useRef(title);
const [createPostHistory] =
useMutation<CreatePostHistoryResponse>(CREATE_POST_HISTORY);
const [editPost, { loading: editPostLoading }] = useMutation<EditPostResult>(
EDIT_POST,
{
const [editPost, { loading: isEditPostLoading }] =
useMutation<EditPostResult>(EDIT_POST, {
client: uncachedClient,
},
);
});

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

const onTempSave = useCallback(
async (notify?: boolean) => {
if (writePostLoading || editPostLoading) return;
console.log('onTempSave');

if (isWritePostLoading || isEditPostLoading) return;
if (!title || !markdown) {
toast.error('제목 또는 내용이 비어있습니다.');
return;
Expand All @@ -164,6 +164,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
};

if (!postId) {
console.log('writePost');
const response = await writePost({
variables: {
title,
Expand Down Expand Up @@ -206,7 +207,6 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
},
});
notifySuccess();
return;
}

// tempsaving released post:
Expand Down Expand Up @@ -243,8 +243,8 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
tags,
title,
writePost,
writePostLoading,
editPostLoading,
isWritePostLoading,
isEditPostLoading,
],
);

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

useEffect(() => {
const changed = !shallowEqual(lastSavedData, { title, body: markdown });
if (changed) {
const timeoutId = setTimeout(() => {
if (!postId && !title && markdown.length < 30) return;
onTempSave(true);
}, 10 * 1000);

return () => {
clearTimeout(timeoutId);
};
}
if (!changed) return;

const timeoutId = setTimeout(() => {
if (!postId && !title && markdown.length < 30) return;
onTempSave(true);
}, 1000 * 10);

return () => {
clearTimeout(timeoutId);
};
}, [title, postId, onTempSave, lastSavedData, markdown]);

useSaveHotKey(() => onTempSave(true));
Expand Down

0 comments on commit a45be44

Please sign in to comment.