11import ApprovalIcon from "@mui/icons-material/Approval" ;
22import { Dialog , DialogContent , DialogTitle } from "@mui/material" ;
3- import { Fragment , useState } from "react" ;
3+ import { Fragment , useState , useRef , useEffect } from "react" ;
44import { SimpleForm , useListContext , useNotify , useRefresh , useUnselectAll } from "react-admin" ;
55
66import SmallButton from "../../commons/custom_fields/SmallButton" ;
@@ -9,29 +9,54 @@ import { ToolbarCancelSave } from "../../commons/custom_fields/ToolbarCancelSave
99import { validate_required , validate_required_255 } from "../../commons/custom_validators" ;
1010import { AutocompleteInputMedium , TextInputWide } from "../../commons/layout/themes" ;
1111import { httpClient } from "../../commons/ra-data-django-rest-framework" ;
12- import { ASSESSMENT_STATUS_APPROVED , ASSESSMENT_STATUS_BULK_CHOICES , ASSESSMENT_STATUS_REJECTED } from "../types" ;
12+ import { ASSESSMENT_STATUS_APPROVED , ASSESSMENT_STATUS_APPROVED_WITH_EDITS , ASSESSMENT_STATUS_BULK_CHOICES , ASSESSMENT_STATUS_CHOICES , ASSESSMENT_STATUS_REJECTED } from "../types" ;
13+ import MarkdownEdit from "../../commons/custom_fields/MarkdownEdit" ;
1314
1415type AssessmentBulkApprovalProps = {
1516 storeKey : string ;
1617} ;
1718
1819const AssessmentBulkApproval = ( { storeKey } : AssessmentBulkApprovalProps ) => {
20+ const dialogRef = useRef < HTMLDivElement > ( null ) ;
1921 const [ open , setOpen ] = useState ( false ) ;
2022 const [ decision , setDecision ] = useState ( ASSESSMENT_STATUS_APPROVED ) ;
2123 const refresh = useRefresh ( ) ;
2224 const notify = useNotify ( ) ;
23- const { selectedIds } = useListContext ( ) ;
25+ const { data = [ ] , selectedIds } = useListContext ( ) ;
2426 const unselectAll = useUnselectAll ( "observation_logs" , storeKey ) ;
2527 const [ loading , setLoading ] = useState ( false ) ;
2628
29+ const selectedRecords = data . filter ( record =>
30+ selectedIds . includes ( record . id )
31+ ) ;
32+
33+ const [ comment , setComment ] = useState ( "" ) ;
34+
35+ const allSame =
36+ selectedRecords . length > 0 &&
37+ selectedRecords . every ( r => r . comment === selectedRecords [ 0 ] . comment ) ;
38+
39+ useEffect ( ( ) => {
40+ if ( allSame ) {
41+ setComment ( selectedRecords [ 0 ] . comment ?? "" ) ;
42+ }
43+ } , [ allSame , selectedRecords [ 0 ] ?. comment ] ) ;
44+
2745 const assessmentUpdate = async ( data : any ) => {
2846 setLoading ( true ) ;
29- const post_data = {
47+ let post_data : Record < string , any > = {
3048 assessment_status : data . assessment_status ,
3149 rejection_remark : data . rejection_remark ,
3250 observation_logs : selectedIds ,
3351 } ;
3452
53+ if ( data . assessment_status === ASSESSMENT_STATUS_REJECTED ) {
54+ post_data . rejection_remark = data . rejection_remark ;
55+ }
56+ if ( data . assessment_status === ASSESSMENT_STATUS_APPROVED_WITH_EDITS ) {
57+ post_data . observation_log_comment = comment ;
58+ }
59+
3560 httpClient ( window . __RUNTIME_CONFIG__ . API_BASE_URL + "/observation_logs/bulk_approval/" , {
3661 method : "POST" ,
3762 body : JSON . stringify ( post_data ) ,
@@ -66,7 +91,7 @@ const AssessmentBulkApproval = ({ storeKey }: AssessmentBulkApprovalProps) => {
6691 return (
6792 < Fragment >
6893 < SmallButton title = "Approval" onClick = { handleOpen } icon = { < ApprovalIcon /> } />
69- < Dialog open = { open && ! loading } onClose = { handleClose } >
94+ < Dialog open = { open && ! loading } onClose = { handleClose } maxWidth = "lg" >
7095 < DialogTitle sx = { { display : "flex" , alignItems : "center" } } >
7196 < ApprovalIcon />
7297 Assessment approval
@@ -75,7 +100,7 @@ const AssessmentBulkApproval = ({ storeKey }: AssessmentBulkApprovalProps) => {
75100 < SimpleForm onSubmit = { assessmentUpdate } toolbar = { < ToolbarCancelSave onClick = { handleCancel } /> } >
76101 < AutocompleteInputMedium
77102 source = "assessment_status"
78- choices = { ASSESSMENT_STATUS_BULK_CHOICES }
103+ choices = { allSame ? ASSESSMENT_STATUS_CHOICES : ASSESSMENT_STATUS_BULK_CHOICES }
79104 validate = { validate_required }
80105 label = "Decision"
81106 onChange = { ( e ) => setDecision ( e ) }
@@ -86,7 +111,16 @@ const AssessmentBulkApproval = ({ storeKey }: AssessmentBulkApprovalProps) => {
86111 validate = { validate_required_255 }
87112 label = "Remark for rejection"
88113 />
89- ) } { " " }
114+ ) }
115+ { decision == ASSESSMENT_STATUS_APPROVED_WITH_EDITS && (
116+ < MarkdownEdit
117+ initialValue = { comment }
118+ setValue = { setComment }
119+ label = "Comment of Observation Log *"
120+ overlayContainer = { dialogRef . current ?? null }
121+ maxLength = { 4096 }
122+ />
123+ ) }
90124 </ SimpleForm >
91125 </ DialogContent >
92126 </ Dialog >
0 commit comments