1
1
import React from 'react' ;
2
2
3
+ import NiceModal from '@ebay/nice-modal-react' ;
3
4
import type { ButtonProps } from '@gravity-ui/uikit' ;
4
5
import { Button , Dialog , DropdownMenu , TextInput } from '@gravity-ui/uikit' ;
5
6
6
7
import {
7
8
clearQueryNameToEdit ,
8
9
saveQuery ,
9
- selectQueryAction ,
10
10
selectQueryName ,
11
11
setQueryAction ,
12
12
} from '../../../../store/reducers/queryActions/queryActions' ;
@@ -24,20 +24,25 @@ interface SaveQueryProps {
24
24
buttonProps ?: ButtonProps ;
25
25
}
26
26
27
- function useSaveQueryHandler ( ) {
27
+ function useSaveQueryHandler ( dialogProps ?: SaveQueryDialogCommonProps ) {
28
28
const dispatch = useTypedDispatch ( ) ;
29
29
const onSaveQueryClick = React . useCallback ( ( ) => {
30
- dispatch ( setQueryAction ( 'save' ) ) ;
30
+ NiceModal . show ( SAVE_QUERY_DIALOG , dialogProps ) ;
31
31
dispatch ( clearQueryNameToEdit ( ) ) ;
32
- } , [ dispatch ] ) ;
32
+ } , [ dispatch , dialogProps ] ) ;
33
33
34
34
return onSaveQueryClick ;
35
35
}
36
36
37
- export function SaveQueryButton ( props : ButtonProps ) {
38
- const onSaveQueryClick = useSaveQueryHandler ( ) ;
37
+ interface SaveQueryButtonProps extends ButtonProps {
38
+ dialogProps ?: SaveQueryDialogCommonProps ;
39
+ }
40
+
41
+ export function SaveQueryButton ( { dialogProps, ...buttonProps } : SaveQueryButtonProps ) {
42
+ const onSaveQueryClick = useSaveQueryHandler ( dialogProps ) ;
43
+
39
44
return (
40
- < Button onClick = { onSaveQueryClick } { ...props } >
45
+ < Button onClick = { onSaveQueryClick } { ...buttonProps } >
41
46
{ i18n ( 'action.save' ) }
42
47
</ Button >
43
48
) ;
@@ -80,15 +85,19 @@ export function SaveQuery({buttonProps = {}}: SaveQueryProps) {
80
85
return queryNameToEdit ? renderSaveDropdownMenu ( ) : < SaveQueryButton /> ;
81
86
}
82
87
83
- interface SaveQueryDialogProps {
88
+ interface SaveQueryDialogCommonProps {
84
89
onSuccess ?: ( ) => void ;
85
90
onCancel ?: ( ) => void ;
91
+ onClose ?: ( ) => void ;
86
92
}
87
93
88
- export function SaveQueryDialog ( { onSuccess, onCancel} : SaveQueryDialogProps ) {
94
+ interface SaveQueryDialogProps extends SaveQueryDialogCommonProps {
95
+ open : boolean ;
96
+ }
97
+
98
+ function SaveQueryDialog ( { onSuccess, onCancel, onClose, open} : SaveQueryDialogProps ) {
89
99
const savedQueries = useSavedQueries ( ) ;
90
100
const dispatch = useTypedDispatch ( ) ;
91
- const queryAction = useTypedSelector ( selectQueryAction ) ;
92
101
const [ queryName , setQueryName ] = React . useState ( '' ) ;
93
102
const [ validationError , setValidationError ] = React . useState < string > ( ) ;
94
103
@@ -106,6 +115,7 @@ export function SaveQueryDialog({onSuccess, onCancel}: SaveQueryDialogProps) {
106
115
dispatch ( setQueryAction ( 'idle' ) ) ;
107
116
setQueryName ( '' ) ;
108
117
setValidationError ( undefined ) ;
118
+ onClose ?.( ) ;
109
119
} ;
110
120
111
121
const onCloseWithoutSave = ( ) => {
@@ -125,12 +135,7 @@ export function SaveQueryDialog({onSuccess, onCancel}: SaveQueryDialogProps) {
125
135
} ;
126
136
127
137
return (
128
- < Dialog
129
- open = { queryAction === 'save' }
130
- hasCloseButton = { false }
131
- size = "s"
132
- onClose = { onCloseWithoutSave }
133
- >
138
+ < Dialog open = { open } hasCloseButton = { false } size = "s" onClose = { onCloseWithoutSave } >
134
139
< Dialog . Header caption = { i18n ( 'action.save' ) } />
135
140
< form
136
141
onSubmit = { ( e ) => {
@@ -175,3 +180,27 @@ export function SaveQueryDialog({onSuccess, onCancel}: SaveQueryDialogProps) {
175
180
</ Dialog >
176
181
) ;
177
182
}
183
+
184
+ export const SAVE_QUERY_DIALOG = 'save-query-dialog' ;
185
+
186
+ export const SaveQueryDialogNiceModal = NiceModal . create ( ( props : SaveQueryDialogProps ) => {
187
+ const modal = NiceModal . useModal ( ) ;
188
+
189
+ const handleClose = ( ) => {
190
+ modal . hide ( ) ;
191
+ modal . remove ( ) ;
192
+ } ;
193
+
194
+ return (
195
+ < SaveQueryDialog
196
+ { ...props }
197
+ onClose = { ( ) => {
198
+ props . onClose ?.( ) ;
199
+ handleClose ( ) ;
200
+ } }
201
+ open = { modal . visible }
202
+ />
203
+ ) ;
204
+ } ) ;
205
+
206
+ NiceModal . register ( SAVE_QUERY_DIALOG , SaveQueryDialogNiceModal ) ;
0 commit comments