11/**
2- * A modal component that displays a confirmation dialog for deleting a vendor .
2+ * A modal component that displays a confirmation dialog for deleting/archiving items .
33 *
44 * @component
55 * @param {BasicModalProps } props - The properties for the BasicModal component.
1212 * <BasicModal isOpen={isOpen} setIsOpen={setIsOpen} />
1313 */
1414
15- import { Button , Modal , Stack , Typography , useTheme } from "@mui/material" ;
15+ import { Button , Modal , Stack , Typography , useTheme , CircularProgress } from "@mui/material" ;
1616import { BasicModalProps } from "../../../../domain/interfaces/iSelect" ;
1717import {
1818 BasicModalCancelButtonStyle ,
1919 BasicModalDeleteButtonStyle ,
2020} from "./style" ;
2121import { useModalKeyHandling } from "../../../../application/hooks/useModalKeyHandling" ;
22+ import { useState } from "react" ;
2223
2324const BasicModal : React . FC < BasicModalProps > = ( {
2425 isOpen,
@@ -30,17 +31,39 @@ const BasicModal: React.FC<BasicModalProps> = ({
3031 type,
3132} ) => {
3233 const theme = useTheme ( ) ;
34+ const [ isLoading , setIsLoading ] = useState ( false ) ;
3335
3436 useModalKeyHandling ( {
3537 isOpen,
36- onClose : ( ) => setIsOpen ( false ) ,
38+ onClose : ( ) => ! isLoading && setIsOpen ( false ) ,
3739 } ) ;
3840
41+ const handleDelete = async ( e : React . MouseEvent ) => {
42+ setIsLoading ( true ) ;
43+ try {
44+ await onDelete ( e ) ;
45+ } finally {
46+ setIsLoading ( false ) ;
47+ }
48+ } ;
49+
50+ const handleCancel = ( e : React . MouseEvent ) => {
51+ if ( ! isLoading ) {
52+ onCancel ( e ) ;
53+ }
54+ } ;
55+
56+ // Determine the action type and button styling
57+ const isArchiveAction = type === "Incident" || type === "Task" ;
58+ const isHardDelete = warningTitle ?. toLowerCase ( ) . includes ( "permanently" ) || warningTitle ?. toLowerCase ( ) . includes ( "delete" ) ;
59+ const buttonColor = isArchiveAction && ! isHardDelete ? "warning" : "error" ;
60+ const buttonText = isArchiveAction ? `Archive ${ type . toLowerCase ( ) } ` : ( isHardDelete ? "Delete permanently" : `Delete ${ type } ` ) ;
61+
3962 return (
4063 < Modal
4164 open = { isOpen }
4265 onClose = { ( _event , reason ) => {
43- if ( reason !== "backdropClick" ) {
66+ if ( reason !== "backdropClick" && ! isLoading ) {
4467 setIsOpen ( false ) ;
4568 }
4669 } }
@@ -57,13 +80,13 @@ const BasicModal: React.FC<BasicModalProps> = ({
5780 top : "50%" ,
5881 left : "50%" ,
5982 transform : "translate(-50%, -50%)" ,
60- width : 450 ,
83+ width : 480 ,
84+ maxWidth : "90vw" ,
6185 bgcolor : theme . palette . background . modal ,
62- border : 1 ,
63- borderColor : theme . palette . border . dark ,
86+ border : `1px solid ${ theme . palette . border . light } ` ,
6487 borderRadius : theme . shape . borderRadius ,
6588 boxShadow : 24 ,
66- p : theme . spacing ( 15 ) ,
89+ p : theme . spacing ( 3 ) ,
6790 "&:focus" : {
6891 outline : "none" ,
6992 } ,
@@ -73,20 +96,23 @@ const BasicModal: React.FC<BasicModalProps> = ({
7396 id = "modal-delete-vendor"
7497 fontSize = { 16 }
7598 fontWeight = { 600 }
99+ color = { theme . palette . text . primary }
76100 >
77101 { warningTitle }
78102 </ Typography >
79103 < Typography
80104 id = "delete-monitor-confirmation"
81105 fontSize = { 13 }
82106 textAlign = { "left" }
107+ color = { theme . palette . text . secondary }
108+ sx = { { lineHeight : 1.5 } }
83109 >
84110 { warningMessage }
85111 </ Typography >
86112 < Stack
87113 direction = "row"
88- gap = { theme . spacing ( 4 ) }
89- mt = { theme . spacing ( 12 ) }
114+ gap = { theme . spacing ( 2 ) }
115+ mt = { theme . spacing ( 4 ) }
90116 justifyContent = "flex-end"
91117 >
92118 < Button
@@ -95,8 +121,13 @@ const BasicModal: React.FC<BasicModalProps> = ({
95121 disableTouchRipple
96122 variant = "text"
97123 color = "inherit"
98- onClick = { ( e ) => onCancel ( e ) }
99- sx = { BasicModalCancelButtonStyle }
124+ onClick = { handleCancel }
125+ disabled = { isLoading }
126+ sx = { {
127+ ...BasicModalCancelButtonStyle ,
128+ opacity : isLoading ? 0.5 : 1 ,
129+ cursor : isLoading ? "not-allowed" : "pointer" ,
130+ } }
100131 >
101132 Cancel
102133 </ Button >
@@ -105,11 +136,20 @@ const BasicModal: React.FC<BasicModalProps> = ({
105136 disableFocusRipple
106137 disableTouchRipple
107138 variant = "contained"
108- color = "error"
109- sx = { BasicModalDeleteButtonStyle }
110- onClick = { ( e ) => onDelete ( e ) }
139+ color = { buttonColor }
140+ sx = { {
141+ ...BasicModalDeleteButtonStyle ,
142+ opacity : isLoading ? 0.7 : 1 ,
143+ cursor : isLoading ? "not-allowed" : "pointer" ,
144+ display : "flex" ,
145+ alignItems : "center" ,
146+ gap : "8px" ,
147+ } }
148+ onClick = { handleDelete }
149+ disabled = { isLoading }
111150 >
112- { type === "Incident" || type === "Task" ? `Archive ${ type . toLowerCase ( ) } ` : `Delete ${ type } ` }
151+ { isLoading && < CircularProgress size = { 16 } color = "inherit" /> }
152+ { buttonText }
113153 </ Button >
114154 </ Stack >
115155 </ Stack >
0 commit comments