1
- // Copyright (c) 2014 - 2023 UNICEF. All rights reserved.
2
-
1
+ import { useEffect , useState } from "react" ;
2
+ import { useDispatch } from "react-redux" ;
3
3
import PropTypes from "prop-types" ;
4
- import { ListItemText } from "@material-ui/core" ;
4
+ import { ListItemText , Button } from "@material-ui/core" ;
5
+ import { useParams } from "react-router-dom" ;
6
+ import { KeyboardDatePicker , MuiPickersUtilsProvider } from "@material-ui/pickers" ;
7
+ import DateFnsUtils from "@date-io/date-fns" ;
5
8
9
+ import ActionDialog from "../../../../../../action-dialog" ;
6
10
import ViolationTitle from "../violation-title" ;
7
11
import css from "../../../styles.css" ;
12
+ import { saveRecord } from "../../../../../../records" ;
13
+ import { usePermissions } from "../../../../../../permissions" ;
14
+ import { RECORD_ACTION_ABILITIES } from "../../../../../../record-actions/constants" ;
15
+ import { useI18n } from "../../../../../../i18n" ;
16
+ import { toServerDateFormat } from "../../../../../../../libs" ;
8
17
9
- import { NAME } from "./constants " ;
18
+ import VerifySelect from "./select " ;
10
19
import { getViolationTallyLabel } from "./utils" ;
20
+ import { NAME } from "./constants" ;
11
21
12
- const Component = ( { fields, values, locale, displayName, index, collapsedFieldValues } ) => {
22
+ const Component = ( { fields, values, locale, displayName, index, collapsedFieldValues, mode } ) => {
13
23
const currentValues = values [ index ] ;
24
+ const verifyParams = useParams ( ) ;
25
+ const i18n = useI18n ( ) ;
26
+ const DATE_FORMAT = "dd-MMM-yyyy" ;
27
+
28
+ // State variables
29
+ const dispatch = useDispatch ( ) ;
30
+ const [ verifyModal , setVerifyModal ] = useState ( false ) ;
31
+ const [ verificationValue , setVerificationValue ] = useState ( currentValues ?. ctfmr_verified || "" ) ;
32
+ const [ selectedDate , setSelectedDate ] = useState ( new Date ( ) ) ;
33
+ const [ validationError , setValidationError ] = useState ( "" ) ;
34
+ const maxDate = new Date ( ) ;
35
+
36
+ useEffect ( ( ) => {
37
+ // Changing dropdown select value when backend data updated
38
+ setVerificationValue ( currentValues ?. ctfmr_verified ) ;
39
+ } , [ currentValues ?. ctfmr_verified ] ) ;
40
+
41
+ const handleDropdownDate = date => {
42
+ if ( selectedDate ) {
43
+ setSelectedDate ( date ) ;
44
+ } else {
45
+ setValidationError ( "date should not be null" ) ;
46
+ setSelectedDate ( null ) ;
47
+ }
48
+ } ;
49
+
50
+ const handleOpenVerifyModal = ( idx , event ) => {
51
+ // To open verify dialog confirmation popup
52
+ event . stopPropagation ( ) ;
53
+ setVerifyModal ( true ) ;
54
+ } ;
55
+
56
+ const cancelVerifyHandler = ( ) => {
57
+ // To close verify dialog popup
58
+ setVerifyModal ( false ) ;
59
+ } ;
14
60
61
+ const { canVerify } = usePermissions ( "incidents" , RECORD_ACTION_ABILITIES ) ; // check permission to verify violations
15
62
const violationTally = getViolationTallyLabel ( fields , currentValues , locale ) ;
16
63
64
+ const handleOk = ( ) => {
65
+ // To update the verify status to Verified
66
+
67
+ dispatch (
68
+ saveRecord (
69
+ verifyParams . recordType ,
70
+ "update" ,
71
+ {
72
+ data : {
73
+ [ currentValues . type ] : [
74
+ {
75
+ unique_id : currentValues . unique_id ,
76
+ ctfmr_verified : verificationValue ,
77
+ ctfmr_verified_date : toServerDateFormat ( selectedDate )
78
+ }
79
+ ]
80
+ }
81
+ } , // Save API Call
82
+ verifyParams . id ,
83
+ "Updated successfully" ,
84
+ "" ,
85
+ false ,
86
+ false
87
+ )
88
+ ) ;
89
+ // close();
90
+ } ;
91
+
92
+ // Define VerifySelect component
93
+ const VerifySelectComponent = (
94
+ < VerifySelect selectedValue = { verificationValue } setSelectedValue = { setVerificationValue } />
95
+ ) ;
96
+
97
+ const keyboardDatePickerInputStyles = {
98
+ borderColor : validationError ? "red" : undefined ,
99
+ marginLeft : "5px" // Add left margin here
100
+ } ;
101
+
102
+ // Define MuiPickersUtilsProvider component
103
+ const MuiPickersUtilsProviderComponent = (
104
+ < MuiPickersUtilsProvider utils = { DateFnsUtils } >
105
+ < div className = { css . keyboardDatePickerWrapper } >
106
+ < KeyboardDatePicker
107
+ variant = "inline"
108
+ format = { DATE_FORMAT }
109
+ margin = "normal"
110
+ id = "date-picker-inline"
111
+ value = { selectedDate }
112
+ onChange = { handleDropdownDate }
113
+ error = { ! ! validationError }
114
+ maxDate = { maxDate } // Disable future dates
115
+ InputProps = { {
116
+ style : keyboardDatePickerInputStyles
117
+ } }
118
+ KeyboardButtonProps = { {
119
+ "aria-label" : i18n . t ( "key_performance_indicators.date_range_dialog.aria-labels.from" )
120
+ } }
121
+ />
122
+ </ div >
123
+ </ MuiPickersUtilsProvider >
124
+ ) ;
125
+
17
126
return (
18
127
< ListItemText
19
128
id = "subform-header-button"
@@ -26,7 +135,32 @@ const Component = ({ fields, values, locale, displayName, index, collapsedFieldV
26
135
</ div >
27
136
}
28
137
>
138
+ { canVerify && mode . isShow ? (
139
+ < Button
140
+ onClick = { event => handleOpenVerifyModal ( index , event ) }
141
+ id = { `verify-button-${ index } ` }
142
+ className = { css . verifiedSpan }
143
+ color = "primary"
144
+ variant = "contained"
145
+ size = "small"
146
+ disableElevation
147
+ >
148
+ { i18n . t ( "incidents.change_status" ) }
149
+ </ Button >
150
+ ) : null }
29
151
< ViolationTitle title = { displayName ?. [ locale ] } values = { currentValues } fields = { fields } />
152
+
153
+ < ActionDialog
154
+ open = { verifyModal }
155
+ successHandler = { handleOk }
156
+ cancelHandler = { cancelVerifyHandler }
157
+ dialogTitle = { i18n . t ( "incidents.change_status" ) }
158
+ confirmButtonLabel = { i18n . t ( "buttons.update" ) }
159
+ maxSize = "xs"
160
+ >
161
+ { VerifySelectComponent }
162
+ { verificationValue === "verified" ? MuiPickersUtilsProviderComponent : null }
163
+ </ ActionDialog >
30
164
</ ListItemText >
31
165
) ;
32
166
} ;
@@ -37,6 +171,7 @@ Component.propTypes = {
37
171
fields : PropTypes . array . isRequired ,
38
172
index : PropTypes . number . isRequired ,
39
173
locale : PropTypes . string . isRequired ,
174
+ mode : PropTypes . object . isRequired ,
40
175
values : PropTypes . array . isRequired
41
176
} ;
42
177
0 commit comments