Skip to content

Commit

Permalink
Adds deletion of queries from workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelechu committed Apr 24, 2020
1 parent 6ffd68b commit 8ce95f7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ stories.add('Basic', () => {
onCreate={() => {
action('onCreate')
}}
onDelete={id => {
action('onDelete')(id)
}}
/>
</NavigationBarContext.Provider>
)
Expand Down
9 changes: 6 additions & 3 deletions src/main/webapp/components/query-manager/query-manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const {
} = require('../result-export/result-export-action')

const QueryCard = (props: QueryCardProps) => {
const { onSearch, query = {}, queryInteractions } = props
const { onSearch, onDelete, query = {}, queryInteractions } = props
const [anchorEl, handleOpen, handleClose] = useAnchorEl()
const [{ active: isDrawing }] = useDrawInterface()
const [wasDrawing, setWasDrawing] = useState(false)
Expand Down Expand Up @@ -56,7 +56,7 @@ const QueryCard = (props: QueryCardProps) => {
>
<Actions attributes={attributes}>
<EditAction onEdit={handleOpen} />
<DeleteAction />
<DeleteAction itemName="query" onDelete={onDelete} />
<MetacardInteractionsDropdown>
{queryInteractions.map((interaction, i) => {
return (
Expand All @@ -79,7 +79,7 @@ const QueryCard = (props: QueryCardProps) => {
}

const QuerySelector = (props: QuerySelectorProps) => {
const { queries, currentQuery, onSearch, queryInteractions } = props
const { queries, currentQuery, onSearch, onDelete, queryInteractions } = props
const hasQueries = queries && queries.length > 0
const [anchorEl, handleOpen, handleClose, open] = useAnchorEl()

Expand All @@ -89,6 +89,7 @@ const QuerySelector = (props: QuerySelectorProps) => {
query={query}
key={query.id}
onSearch={() => onSearch(query.id!)}
onDelete={() => onDelete(query.id!)}
/>
))

Expand All @@ -112,6 +113,7 @@ const QuerySelector = (props: QuerySelectorProps) => {
]}
query={queries.find(query => query.id === currentQuery)}
onSearch={() => onSearch(currentQuery)}
onDelete={() => onDelete(currentQuery)}
/>
<Button
color="primary"
Expand Down Expand Up @@ -155,6 +157,7 @@ const QueryManager = (props: QueryManagerProps) => {
props.onSave(id)
props.onSearch(id)
}

const queryInteractions = [
(query: QueryType) => {
return (
Expand Down
17 changes: 11 additions & 6 deletions src/main/webapp/components/query-manager/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import { EditorProps } from '../query-editor'

type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U

export type QueryCardProps = {
type QueryEditorProps = {
QueryEditor: React.FunctionComponent<EditorProps>
query?: QueryType
onSearch: () => void
onChange: (query: QueryType) => void
}

export type QueryCardProps = QueryEditorProps & {
onDelete: () => void
queryInteractions: Array<(query: QueryType) => React.ReactNode>
}

export type QuerySelectorProps = Overwrite<
QueryCardProps,
{ onSearch: (id: string) => void }
{
onSearch: (id: string) => void
onDelete: (id: string) => void
}
> & {
queries: QueryType[]
currentQuery: string
Expand All @@ -23,10 +31,7 @@ export type AddQueryProps = QuerySelectorProps & {
onCreate: (query: QueryType) => void
}

export type QueryEditorPopoverProps = Omit<
QueryCardProps,
'queryInteractions'
> & {
export type QueryEditorPopoverProps = QueryEditorProps & {
anchorEl: HTMLDivElement
onClose: () => void
}
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/components/workspace/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as useCreateQuery } from './use-create-query'
export { default as useDeleteQuery } from './use-delete-query'
export { default as useSaveQuery } from './use-save-query'
export { default as useSaveWorkspace } from './use-save-workspace'
25 changes: 25 additions & 0 deletions src/main/webapp/components/workspace/hooks/use-delete-query.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useMutation } from '@apollo/react-hooks'
import gql from 'graphql-tag'

export default (onDelete: any) => {
const mutation = gql`
mutation DeleteQuery($id: ID!) {
deleteMetacard(id: $id)
}
`

const [_delete] = useMutation(mutation, {
onCompleted: data => {
onDelete(data.deleteMetacard)
},
})

return (id: string) => {
_delete({
variables: {
id,
},
optimisticResponse: { deleteMetacard: id },
})
}
}
20 changes: 19 additions & 1 deletion src/main/webapp/components/workspace/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ import QueryEditor from '../query-editor'
import QueryManager from '../query-manager'
import QueryStatus from '../query-status'
import { ResultIndexCards } from '../results'
import { useCreateQuery, useSaveQuery, useSaveWorkspace } from './hooks'
import {
useCreateQuery,
useDeleteQuery,
useSaveQuery,
useSaveWorkspace,
} from './hooks'
import WorkspaceTitle from './workspace-title'
import { get } from 'immutable'

const LoadingComponent = () => <LinearProgress />

Expand Down Expand Up @@ -91,6 +97,17 @@ export default () => {

const { results, status, onSearch, onCancel, onClear } = useQueryExecutor()

const deleteQuery = useDeleteQuery(id => {
const updatedQueries = queries.filter(query => query.id !== id)
setQueries(updatedQueries)
if (currentQuery === id) {
onClear()
setCurrentQuery(get(updatedQueries[0], 'id', null))
}

saveWorkspace({ queries: updatedQueries })
})

const saveQuery = useSaveQuery()
const [saveWorkspace, saving] = useSaveWorkspace()

Expand Down Expand Up @@ -193,6 +210,7 @@ export default () => {
)
}}
onCreate={createQuery}
onDelete={deleteQuery}
onSave={id => {
saveQuery(queries.find(query => query.id === id))
}}
Expand Down

0 comments on commit 8ce95f7

Please sign in to comment.