1
1
import React , {
2
- useCallback , useContext , useEffect , useRef ,
2
+ useCallback , useContext , useEffect , useRef , useState ,
3
3
} from 'react' ;
4
4
import PropTypes from 'prop-types' ;
5
5
@@ -22,7 +22,9 @@ import {
22
22
selectUserIsGroupTa ,
23
23
selectUserIsStaff ,
24
24
} from '../../../data/selectors' ;
25
- import { formikCompatibleHandler , isFormikFieldInvalid } from '../../../utils' ;
25
+ import { extractContent , formikCompatibleHandler , isFormikFieldInvalid } from '../../../utils' ;
26
+ import { useDraftContent } from '../../data/hooks' ;
27
+ import { setDraftComments , setDraftResponses } from '../../data/slices' ;
26
28
import { addComment , editComment } from '../../data/thunks' ;
27
29
import messages from '../../messages' ;
28
30
@@ -45,6 +47,8 @@ const CommentEditor = ({
45
47
const userIsStaff = useSelector ( selectUserIsStaff ) ;
46
48
const { editReasons } = useSelector ( selectModerationSettings ) ;
47
49
const [ submitting , dispatch ] = useDispatchWithState ( ) ;
50
+ const [ editorContent , setEditorContent ] = useState ( ) ;
51
+ const { addDraftContent, getDraftContent, removeDraftContent } = useDraftContent ( ) ;
48
52
49
53
const canDisplayEditReason = ( edit
50
54
&& ( userHasModerationPrivileges || userIsGroupTa || userIsStaff )
@@ -62,7 +66,7 @@ const CommentEditor = ({
62
66
} ) ;
63
67
64
68
const initialValues = {
65
- comment : rawBody ,
69
+ comment : editorContent ,
66
70
editReasonCode : lastEdit ?. reasonCode || ( userIsStaff && canDisplayEditReason ? 'violates-guidelines' : undefined ) ,
67
71
} ;
68
72
@@ -71,6 +75,15 @@ const CommentEditor = ({
71
75
onCloseEditor ( ) ;
72
76
} , [ onCloseEditor , initialValues ] ) ;
73
77
78
+ const deleteEditorContent = useCallback ( async ( ) => {
79
+ const { updatedResponses, updatedComments } = removeDraftContent ( parentId , id , threadId ) ;
80
+ if ( parentId ) {
81
+ await dispatch ( setDraftComments ( updatedComments ) ) ;
82
+ } else {
83
+ await dispatch ( setDraftResponses ( updatedResponses ) ) ;
84
+ }
85
+ } , [ parentId , id , threadId , setDraftComments , setDraftResponses ] ) ;
86
+
74
87
const saveUpdatedComment = useCallback ( async ( values , { resetForm } ) => {
75
88
if ( id ) {
76
89
const payload = {
@@ -86,6 +99,7 @@ const CommentEditor = ({
86
99
editorRef . current . plugins . autosave . removeDraft ( ) ;
87
100
}
88
101
handleCloseEditor ( resetForm ) ;
102
+ deleteEditorContent ( ) ;
89
103
} , [ id , threadId , parentId , enableInContextSidebar , handleCloseEditor ] ) ;
90
104
// The editorId is used to autosave contents to localstorage. This format means that the autosave is scoped to
91
105
// the current comment id, or the current comment parent or the curren thread.
@@ -97,11 +111,33 @@ const CommentEditor = ({
97
111
}
98
112
} , [ formRef ] ) ;
99
113
114
+ useEffect ( ( ) => {
115
+ const draftHtml = getDraftContent ( parentId , threadId , id ) || rawBody ;
116
+ setEditorContent ( draftHtml ) ;
117
+ } , [ parentId , threadId , id ] ) ;
118
+
119
+ const saveDraftContent = async ( content ) => {
120
+ const draftDataContent = extractContent ( content ) ;
121
+
122
+ const { updatedResponses, updatedComments } = addDraftContent (
123
+ draftDataContent ,
124
+ parentId ,
125
+ id ,
126
+ threadId ,
127
+ ) ;
128
+ if ( parentId ) {
129
+ await dispatch ( setDraftComments ( updatedComments ) ) ;
130
+ } else {
131
+ await dispatch ( setDraftResponses ( updatedResponses ) ) ;
132
+ }
133
+ } ;
134
+
100
135
return (
101
136
< Formik
102
137
initialValues = { initialValues }
103
138
validationSchema = { validationSchema }
104
139
onSubmit = { saveUpdatedComment }
140
+ enableReinitialize
105
141
>
106
142
{ ( {
107
143
values,
@@ -151,7 +187,10 @@ const CommentEditor = ({
151
187
id = { editorId }
152
188
value = { values . comment }
153
189
onEditorChange = { formikCompatibleHandler ( handleChange , 'comment' ) }
154
- onBlur = { formikCompatibleHandler ( handleBlur , 'comment' ) }
190
+ onBlur = { ( content ) => {
191
+ formikCompatibleHandler ( handleChange , 'comment' ) ;
192
+ saveDraftContent ( content ) ;
193
+ } }
155
194
/>
156
195
{ isFormikFieldInvalid ( 'comment' , {
157
196
errors,
0 commit comments