Skip to content

Commit

Permalink
add toggle entre busca básica e busca avançada
Browse files Browse the repository at this point in the history
  • Loading branch information
jesielviana committed Sep 18, 2023
1 parent 02b5b06 commit 2134847
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 141 deletions.
14 changes: 8 additions & 6 deletions src/components/AdvancedSearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { useRouter } from 'next/router';
import { useState } from 'react';

interface CustomSearchBoxProps extends SearchContextState {
updateQueryConfig: (advancedQuery: string) => void;
toogleAdvancedConfig: (advanced: boolean) => void;
}

const AdvancedSearchBox = ({ searchTerm, setSearchTerm, updateQueryConfig }: CustomSearchBoxProps) => {
const AdvancedSearchBox = ({ searchTerm, setSearchTerm, toogleAdvancedConfig }: CustomSearchBoxProps) => {
const { t } = useTranslation('common');
const router = useRouter();
const [field, setField] = useState('All fields');
const [field, setField] = useState('title_text');
const [value, setValue] = useState('');
const [op, setOp] = useState('&');
const [fullQuery, setFullQuery] = useState(searchTerm || '');
Expand All @@ -25,7 +25,6 @@ const AdvancedSearchBox = ({ searchTerm, setSearchTerm, updateQueryConfig }: Cus

return (
<div className="d-flex flex-column flex-gap-1 advanced">
<span className="">Basic Search </span>
<div className="d-flex flex-gap-1">
<select
id={`field`}
Expand Down Expand Up @@ -70,9 +69,9 @@ const AdvancedSearchBox = ({ searchTerm, setSearchTerm, updateQueryConfig }: Cus
<SearchBox
onSubmit={(searchTerm) => {
searchTerm = fullQuery || searchTerm;
updateQueryConfig(fullQuery);
// updateQueryConfig(fullQuery);
setSearchTerm(fullQuery);
router.query.q = `advanced=true&&${searchTerm}`;
router.query.q = `${searchTerm}`;
router.push(router);
}}
view={({ onChange, onSubmit }) => (
Expand All @@ -99,6 +98,9 @@ const AdvancedSearchBox = ({ searchTerm, setSearchTerm, updateQueryConfig }: Cus
</form>
)}
/>
<span onClick={() => toogleAdvancedConfig(false)} className="link-color">
Basic Search
</span>
</div>
);
};
Expand Down
95 changes: 95 additions & 0 deletions src/components/BasicSearchBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { SearchBox } from '@elastic/react-search-ui';
import { useTranslation } from 'next-i18next';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import ElasticSearchStatsService from '../services/ElasticSearchStatsService';

const VIVO_URL_ITEM_BASE = process.env.VIVO_URL_ITEM_BASE;

export type BasicSearchBoxProps = {
titleFieldName: string;
itemLinkPrefix: string;
indexName: string;
updateOpetatorConfig: (operator: string) => void;
toogleAdvancedConfig: (advanced: boolean) => void;
};

const BasicSearchBox = ({
titleFieldName,
itemLinkPrefix,
indexName,
updateOpetatorConfig,
toogleAdvancedConfig,
}: BasicSearchBoxProps) => {
const { t } = useTranslation('common');
const router = useRouter();
const [docsCount, setDocsCount] = useState(localStorage.getItem(indexName));

useEffect(() => {
ElasticSearchStatsService(indexName)
.then((res) => {
const count = res['docs.count'];
localStorage.setItem(indexName, count);
setDocsCount(count);
})
.catch((err) => {
console.error(err);
});
}, []);

return (
<SearchBox
autocompleteMinimumCharacters={3}
searchAsYouType={false}
autocompleteResults={{
linkTarget: '_blank',
sectionTitle: t('Open link') || '',
titleField: titleFieldName,
urlField: '',
shouldTrackClickThrough: true,
}}
autocompleteSuggestions={true}
debounceLength={0}
onSubmit={(searchTerm) => {
updateOpetatorConfig('OR');
router.query.q = searchTerm;
router.push(router);
}}
onSelectAutocomplete={(selection: any, item: any, defaultOnSelectAutocomplete: any) => {
if (selection.suggestion) {
updateOpetatorConfig('AND');
defaultOnSelectAutocomplete(selection);
} else {
router.push(`${VIVO_URL_ITEM_BASE}/${itemLinkPrefix}${selection.id.raw}&lang=${router.locale}`);
}
}}
inputView={({ getAutocomplete, getInputProps, getButtonProps }) => (
<div className="d-flex flex-column flex-gap-1 basic-search">
<div className="d-flex flex-gap-1 align-items-center">
<div className="sui-search-box__wrapper">
<input
{...getInputProps({
placeholder: `${t('Enter at least 3 characters and search among')} ${t('numberFormat', {
value: docsCount,
})} ${t('documents')}`,
})}
/>
{getAutocomplete()}
</div>
<input
{...getButtonProps({
disabled: getInputProps()?.value?.trim().length < 3,
})}
/>
</div>
<span onClick={() => toogleAdvancedConfig(true)} className="link-color">
Advanced Search
</span>
</div>
)}
></SearchBox>
);
};

export default BasicSearchBox;
88 changes: 18 additions & 70 deletions src/components/CustomSearchBox.tsx
Original file line number Diff line number Diff line change
@@ -1,82 +1,30 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { SearchBox } from '@elastic/react-search-ui';
import { useTranslation } from 'next-i18next';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import ElasticSearchStatsService from '../services/ElasticSearchStatsService';
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { useContext } from 'react';
import AdvancedSearchBox from './AdvancedSearchBox';
import BasicSearchBox from './BasicSearchBox';
import CustomContext from './context/CustomContext';

const VIVO_URL_ITEM_BASE = process.env.VIVO_URL_ITEM_BASE;

type CustomSearchBoxProps = {
export type CustomSearchBoxProps = {
titleFieldName: string;
itemLinkPrefix: string;
indexName: string;
updateOpetatorConfig: (operator: string) => void;
};

const CustomSearchBox = ({ titleFieldName, itemLinkPrefix, indexName, updateOpetatorConfig }: CustomSearchBoxProps) => {
const { t } = useTranslation('common');
const router = useRouter();
const [docsCount, setDocsCount] = useState(localStorage.getItem(indexName));

useEffect(() => {
ElasticSearchStatsService(indexName)
.then((res) => {
const count = res['docs.count'];
localStorage.setItem(indexName, count);
setDocsCount(count);
})
.catch((err) => {
console.error(err);
});
}, []);
const { advanced, setAdvanced } = useContext(CustomContext);

return (
<SearchBox
autocompleteMinimumCharacters={3}
searchAsYouType={false}
autocompleteResults={{
linkTarget: '_blank',
sectionTitle: t('Open link') || '',
titleField: titleFieldName,
urlField: '',
shouldTrackClickThrough: true,
}}
autocompleteSuggestions={true}
debounceLength={0}
onSubmit={(searchTerm) => {
updateOpetatorConfig('OR');
router.query.q = searchTerm;
router.push(router);
}}
onSelectAutocomplete={(selection: any, item: any, defaultOnSelectAutocomplete: any) => {
if (selection.suggestion) {
updateOpetatorConfig('AND');
defaultOnSelectAutocomplete(selection);
} else {
router.push(`${VIVO_URL_ITEM_BASE}/${itemLinkPrefix}${selection.id.raw}&lang=${router.locale}`);
}
}}
inputView={({ getAutocomplete, getInputProps, getButtonProps }) => (
<>
<div className="sui-search-box__wrapper">
<input
{...getInputProps({
placeholder: `${t('Enter at least 3 characters and search among')} ${t('numberFormat', {
value: docsCount,
})} ${t('documents')}`,
})}
/>
{getAutocomplete()}
</div>
<input
{...getButtonProps({
disabled: getInputProps()?.value?.trim().length < 3,
})}
/>
</>
)}
></SearchBox>
return advanced ? (
//@ts-ignore
<AdvancedSearchBox toogleAdvancedConfig={setAdvanced} />
) : (
<BasicSearchBox
titleFieldName={titleFieldName}
itemLinkPrefix={itemLinkPrefix}
updateOpetatorConfig={updateOpetatorConfig}
indexName={indexName}
toogleAdvancedConfig={setAdvanced}
/>
);
};

Expand Down
1 change: 1 addition & 0 deletions src/components/DefaultQueryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const DefaultQueryConfig = (indexName: string) => {
return {
debug: false,
indicators: [],
advanced: false,
urlPushDebounceLength: 500,
alwaysSearchOnInitialLoad: false,
hasA11yNotifications: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { PropsWithChildren, createContext, useState } from 'react';
import { IndicatorType } from '../../types/Entities';

interface IndicadorContextProps {
interface CustomContextProps {
indicators: IndicatorType[][];
setIndicatorsData: (data: Array<IndicatorType[]>) => void;
isEmpty: () => boolean;
advanced: boolean;
setAdvanced: (advanced: boolean) => void;
}

const IndicatorContext = createContext<IndicadorContextProps>({} as IndicadorContextProps);
export default IndicatorContext;
const CustomContext = createContext<CustomContextProps>({} as CustomContextProps);
export default CustomContext;

export function IndicatorProvider(props: PropsWithChildren<object>) {
export function CustomProvider(props: PropsWithChildren<object>) {
const [indicators, setIndicators] = useState<Array<IndicatorType[]>>([] as Array<IndicatorType[]>);
const [advanced, setAdvanced] = useState(false);

function setIndicatorsData(data: Array<IndicatorType[]>) {
setIndicators(data);
Expand All @@ -22,14 +25,16 @@ export function IndicatorProvider(props: PropsWithChildren<object>) {
}

return (
<IndicatorContext.Provider
<CustomContext.Provider
value={{
indicators,
setIndicatorsData,
isEmpty,
advanced,
setAdvanced,
}}
>
{props.children}
</IndicatorContext.Provider>
</CustomContext.Provider>
);
}
2 changes: 1 addition & 1 deletion src/components/indicators/GroupsIndicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CHART_BACKGROUD_COLORS, CHART_BORDER_COLORS } from '../../../utils/Util
import ElasticSearchService from '../../services/ElasticSearchService';
import { CustomSearchQuery, IndicatorType } from '../../types/Entities';
import { IndicatorsProps } from '../../types/Propos';
import IndicatorContext from '../context/IndicatorsContext';
import IndicatorContext from '../context/CustomContext';
import { OptionsBar, OptionsPie } from './options/ChartsOptions';
import getFormatedQuery from './query/Query';

Expand Down
2 changes: 1 addition & 1 deletion src/components/indicators/JornalsIndicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CHART_BACKGROUD_COLORS, CHART_BORDER_COLORS } from '../../../utils/Util
import ElasticSearchService from '../../services/ElasticSearchService';
import { CustomSearchQuery, IndicatorType } from '../../types/Entities';
import { IndicatorsProps } from '../../types/Propos';
import IndicatorContext from '../context/IndicatorsContext';
import IndicatorContext from '../context/CustomContext';
import { OptionsBar } from './options/ChartsOptions';
import getFormatedQuery from './query/Query';

Expand Down
2 changes: 1 addition & 1 deletion src/components/indicators/OrgUnitIndicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CHART_BACKGROUD_COLORS, CHART_BORDER_COLORS } from '../../../utils/Util
import ElasticSearchService from '../../services/ElasticSearchService';
import { CustomSearchQuery, IndicatorType } from '../../types/Entities';
import { IndicatorsProps } from '../../types/Propos';
import IndicatorContext from '../context/IndicatorsContext';
import IndicatorContext from '../context/CustomContext';
import { OptionsBar } from './options/ChartsOptions';
import getFormatedQuery from './query/Query';

Expand Down
2 changes: 1 addition & 1 deletion src/components/indicators/PatentsIndicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CHART_BACKGROUD_COLORS, CHART_BORDER_COLORS } from '../../../utils/Util
import ElasticSearchService from '../../services/ElasticSearchService';
import { CustomSearchQuery, IndicatorType } from '../../types/Entities';
import { IndicatorsProps } from '../../types/Propos';
import IndicatorContext from '../context/IndicatorsContext';
import IndicatorContext from '../context/CustomContext';
import { OptionsBar, OptionsPie } from './options/ChartsOptions';
import getFormatedQuery from './query/Query';

Expand Down
2 changes: 1 addition & 1 deletion src/components/indicators/PeopleIndicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useTranslation } from 'next-i18next';
import { CHART_BACKGROUD_COLORS, CHART_BORDER_COLORS } from '../../../utils/Utils';
import { CustomSearchQuery, IndicatorType } from '../../types/Entities';
import { IndicatorsProps } from '../../types/Propos';
import IndicatorContext from '../context/IndicatorsContext';
import IndicatorContext from '../context/CustomContext';
import { OptionsPie } from './options/ChartsOptions';
import getFormatedQuery from './query/Query';

Expand Down
2 changes: 1 addition & 1 deletion src/components/indicators/ProgramsIndicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CHART_BACKGROUD_COLORS, CHART_BORDER_COLORS } from '../../../utils/Util
import ElasticSearchService from '../../services/ElasticSearchService';
import { CustomSearchQuery, IndicatorType } from '../../types/Entities';
import { IndicatorsProps } from '../../types/Propos';
import IndicatorContext from '../context/IndicatorsContext';
import IndicatorContext from '../context/CustomContext';
import { OptionsBar } from './options/ChartsOptions';
import getFormatedQuery from './query/Query';

Expand Down
4 changes: 2 additions & 2 deletions src/components/indicators/PublicationsIndicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CHART_BACKGROUD_COLORS, CHART_BORDER_COLORS } from '../../../utils/Util
import ElasticSearchService from '../../services/ElasticSearchService';
import { CustomSearchQuery, IndicatorType } from '../../types/Entities';
import { IndicatorsProps } from '../../types/Propos';
import IndicatorContext from '../context/IndicatorsContext';
import IndicatorContext from '../context/CustomContext';
import { OptionsBar, OptionsPie } from './options/ChartsOptions';
import getFormatedQuery from './query/Query';

Expand Down Expand Up @@ -48,7 +48,7 @@ function PublicationsIndicators({ filters, searchTerm, isLoading }: IndicatorsPr
options.plugins.title.text = t(options.title);
// @ts-ignore
optionsType.plugins.title.text = t(optionsType.title);

console.log(isLoading);
isLoading
? ElasticSearchService(
[
Expand Down
2 changes: 1 addition & 1 deletion src/components/indicators/SoftwaresIndicators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CHART_BACKGROUD_COLORS, CHART_BORDER_COLORS } from '../../../utils/Util
import ElasticSearchService from '../../services/ElasticSearchService';
import { CustomSearchQuery, IndicatorType } from '../../types/Entities';
import { IndicatorsProps } from '../../types/Propos';
import IndicatorContext from '../context/IndicatorsContext';
import IndicatorContext from '../context/CustomContext';
import { OptionsBar, OptionsPie } from './options/ChartsOptions';
import getFormatedQuery from './query/Query';

Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function builConnector(index: string) {
// transforming the query before sending to Elasticsearch using the requestState and queryConfig
const searchFields: any = queryConfig.search_fields;
// @ts-ignore
if (queryConfig.advanced) {
if (requestState.searchTerm.indexOf('=') > 0) {
if (requestState.searchTerm.indexOf('(') >= 0) {
let fullQuery = stringify(parseElasticsearchQuery(requestState.searchTerm));
fullQuery = fullQuery.replace(',null', '');
Expand Down
Loading

0 comments on commit 2134847

Please sign in to comment.