Skip to content

Commit

Permalink
basic search results
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Aug 2, 2024
1 parent cf97374 commit 67097bc
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 4 deletions.
8 changes: 8 additions & 0 deletions ui2/src/components/Header/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {useState} from "react"
import {useDispatch} from "react-redux"
import {Input, CloseButton, rem} from "@mantine/core"
import {IconSearch} from "@tabler/icons-react"
import {fetchPaginatedSearchResults} from "@/slices/dualPanel/dualPanel"

export default function Search() {
const dispatch = useDispatch()
const [value, setValue] = useState("Clear me")

const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -14,6 +17,11 @@ export default function Search() {

if (key === "Enter") {
console.log(`Enter pressed. Query = ${value}`)
dispatch(
fetchPaginatedSearchResults({
query: value
})
)
}
}

Expand Down
13 changes: 13 additions & 0 deletions ui2/src/components/SearchResults/ActionButtons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Group} from "@mantine/core"
import ToggleSecondaryPanel from "@/components/DualPanel/ToggleSecondaryPanel"

export default function ActionButtons() {
return (
<Group justify="space-between">
<Group>Some button here</Group>
<Group>
<ToggleSecondaryPanel />
</Group>
</Group>
)
}
9 changes: 9 additions & 0 deletions ui2/src/components/SearchResults/SearchResultItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {SearchResultNode} from "@/types"

type Args = {
item: SearchResultNode
}

export default function SearchResultItem({item}: Args) {
return <div>{item.title}</div>
}
17 changes: 17 additions & 0 deletions ui2/src/components/SearchResults/SearchResultItems.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {useContext} from "react"
import {useSelector} from "react-redux"
import {RootState} from "@/app/types"
import {selectSearchResultItems} from "@/slices/dualPanel/dualPanel"
import SearchResultItem from "./SearchResultItem"
import PanelContext from "@/contexts/PanelContext"
import {PanelMode} from "@/types"

export default function SearchResultItems() {
const mode: PanelMode = useContext(PanelContext)
const items = useSelector((state: RootState) =>
selectSearchResultItems(state, mode)
)
const itemComponents = items?.data?.map(i => <SearchResultItem item={i} />)

return <div>{itemComponents}</div>
}
15 changes: 15 additions & 0 deletions ui2/src/components/SearchResults/SearchResults.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Flex} from "@mantine/core"

import ActionButtons from "./ActionButtons"
import SearchResultItems from "./SearchResultItems"

export default function SearchResults() {
return (
<div>
<ActionButtons />
<Flex style={{height: "740px"}}>
<SearchResultItems />
</Flex>
</div>
)
}
3 changes: 3 additions & 0 deletions ui2/src/components/SearchResults/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import SearchResults from "./SearchResults"

export default SearchResults
14 changes: 13 additions & 1 deletion ui2/src/components/SinglePanel/SinglePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {useContext} from "react"

import {selectCommander, selectViewer} from "@/slices/dualPanel/dualPanel"
import {
selectCommander,
selectViewer,
selectSearchResults
} from "@/slices/dualPanel/dualPanel"
import {useSelector} from "react-redux"
import Commander from "@/components/Commander"
import Viewer from "@/components/Viewer"
import SearchResults from "@/components/SearchResults"
import PanelContext from "@/contexts/PanelContext"
import {PanelMode} from "@/types"
import {RootState} from "@/app/types"
Expand All @@ -14,6 +19,9 @@ export default function SinglePanel() {
selectCommander(state, mode)
)
const viewer = useSelector((state: RootState) => selectViewer(state, mode))
const searchResults = useSelector((state: RootState) =>
selectSearchResults(state, mode)
)

if (commander) {
return <Commander />
Expand All @@ -23,5 +31,9 @@ export default function SinglePanel() {
return <Viewer />
}

if (searchResults) {
return <SearchResults />
}

return <>Error: neither viewer nor commander</>
}
67 changes: 64 additions & 3 deletions ui2/src/slices/dualPanel/dualPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import type {
DocumentType,
CurrentNodeType,
PaginationType,
SliceStateStatus
SliceStateStatus,
SearchResultNode
} from "@/types"
import {
DualPanelState,
Expand All @@ -48,7 +49,8 @@ import {INITIAL_PAGE_SIZE} from "@/cconstants"
const initialState: DualPanelState = {
mainPanel: {
commander: commanderInitialState(null),
viewer: null
viewer: null,
searchResults: null
},
secondaryPanel: null,
// common nodes data shared between mainPanel and secondary Panel
Expand Down Expand Up @@ -105,6 +107,26 @@ export const fetchPaginatedNodes = createAsyncThunk<
return result
})

type fetchPaginatedSearchResultsArgs = {
query: string
}

export const fetchPaginatedSearchResults = createAsyncThunk<
Array<SearchResultNode>,
fetchPaginatedSearchResultsArgs
>(
"paginatedSearchResults/fetchSearchResults",
async ({query}: fetchPaginatedSearchResultsArgs) => {
const resp = await axios.get("/api/search/", {
params: {
q: query
},
validateStatus: () => true
})
return resp.data as Array<SearchResultNode>
}
)

export const deleteNodes = createAsyncThunk<string[], string[]>(
"dualPanel/deleteNodes",
async (nodeIds: string[]) => {
Expand Down Expand Up @@ -188,7 +210,8 @@ const dualPanelSlice = createSlice({
openSecondaryPanel(state, action: PayloadAction<CurrentNodeType>) {
state.secondaryPanel = {
commander: commanderInitialState(action.payload),
viewer: null
viewer: null,
searchResults: null
}
},
closeSecondaryPanel(state) {
Expand Down Expand Up @@ -307,6 +330,7 @@ const dualPanelSlice = createSlice({
const versionNumbers = action.payload.versions.map(v => v.number)
state.secondaryPanel = {
commander: null,
searchResults: null,
viewer: {
breadcrumb: action.payload.breadcrumb,
versions: action.payload.versions,
Expand All @@ -316,6 +340,24 @@ const dualPanelSlice = createSlice({
return
}
})
builder.addCase(fetchPaginatedSearchResults.fulfilled, (state, action) => {
state.mainPanel = {
commander: null,
viewer: null,
searchResults: {
pagination: {
numPages: 1,
pageNumber: 1,
pageSize: 100
},
items: {
data: action.payload,
status: "succeeded",
error: null
}
}
}
})
}
})

Expand Down Expand Up @@ -358,6 +400,25 @@ export const selectViewer = (state: RootState, mode: PanelMode) => {
return state.dualPanel.secondaryPanel?.viewer
}

export const selectSearchResults = (state: RootState, mode: PanelMode) => {
if (mode === "main") {
return state.dualPanel.mainPanel.searchResults
}

return null
}

export const selectSearchResultItems = (
state: RootState,
mode: PanelMode
): SliceState<Array<SearchResultNode>> | null | undefined => {
if (mode === "main") {
return state.dualPanel.mainPanel.searchResults?.items
}

return null
}

export const selectPanelNodesRaw = (
state: RootState,
mode: PanelMode
Expand Down
7 changes: 7 additions & 0 deletions ui2/src/slices/dualPanel/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
SliceState,
NodeType,
SearchResultNode,
PanelMode,
CurrentNodeType,
PaginationType,
Expand Down Expand Up @@ -47,9 +48,15 @@ export interface Viewer {
currentVersion: number | null
}

export interface SearchResults {
pagination: PaginationType | null | undefined
items: SliceState<Array<SearchResultNode>>
}

export interface SinglePanel {
commander: Commander | null
viewer: Viewer | null
searchResults: SearchResults | null
}

export interface DualPanelState {
Expand Down
16 changes: 16 additions & 0 deletions ui2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,19 @@ export type DocumentType = {
user_id: string
updated_at: string
}

export type SearchResultPage = {
id: string
page_number: number
text: string
}

export type SearchResultNode = {
id: string
title: string
entity_type: CType
lang: string | null
breadcrumb: Array<[string, string]> | null | undefined
tags: Array<string>
pages: Array<SearchResultPage>
}

0 comments on commit 67097bc

Please sign in to comment.