Skip to content

Commit 5533b22

Browse files
committed
Might resolve velopert#40
Success message will not show when autosaved
1 parent 1bade58 commit 5533b22

File tree

2 files changed

+78
-74
lines changed

2 files changed

+78
-74
lines changed

src/components/write/WriteFooter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const StyledButton = styled(Button)`
2323
`;
2424

2525
export interface WriteFooterProps {
26-
onTempSave: () => void;
26+
onTempSave: (notify?: boolean) => void;
2727
onPublish: () => void;
2828
edit: boolean;
2929
}
@@ -35,7 +35,7 @@ const WriteFooter: React.FC<WriteFooterProps> = ({
3535
}) => {
3636
return (
3737
<WriteFooterBlock>
38-
<StyledButton inline color="lightGray" onClick={onTempSave}>
38+
<StyledButton inline color="lightGray" onClick={() => onTempSave(true)}>
3939
임시저장
4040
</StyledButton>
4141
<StyledButton inline color="teal" onClick={onPublish}>

src/containers/write/MarkdownEditorContainer.tsx

Lines changed: 76 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -147,89 +147,93 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
147147
[s3Upload],
148148
);
149149

150-
const onTempSave = useCallback(async () => {
151-
if (!title || !markdown) {
152-
toast.error('제목 또는 내용이 비어있습니다.');
153-
return;
154-
}
150+
const onTempSave = useCallback(
151+
async (notify?: boolean) => {
152+
if (!title || !markdown) {
153+
toast.error('제목 또는 내용이 비어있습니다.');
154+
return;
155+
}
155156

156-
const notifySuccess = () => {
157-
toast.success('포스트가 임시저장되었습니다.');
158-
};
157+
const notifySuccess = () => {
158+
if (!notify) return;
159+
toast.success('포스트가 임시저장되었습니다.');
160+
};
159161

160-
if (!postId) {
161-
const response = await writePost({
162-
variables: {
163-
title,
164-
body: markdown,
165-
tags: [],
166-
is_markdown: true,
167-
is_temp: true,
168-
is_private: false,
169-
url_slug: escapeForUrl(title),
170-
thumbnail: null,
171-
meta: {},
172-
series_id: null,
173-
},
174-
});
175-
if (!response || !response.data) return;
176-
const { id } = response.data.writePost;
177-
dispatch(setWritePostId(id));
178-
history.replace(`/write?id=${id}`);
179-
notifySuccess();
180-
return;
181-
}
182-
// tempsaving unreleased post:
183-
if (isTemp) {
184-
await editPost({
162+
if (!postId) {
163+
const response = await writePost({
164+
variables: {
165+
title,
166+
body: markdown,
167+
tags: [],
168+
is_markdown: true,
169+
is_temp: true,
170+
is_private: false,
171+
url_slug: escapeForUrl(title),
172+
thumbnail: null,
173+
meta: {},
174+
series_id: null,
175+
},
176+
});
177+
if (!response || !response.data) return;
178+
const { id } = response.data.writePost;
179+
dispatch(setWritePostId(id));
180+
history.replace(`/write?id=${id}`);
181+
notifySuccess();
182+
return;
183+
}
184+
// tempsaving unreleased post:
185+
if (isTemp) {
186+
await editPost({
187+
variables: {
188+
id: postId,
189+
title,
190+
body: markdown,
191+
is_markdown: true,
192+
is_temp: true,
193+
is_private: false,
194+
url_slug: escapeForUrl(title),
195+
thumbnail: null,
196+
meta: {},
197+
series_id: null,
198+
tags: [],
199+
},
200+
});
201+
notifySuccess();
202+
return;
203+
}
204+
205+
// tempsaving released post:
206+
// save only if something has been changed
207+
if (shallowEqual(lastSavedData, { title, body: markdown })) {
208+
return;
209+
}
210+
await createPostHistory({
185211
variables: {
186-
id: postId,
212+
post_id: postId,
187213
title,
188214
body: markdown,
189215
is_markdown: true,
190-
is_temp: true,
191-
is_private: false,
192-
url_slug: escapeForUrl(title),
193-
thumbnail: null,
194-
meta: {},
195-
series_id: null,
196-
tags: [],
197216
},
198217
});
199-
notifySuccess();
200-
return;
201-
}
202-
203-
// tempsaving released post:
204-
// save only if something has been changed
205-
if (shallowEqual(lastSavedData, { title, body: markdown })) {
206-
return;
207-
}
208-
await createPostHistory({
209-
variables: {
210-
post_id: postId,
218+
setLastSavedData({
211219
title,
212220
body: markdown,
213-
is_markdown: true,
214-
},
215-
});
216-
setLastSavedData({
221+
});
222+
notifySuccess();
223+
},
224+
[
225+
createPostHistory,
226+
dispatch,
227+
editPost,
228+
history,
229+
isTemp,
230+
lastSavedData,
231+
markdown,
232+
postId,
217233
title,
218-
body: markdown,
219-
});
220-
notifySuccess();
221-
}, [
222-
createPostHistory,
223-
dispatch,
224-
editPost,
225-
history,
226-
isTemp,
227-
lastSavedData,
228-
markdown,
229-
postId,
230-
title,
231-
writePost,
232-
]);
234+
writePost,
235+
],
236+
);
233237

234238
useEffect(() => {
235239
const changed = !shallowEqual(lastSavedData, { title, body: markdown });

0 commit comments

Comments
 (0)