diff --git a/src/components/SearchPopover/index.tsx b/src/components/SearchPopover/index.tsx new file mode 100644 index 0000000..8d0bec6 --- /dev/null +++ b/src/components/SearchPopover/index.tsx @@ -0,0 +1,77 @@ +import React, { useState } from 'react'; +import { + IonPopover, + IonButton, + IonIcon, + IonLabel, + IonItem, + IonList, + IonListHeader, + IonRadio, + IonRadioGroup, +} from '@ionic/react'; +import { ellipsisHorizontal, ellipsisVertical } from 'ionicons/icons'; +import { useDispatch, useSelector } from 'react-redux'; +import { setCurrentSearchView } from '../../data/user/user.slice'; +import { RootState } from '../../store'; +import { selectCurrentSearchView } from '../../data/user/user.selector'; + +export const SearchPopover: React.FC = () => { + const [popoverState, setShowPopover] = useState({ + showPopover: false, + event: undefined, + }); + + const dispatch = useDispatch(); + const currentSearchView = useSelector((state: RootState) => + selectCurrentSearchView(state), + ); + + return ( + <> + + setShowPopover({ showPopover: false, event: undefined }) + } + > + + + dispatch( + setCurrentSearchView({ currentSearchView: e.detail.value }), + ) + } + > + + View + + + Card + + + + List + + + + + + { + event.persist(); + setShowPopover({ showPopover: true, event }); + }} + > + + + + ); +}; diff --git a/src/components/SearchResults/index.tsx b/src/components/SearchResults/index.tsx new file mode 100644 index 0000000..f0289be --- /dev/null +++ b/src/components/SearchResults/index.tsx @@ -0,0 +1,54 @@ +import { IonNote, IonRouterLink } from '@ionic/react'; +import React from 'react'; +import { Chapter, getChapterIdsByUrl } from '../Chapters'; +import { LunrResult } from '../../utils/lunr'; + +interface ContainerProps { + results: LunrResult[]; +} + +export const SearchResults: React.FC = (props) => { + const { results } = props; + + if (results.length === 0) { + return ( +
+ No results found +
+ ); + } + + return ( + <> + Found {results.length} results +
+ {results.map((result, resultIndex) => { + const { id: chapterUrl } = result; + const { id, subId } = getChapterIdsByUrl(chapterUrl); + return ( + + + + ); + })} +
+ + ); +}; diff --git a/src/pages/Search/index.tsx b/src/pages/Search/index.tsx index b7ac20a..db5ec44 100644 --- a/src/pages/Search/index.tsx +++ b/src/pages/Search/index.tsx @@ -5,14 +5,14 @@ import { IonHeader, IonMenuButton, IonPage, - IonRouterLink, IonSearchbar, IonTitle, IonToolbar, } from '@ionic/react'; import { useTranslation } from 'react-i18next'; +import { SearchPopover } from '../../components/SearchPopover'; +import { SearchResults } from '../../components/SearchResults'; import { useLunr } from '../../utils/lunr'; -import { Chapter, getChapterIdsByUrl } from '../../components/Chapters'; interface ContainerProps {} @@ -25,39 +25,23 @@ export const SearchPage: React.FC = (props) => { return ( - + {t('SEARCH.TITLE')} + + + - - - - {t('SEARCH.TITLE')} - - + setSearchValue(event.detail.value!)} > -
- {results.map((result, resultIndex) => { - const chapterUrl = result.id; - const { id, subId } = getChapterIdsByUrl(chapterUrl); - return ( - - - - ); - })} -
+
); diff --git a/src/utils/lunr.tsx b/src/utils/lunr.tsx index d604b2c..58fa148 100644 --- a/src/utils/lunr.tsx +++ b/src/utils/lunr.tsx @@ -12,6 +12,12 @@ type LunrDocs = { }; }; +export type LunrResult = { + id: string; + title: string; + body: string; +}; + const createLunrDocs = () => { // eslint-disable-next-line react-hooks/rules-of-hooks const { t } = useTranslation();