1
1
/**
2
2
* AppealComment.
3
3
*/
4
- import { FC , useCallback , useState } from 'react'
4
+ import { FC , useCallback , useContext , useEffect , useMemo , useState } from 'react'
5
5
import {
6
6
Controller ,
7
7
ControllerRenderProps ,
8
8
useForm ,
9
9
UseFormReturn ,
10
10
} from 'react-hook-form'
11
- import _ , { bind , includes } from 'lodash'
11
+ import { get } from 'lodash'
12
12
import classNames from 'classnames'
13
13
14
14
import { yupResolver } from '@hookform/resolvers/yup'
15
15
16
16
import { MarkdownReview } from '../MarkdownReview'
17
17
import { FieldMarkdownEditor } from '../FieldMarkdownEditor'
18
- import { FormAppealResponse } from '../../models'
19
- import { formAppealResponseSchema } from '../../utils'
20
- import { ADMIN , COPILOT , FINISHTAB , ITERATIVE_REVIEW , TAB } from '../../../config/index.config'
18
+ import { AppealInfo , ChallengeDetailContextModel , FormAppealResponse } from '../../models'
19
+ import { formAppealResponseSchema , isAppealsPhase } from '../../utils'
20
+ import { ReviewItemComment } from '../../models/ReviewItemComment.model'
21
+ import { ChallengeDetailContext } from '../../contexts'
21
22
22
23
import styles from './Appeal.module.scss'
23
24
24
25
interface Props {
25
26
className ?: string
26
- role ?: string
27
+ appealInfo ?: AppealInfo
28
+ commentItem : ReviewItemComment
29
+ isSavingAppeal : boolean
30
+ addAppeal : (
31
+ content : string ,
32
+ commentItem : ReviewItemComment ,
33
+ success : ( ) => void ,
34
+ ) => void
35
+ doDeleteAppeal : (
36
+ appealInfo : AppealInfo | undefined ,
37
+ success : ( ) => void ,
38
+ ) => void
27
39
}
28
40
29
41
export const AppealComment : FC < Props > = ( props : Props ) => {
30
42
const [ appealResponse , setAppealResponse ] = useState ( '' )
31
43
const [ showResponseForm , setShowResponseForm ] = useState ( false )
32
- const [ showAppealResponse , setShowAppealResponse ] = useState ( false )
44
+
45
+ const { challengeInfo } : ChallengeDetailContextModel = useContext (
46
+ ChallengeDetailContext ,
47
+ )
48
+ const canAddAppeal = useMemo ( ( ) => isAppealsPhase ( challengeInfo ) , [ challengeInfo ] )
33
49
34
50
const {
35
51
handleSubmit,
@@ -44,29 +60,54 @@ export const AppealComment: FC<Props> = (props: Props) => {
44
60
} )
45
61
46
62
const onSubmit = useCallback ( ( data : FormAppealResponse ) => {
47
- setAppealResponse ( data . response )
48
- setShowResponseForm ( false )
49
- setShowAppealResponse ( true )
50
- } , [ ] )
63
+ props . addAppeal ( data . response , props . commentItem , ( ) => {
64
+ setAppealResponse ( data . response )
65
+ setShowResponseForm ( false )
66
+ } )
67
+ } , [ props . commentItem ] )
51
68
52
- if ( includes ( FINISHTAB , sessionStorage . getItem ( TAB ) ) ) {
53
- return < > </ >
54
- }
69
+ useEffect ( ( ) => {
70
+ if ( props . appealInfo ) {
71
+ setAppealResponse ( props . appealInfo . content )
72
+ }
73
+ } , [ props . appealInfo ] )
55
74
56
75
return (
57
76
< div className = { classNames ( styles . container , props . className ) } >
58
- { showAppealResponse && (
77
+ { appealResponse && ! showResponseForm && (
59
78
< div className = { styles . blockAppealComment } >
60
79
< span className = { styles . textTitle } > Appeal Comment</ span >
61
80
< MarkdownReview value = { appealResponse } />
81
+ { canAddAppeal && (
82
+ < div className = { styles . blockBtns } >
83
+ < button
84
+ onClick = { function onClick ( ) {
85
+ setShowResponseForm ( true )
86
+ } }
87
+ className = 'filledButton'
88
+ type = 'button'
89
+ disabled = { props . isSavingAppeal }
90
+ >
91
+ Edit Appeal
92
+ </ button >
93
+ < button
94
+ onClick = { function onClick ( ) {
95
+ props . doDeleteAppeal ( props . appealInfo , ( ) => {
96
+ setAppealResponse ( '' )
97
+ } )
98
+ } }
99
+ type = 'button'
100
+ className = 'cancelButton'
101
+ disabled = { props . isSavingAppeal }
102
+ >
103
+ Delete
104
+ </ button >
105
+ </ div >
106
+ ) }
62
107
</ div >
63
108
) }
64
109
65
- { ! showResponseForm
66
- && ! showAppealResponse
67
- && ! includes ( FINISHTAB , sessionStorage . getItem ( TAB ) )
68
- && ! includes ( sessionStorage . getItem ( TAB ) , ITERATIVE_REVIEW )
69
- && ! includes ( [ COPILOT , ADMIN ] , props . role ) && (
110
+ { ! appealResponse && ! showResponseForm && canAddAppeal && (
70
111
< button
71
112
type = 'button'
72
113
className = 'borderButton'
@@ -95,23 +136,31 @@ export const AppealComment: FC<Props> = (props: Props) => {
95
136
} ) {
96
137
return (
97
138
< FieldMarkdownEditor
139
+ initialValue = { appealResponse }
98
140
className = { styles . markdownEditor }
99
141
onChange = { controlProps . field . onChange }
100
142
showBorder
101
143
onBlur = { controlProps . field . onBlur }
102
- error = { _ . get ( errors , 'response.message' ) }
144
+ error = { get ( errors , 'response.message' ) }
145
+ disabled = { props . isSavingAppeal }
103
146
/>
104
147
)
105
148
} }
106
149
/>
107
150
< div className = { styles . blockBtns } >
108
- < button className = 'filledButton' type = 'submit' >
151
+ < button
152
+ disabled = { props . isSavingAppeal }
153
+ className = 'filledButton'
154
+ type = 'submit'
155
+ >
109
156
Submit Appeal
110
157
</ button >
111
158
< button
112
159
type = 'button'
113
160
className = 'borderButton'
114
- onClick = { bind ( setShowResponseForm , undefined , false ) }
161
+ onClick = { function onClick ( ) {
162
+ setShowResponseForm ( false )
163
+ } }
115
164
>
116
165
Cancel
117
166
</ button >
0 commit comments