Skip to content

Commit

Permalink
from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-hodgson committed Aug 20, 2024
1 parent 614b5ed commit 3e0662c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
21 changes: 12 additions & 9 deletions packages/synapse-react-client/src/components/FullTextSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { ChangeEvent, useRef, useState } from 'react'
import {
ColumnSingleValueFilterOperator,
ColumnSingleValueQueryFilter,
ColumnTypeEnum,
TextMatchesQueryFilter,
} from '@sage-bionetworks/synapse-types'
import { useQueryContext } from './QueryContext'
Expand All @@ -12,6 +11,9 @@ import { HelpPopover } from './HelpPopover/HelpPopover'
import IconSvg from './IconSvg/IconSvg'
import { IconSvgButton } from './IconSvgButton'
import { SYNAPSE_ENTITY_ID_REGEX } from '../utils/functions/RegularExpressions'
import { useAtomValue } from 'jotai'
import { tableQueryDataAtom } from './QueryWrapper/QueryWrapper'
import { getFileColumnModelId } from './SynapseTable/SynapseTableUtils'

// See PLFM-7011
const MIN_SEARCH_QUERY_LENGTH = 3
Expand All @@ -25,7 +27,9 @@ export const FullTextSearch: React.FunctionComponent<FullTextSearchProps> = ({
helpMessage = 'This search bar is powered by MySQL Full Text Search.',
helpUrl,
}: FullTextSearchProps) => {
const { executeQueryRequest, getColumnModel } = useQueryContext()
const { executeQueryRequest } = useQueryContext()
const data = useAtomValue(tableQueryDataAtom)
const columnModels = data?.columnModels
const { showSearchBar } = useQueryVisualizationContext()
const [searchText, setSearchText] = useState('')
const searchInputRef = useRef<HTMLInputElement>(null)
Expand All @@ -42,17 +46,16 @@ export const FullTextSearch: React.FunctionComponent<FullTextSearchProps> = ({
} else {
executeQueryRequest(request => {
const { additionalFilters = [] } = request.query
const idColumnModel = getColumnModel('id')
const synIdColumnModelId = getFileColumnModelId(columnModels)
const isSynapseID = searchText.match(SYNAPSE_ENTITY_ID_REGEX)
if (
isSynapseID &&
idColumnModel &&
idColumnModel.columnType == ColumnTypeEnum.ENTITYID
) {
if (isSynapseID && synIdColumnModelId) {
const idColumnModel = columnModels?.filter(
el => el.id === synIdColumnModelId,
)
const singleValueQueryFilter: ColumnSingleValueQueryFilter = {
concreteType:
'org.sagebionetworks.repo.model.table.ColumnSingleValueQueryFilter',
columnName: 'id',
columnName: idColumnModel![0].name,
operator: ColumnSingleValueFilterOperator.IN,
values: [searchText],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ import {
ColumnSingleValueFilterOperator,
ColumnTypeEnum,
} from '@sage-bionetworks/synapse-types'
import { useSetAtom } from 'jotai'
import { tableQueryDataAtom } from '../../src/components/QueryWrapper/QueryWrapper'
import { mockQueryResultBundle } from '../../src/mocks/mockFileViewQuery'

let setQueryData: ReturnType<typeof useSetAtom> | undefined

function ContextReceiver(props: React.PropsWithChildren<any>) {
setQueryData = useSetAtom(tableQueryDataAtom)
return <>{props.children}</>
}

const renderComponent = (
queryContext: Partial<QueryContextType>,
Expand All @@ -26,7 +36,9 @@ const renderComponent = (
<QueryVisualizationContextProvider
queryVisualizationContext={queryVisualizationContext}
>
<FullTextSearch />
<ContextReceiver>
<FullTextSearch />
</ContextReceiver>
</QueryVisualizationContextProvider>
</QueryContextProvider>,
{
Expand All @@ -37,12 +49,10 @@ const renderComponent = (

const mockExecuteQueryRequest = jest.fn()
const mockGetLastQueryRequest = jest.fn()
const mockGetColumnModel = jest.fn()

const queryContext: Partial<QueryContextType> = {
executeQueryRequest: mockExecuteQueryRequest,
getCurrentQueryRequest: mockGetLastQueryRequest,
getColumnModel: mockGetColumnModel,
}

const queryVisualizationContext: Partial<QueryVisualizationContextType> = {
Expand Down Expand Up @@ -98,12 +108,7 @@ describe('FullTextSearch tests', () => {
})

it('adds the appropriate QueryFilter when searching for Synapse ID', async () => {
const idColModel: ColumnModel = {
name: 'id',
columnType: ColumnTypeEnum.ENTITYID,
id: '1',
}
mockGetColumnModel.mockReturnValue(idColModel)
setQueryData!(mockQueryResultBundle)
renderComponent(queryContext, queryVisualizationContext)

const searchBox = screen.getByRole('textbox')
Expand Down

0 comments on commit 3e0662c

Please sign in to comment.