Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save edited queries on search #304

Merged
merged 1 commit into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ stories.add('Basic', () => {
action('onSearch')(id)
setQuery(id)
}}
onSave={id => {
action('onSave')(id)
}}
onCreate={() => {
action('onCreate')
}}
Expand Down
7 changes: 6 additions & 1 deletion src/main/webapp/components/query-manager/query-manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ const QueryManager = (props: QueryManagerProps) => {
props.onChange(updatedQueries)
}

const onSearch = (id: string) => {
props.onSave(id)
props.onSearch(id)
}

return (
<React.Fragment>
<AddQuery {...props} onChange={onChange} />
<QuerySelector {...props} onChange={onChange} />
<QuerySelector {...props} onChange={onChange} onSearch={onSearch} />
</React.Fragment>
)
}
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/components/query-manager/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export type QueryManagerProps = Overwrite<
{ onChange: (queries: QueryType[]) => void }
> & {
onCreate: (query: QueryType) => void
onSave: (id: string) => void
}
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,2 +1,3 @@
export { default as useCreateQuery } from './use-create-query'
export { default as useSaveQuery } from './use-save-query'
export { default as useSaveWorkspace } from './use-save-workspace'
26 changes: 26 additions & 0 deletions src/main/webapp/components/workspace/hooks/use-save-query.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useMutation } from '@apollo/react-hooks'
import gql from 'graphql-tag'
import { QueryType } from '../../query-builder/types'

export default () => {
const mutation = gql`
mutation SaveQuery($id: ID!, $attrs: MetacardAttributesInput!) {
saveMetacard(id: $id, attributes: $attrs) {
id
}
}
`

const [save] = useMutation(mutation)
return (query: QueryType) => {
save({
variables: {
id: query.id,
attrs: {
...query,
metacard_type: 'metacard.query',
},
},
})
}
}
10 changes: 8 additions & 2 deletions src/main/webapp/components/workspace/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { InlineRetry } from '../network-retry'
import QueryEditor from '../query-editor'
import QueryManager from '../query-manager'
import QueryStatus from '../query-status'
import { useCreateQuery, useSaveWorkspace } from './hooks'
import { useCreateQuery, useSaveQuery, useSaveWorkspace } from './hooks'

const LoadingComponent = () => <LinearProgress />

Expand All @@ -41,6 +41,8 @@ const workspaceById = gql`
title
filterTree
type
sorts
sources
}
lists {
list_bookmarks
Expand Down Expand Up @@ -95,8 +97,10 @@ export default () => {
const [queries, setQueries] = useState()
const { results, status, onSearch, onCancel, onClear } = useQueryExecutor()

const saveQuery = useSaveQuery()
const saveWorkspace = useSaveWorkspace()
const createQuery = useCreateQuery(query => {
onClear()
setQueries([query, ...queries])
setCurrentQuery(query.id)
saveWorkspace({ queries: [query, ...queries] })
Expand Down Expand Up @@ -166,7 +170,6 @@ export default () => {
{tab === 0 &&
queries && (
<React.Fragment>
{/* //TODO mutate cache on search so that the queries reflect edits made in queryEditor */}
<QueryManager
QueryEditor={QueryEditor}
queries={queries}
Expand All @@ -179,6 +182,9 @@ export default () => {
)
}}
onCreate={createQuery}
onSave={id => {
saveQuery(queries.find(query => query.id === id))
}}
onChange={queries => setQueries(queries)}
/>

Expand Down