Skip to content

Commit 032ad4b

Browse files
committed
save comment input on session storage to persist on reload or page navigation
1 parent 61cb20c commit 032ad4b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

frontend/src/components/comments/commentInput.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, useEffect, useState } from 'react';
1+
import { useRef, useEffect, useState, useCallback } from 'react';
22
import { useSelector } from 'react-redux';
33
import MDEditor from '@uiw/react-md-editor';
44
import Tribute from 'tributejs';
@@ -21,6 +21,7 @@ import { CurrentUserAvatar } from '../user/avatar';
2121
const maxFileSize = 1 * 1024 * 1024; // 1MB
2222

2323
function CommentInputField({
24+
sessionkey,
2425
comment,
2526
setComment,
2627
contributors,
@@ -130,6 +131,23 @@ function CommentInputField({
130131
});
131132
};
132133

134+
useEffect(() => {
135+
if (!sessionkey) return;
136+
const commenEvent = sessionStorage.getItem(sessionkey);
137+
if (commenEvent) {
138+
setComment(commenEvent);
139+
}
140+
}, [sessionkey, setComment]);
141+
142+
const onCommentChange = useCallback(
143+
(e) => {
144+
setComment(e);
145+
if (!sessionkey) return;
146+
sessionStorage.setItem(sessionkey, e);
147+
},
148+
[sessionkey, setComment],
149+
);
150+
133151
return (
134152
<div {...getRootProps()}>
135153
{isShowTabNavs && (
@@ -165,7 +183,7 @@ function CommentInputField({
165183
extraCommands={[]}
166184
height={200}
167185
value={comment}
168-
onChange={setComment}
186+
onChange={onCommentChange}
169187
textareaProps={{
170188
...getInputProps(),
171189
spellCheck: 'true',

frontend/src/components/projectDetail/questionsAndComments.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ export const PostProjectComment = ({ projectId, refetchComments, contributors })
2626
const token = useSelector((state) => state.auth.token);
2727
const locale = useSelector((state) => state.preferences['locale']);
2828
const [comment, setComment] = useState('');
29+
const SESSION_KEY = 'project-comment';
2930

3031
const mutation = useMutation({
3132
mutationFn: () => postProjectComment(projectId, comment, token, locale),
3233
onSuccess: () => {
3334
refetchComments();
3435
setComment('');
36+
sessionStorage.clear(SESSION_KEY);
3537
},
3638
});
3739

@@ -44,6 +46,7 @@ export const PostProjectComment = ({ projectId, refetchComments, contributors })
4446
<div className={`w-100 h-100`} style={{ position: 'relative', display: 'block' }}>
4547
<Suspense fallback={<ReactPlaceholder showLoadingAnimation={true} rows={13} delay={300} />}>
4648
<CommentInputField
49+
sessionkey={SESSION_KEY}
4750
comment={comment}
4851
setComment={setComment}
4952
enableHashtagPaste

0 commit comments

Comments
 (0)