-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a new query button [#238]
- Loading branch information
Showing
16 changed files
with
304 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as React from 'react'; | ||
|
||
class PromiseWaiter<Data> { | ||
promise: Promise<Data | undefined> | undefined = undefined; | ||
resolve = (_d?: Data) => {}; | ||
reject = (_e?: Error) => {}; | ||
|
||
create() { | ||
this.promise = new Promise<Data | undefined>((resolve, reject) => { | ||
this.resolve = (d) => { | ||
resolve(d); | ||
this.promise = undefined; | ||
this.resolve = () => {}; | ||
}; | ||
this.reject = (e) => { | ||
reject(e); | ||
this.promise = undefined; | ||
this.reject = () => {}; | ||
}; | ||
}); | ||
|
||
return this.promise; | ||
} | ||
} | ||
|
||
export function usePromiseWaiter<Data>() { | ||
return React.useRef(new PromiseWaiter<Data>()).current; | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/ui/src/ui/pages/query-tracker/NewQueryButton/NewQueryButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as React from 'react'; | ||
import {useState} from 'react'; | ||
import {useSelector} from 'react-redux'; | ||
import {Button} from '@gravity-ui/uikit'; | ||
import Icon from '../../../components/Icon/Icon'; | ||
import {usePromiseWaiter} from '../../../hooks/use-promise-waiter'; | ||
import Modal from '../../../components/Modal/Modal'; | ||
import {isQueryDraftEditted} from '../module/query/selectors'; | ||
|
||
const NewQueryPromt = (props: {cancel: () => void; confirm: () => void; visible: boolean}) => { | ||
return ( | ||
<Modal | ||
title="New query" | ||
content="All the changes will be lost. Are you sure you want to reset query?" | ||
onCancel={props.cancel} | ||
onConfirm={props.confirm} | ||
onOutsideClick={props.cancel} | ||
visible={props.visible} | ||
/> | ||
); | ||
}; | ||
|
||
export const NewQueryButton = ({onClick}: {onClick: () => void}) => { | ||
const waiter = usePromiseWaiter(); | ||
const dirtyQuery = useSelector(isQueryDraftEditted); | ||
const [visible, setVisible] = useState(false); | ||
|
||
const handleClick = () => { | ||
if (dirtyQuery) { | ||
setVisible(true); | ||
|
||
waiter | ||
.create() | ||
.then(() => { | ||
onClick(); | ||
}) | ||
.catch(() => null) | ||
.finally(() => { | ||
setVisible(false); | ||
}); | ||
} else { | ||
onClick(); | ||
} | ||
}; | ||
|
||
return ( | ||
<React.Fragment> | ||
<Button | ||
qa="new-query-btn" | ||
view="outlined" | ||
size="l" | ||
title="New query" | ||
onClick={handleClick} | ||
> | ||
<Icon awesome="file" size={16} /> | ||
New | ||
</Button> | ||
<NewQueryPromt confirm={waiter.resolve} cancel={waiter.reject} visible={visible} /> | ||
</React.Fragment> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 10 additions & 3 deletions
13
packages/ui/src/ui/pages/query-tracker/QueryTrackerTopRow/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 20 additions & 6 deletions
26
packages/ui/src/ui/pages/query-tracker/QueryWidget/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.