Skip to content

Commit 99775b8

Browse files
committed
fix: prevent duplicate submissions for write post
1 parent 20f23d2 commit 99775b8

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/containers/write/MarkdownEditorContainer.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,21 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
6161
tags,
6262
} = useSelector((state: RootState) => state.write);
6363
const uncachedClient = useUncachedApolloClient();
64-
const [writePost] = useMutation<WritePostResponse>(WRITE_POST, {
65-
client: uncachedClient,
66-
});
64+
const [writePost, { loading: writePostLoading }] =
65+
useMutation<WritePostResponse>(WRITE_POST, {
66+
client: uncachedClient,
67+
});
6768

6869
const bodyRef = useRef(initialBody);
6970
const titleRef = useRef(title);
7071
const [createPostHistory] =
7172
useMutation<CreatePostHistoryResponse>(CREATE_POST_HISTORY);
72-
const [editPost] = useMutation<EditPostResult>(EDIT_POST, {
73-
client: uncachedClient,
74-
});
73+
const [editPost, { loading: editPostLoading }] = useMutation<EditPostResult>(
74+
EDIT_POST,
75+
{
76+
client: uncachedClient,
77+
},
78+
);
7579

7680
const [lastSavedData, setLastSavedData] = useState({
7781
title: initialTitle,
@@ -148,6 +152,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
148152

149153
const onTempSave = useCallback(
150154
async (notify?: boolean) => {
155+
if (writePostLoading || editPostLoading) return;
151156
if (!title || !markdown) {
152157
toast.error('제목 또는 내용이 비어있습니다.');
153158
return;
@@ -233,6 +238,8 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
233238
tags,
234239
title,
235240
writePost,
241+
writePostLoading,
242+
editPostLoading,
236243
],
237244
);
238245

@@ -244,6 +251,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
244251
const uploadWithPostId = useCallback(
245252
async (file: File) => {
246253
if (!file) return;
254+
if (writePostLoading || editPostLoading) return;
247255
let id = postIdRef.current;
248256
if (!id) {
249257
const title = titleRef.current || 'Temp Title';
@@ -278,7 +286,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
278286
toast.error('이미지 업로드 실패! 잠시 후 다시 시도하세요.');
279287
});
280288
},
281-
[cfUpload, writePost, dispatch, history],
289+
[cfUpload, writePost, dispatch, history, writePostLoading, editPostLoading],
282290
);
283291

284292
const onDragDropUpload = useCallback(

src/containers/write/PublishActionButtonsContainer.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ const PublishActionButtonsContainer: React.FC<
5959

6060
const uncachedClient = useUncachedApolloClient();
6161

62-
const [writePost] = useMutation<WritePostResponse>(WRITE_POST, {
63-
client: uncachedClient,
64-
});
65-
const [editPost] = useMutation<EditPostResult>(EDIT_POST, {
66-
client: uncachedClient,
67-
});
62+
const [writePost, { loading: writePostLoading }] =
63+
useMutation<WritePostResponse>(WRITE_POST, {
64+
client: uncachedClient,
65+
});
66+
const [editPost, { loading: editPostLoading }] = useMutation<EditPostResult>(
67+
EDIT_POST,
68+
{
69+
client: uncachedClient,
70+
},
71+
);
6872

6973
const variables = {
7074
title: options.title,
@@ -86,6 +90,7 @@ const PublishActionButtonsContainer: React.FC<
8690
};
8791

8892
const onPublish = async () => {
93+
if (writePostLoading || editPostLoading) return;
8994
if (options.title.trim() === '') {
9095
toast.error('제목이 비어있습니다.');
9196
return;
@@ -104,6 +109,7 @@ const PublishActionButtonsContainer: React.FC<
104109
};
105110

106111
const onEdit = async () => {
112+
if (editPostLoading) return;
107113
const response = await editPost({
108114
variables: {
109115
id: options.postId,

src/containers/write/PublishPreviewContainer.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ const PublishPreviewContainer: React.FC<PublishPreviewContainerProps> = ({
4747
[changeDescription],
4848
);
4949
const uncachedClient = useUncachedApolloClient();
50-
const [writePost] = useMutation<WritePostResponse>(WRITE_POST, {
51-
client: uncachedClient,
52-
});
50+
const [writePost, { loading: writePostLoading }] =
51+
useMutation<WritePostResponse>(WRITE_POST, {
52+
client: uncachedClient,
53+
});
5354
const [upload, file] = useUpload();
5455
const { upload: cfUpload, image } = useCFUpload();
5556

@@ -103,6 +104,7 @@ const PublishPreviewContainer: React.FC<PublishPreviewContainerProps> = ({
103104

104105
const uploadWithPostId = useCallback(
105106
async (file: File) => {
107+
if (!file) return;
106108
const id = await getValidPostId();
107109
if (!id) return;
108110
cfUpload(file, { type: 'post', refId: id });
@@ -112,9 +114,10 @@ const PublishPreviewContainer: React.FC<PublishPreviewContainerProps> = ({
112114

113115
useEffect(() => {
114116
if (!file) return;
117+
if (writePostLoading) return;
115118
uploadWithPostId(file);
116119
// eslint-disable-next-line react-hooks/exhaustive-deps
117-
}, [file]);
120+
}, [file, writePostLoading]);
118121

119122
useEffect(() => {
120123
if (!image) return;

0 commit comments

Comments
 (0)