11import React from 'react' ;
22
3+ import NiceModal from '@ebay/nice-modal-react' ;
34import type { ButtonProps } from '@gravity-ui/uikit' ;
45import { Button , Dialog , DropdownMenu , TextInput } from '@gravity-ui/uikit' ;
56
67import {
78 clearQueryNameToEdit ,
89 saveQuery ,
9- selectQueryAction ,
1010 selectQueryName ,
1111 setQueryAction ,
1212} from '../../../../store/reducers/queryActions/queryActions' ;
@@ -24,20 +24,25 @@ interface SaveQueryProps {
2424 buttonProps ?: ButtonProps ;
2525}
2626
27- function useSaveQueryHandler ( ) {
27+ function useSaveQueryHandler ( dialogProps ?: SaveQueryDialogCommonProps ) {
2828 const dispatch = useTypedDispatch ( ) ;
2929 const onSaveQueryClick = React . useCallback ( ( ) => {
30- dispatch ( setQueryAction ( 'save' ) ) ;
30+ NiceModal . show ( SAVE_QUERY_DIALOG , dialogProps ) ;
3131 dispatch ( clearQueryNameToEdit ( ) ) ;
32- } , [ dispatch ] ) ;
32+ } , [ dispatch , dialogProps ] ) ;
3333
3434 return onSaveQueryClick ;
3535}
3636
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+
3944 return (
40- < Button onClick = { onSaveQueryClick } { ...props } >
45+ < Button onClick = { onSaveQueryClick } { ...buttonProps } >
4146 { i18n ( 'action.save' ) }
4247 </ Button >
4348 ) ;
@@ -80,15 +85,19 @@ export function SaveQuery({buttonProps = {}}: SaveQueryProps) {
8085 return queryNameToEdit ? renderSaveDropdownMenu ( ) : < SaveQueryButton /> ;
8186}
8287
83- interface SaveQueryDialogProps {
88+ interface SaveQueryDialogCommonProps {
8489 onSuccess ?: ( ) => void ;
8590 onCancel ?: ( ) => void ;
91+ onClose ?: ( ) => void ;
8692}
8793
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 ) {
8999 const savedQueries = useSavedQueries ( ) ;
90100 const dispatch = useTypedDispatch ( ) ;
91- const queryAction = useTypedSelector ( selectQueryAction ) ;
92101 const [ queryName , setQueryName ] = React . useState ( '' ) ;
93102 const [ validationError , setValidationError ] = React . useState < string > ( ) ;
94103
@@ -106,6 +115,7 @@ export function SaveQueryDialog({onSuccess, onCancel}: SaveQueryDialogProps) {
106115 dispatch ( setQueryAction ( 'idle' ) ) ;
107116 setQueryName ( '' ) ;
108117 setValidationError ( undefined ) ;
118+ onClose ?.( ) ;
109119 } ;
110120
111121 const onCloseWithoutSave = ( ) => {
@@ -125,12 +135,7 @@ export function SaveQueryDialog({onSuccess, onCancel}: SaveQueryDialogProps) {
125135 } ;
126136
127137 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 } >
134139 < Dialog . Header caption = { i18n ( 'action.save' ) } />
135140 < form
136141 onSubmit = { ( e ) => {
@@ -175,3 +180,27 @@ export function SaveQueryDialog({onSuccess, onCancel}: SaveQueryDialogProps) {
175180 </ Dialog >
176181 ) ;
177182}
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