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

[FEATURE] Search filters #108

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
10,796 changes: 0 additions & 10,796 deletions package-lock.json

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"react": "17",
"react-dom": "16.13.1",
"react-hook-form": "^6.15.1",
"react-instantsearch-native": "^6.9.0",
"react-instantsearch-native": "^6.10.3",
"react-native": "https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz",
"react-native-dotenv": "^2.5.3",
"react-native-gesture-handler": "^1.8.0",
Expand All @@ -48,7 +48,7 @@
"react-native-screens": "^2.15.0",
"react-native-svg": "12.1.0",
"react-native-web": "~0.13.12",
"react-native-webview": "^11.2.4",
"react-native-webview": "^11.3.2",
"react-native-youtube-iframe": "^1.4.1",
"styled-components": "^5.2.1",
"yup": "^0.32.8"
Expand Down
2 changes: 1 addition & 1 deletion src/components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ interface ModalProps extends ModalBaseProps {
title?: string
titleStyle?: object
children: JSX.Element
height?: number
onClose(): void
height?: number | string
}

const Modal = ({ children, ...props }: ModalProps): JSX.Element => {
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const Root = (): JSX.Element => (
component={Home}
/>
<Tab.Screen
options={{ unmountOnBlur: true }}
name="Search"
component={Search}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Search/components/Card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { formatMoney } from '../../../../utils/formats';
import * as S from './styles';

interface CardProps {
onPress?: () => void
onPress: () => void
title: string
name: string
image: string
Expand Down
65 changes: 58 additions & 7 deletions src/screens/Search/components/SearchList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
import React from 'react';
import { Index } from 'react-instantsearch-native';
import React, { useEffect, useState } from 'react';
import { Index, connectRefinementList } from 'react-instantsearch-native';
import { List } from './styles';

interface SearchListProps {
index: string
type: string
filters?: {
refinementList?: {
[key: string]: any
}
}
}

export default ({ index, type }: SearchListProps): JSX.Element => (
<Index indexName={index}>
<List type={type} />
</Index>
);
interface FilterStates {
[key: string]: any
}

const Refinement = connectRefinementList(() => null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @davioliveiira! Could you show how the filters on this screen are working? 👀

Just a note: these filters are only for the technology index. When we need to list the services, for example, this component would no longer be useful because this refinement list is static on your component now. I suggest you take a look at the web package solution for this problem 🤓

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mateus4k estou subindo um fix para organizar melhor isso, tem algumas coisas que no native são diferentes, pois a lib de algolia não tem suporte a tudo que temos na web 😢


export default ({ index, type, filters }: SearchListProps): JSX.Element => {
const { refinementList } = filters;

const [filtersState, setFiltersState] = useState<FilterStates>({});

useEffect(() => {
if (refinementList) setFiltersState(refinementList);
}, [refinementList]);

return (
<Index indexName={index}>
<List type={type} />
<Refinement
attribute="type"
defaultRefinement={
filtersState?.type ? filtersState.type : []
}
/>
<Refinement
attribute="classification"
defaultRefinement={
filtersState?.classification ? filtersState.classification : []
}
/>
<Refinement
attribute="dimension"
defaultRefinement={
filtersState?.dimension ? filtersState.dimension : []
}
/>
<Refinement
attribute="institution"
defaultRefinement={
filtersState?.institution ? filtersState.institution : []
}
/>
<Refinement
attribute="keywords"
defaultRefinement={
filtersState?.keywords ? filtersState.keywords : []
}
/>
</Index>
);
};
1 change: 1 addition & 0 deletions src/screens/Search/components/SearchList/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const List = connectInfiniteHits(({
contentContainerStyle={{
paddingBottom: 266,
}}
showsVerticalScrollIndicator={false}
/>
)
);
Expand Down
150 changes: 141 additions & 9 deletions src/screens/Search/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
/* eslint-disable no-unused-vars */
/* eslint-disable react/style-prop-object */
/* eslint-disable import/no-unresolved */
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { View, ScrollView } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, connectSearchBox } from 'react-instantsearch-native';
import {
InstantSearch,
connectSearchBox,
connectRefinementList,
} from 'react-instantsearch-native';
import { Feather } from '@expo/vector-icons';
import {
ALGOLIA_APP_ID,
ALGOLIA_ADMIN_KEY,
} from '@env';
import * as S from './styles';
import { Input, Tabs } from '../../components';
import { Input, Tabs, Modal } from '../../components';
import SearchList from './components/SearchList';
import { algoliaIndexes } from '../../utils/algolia';

Expand All @@ -19,24 +25,80 @@ const searchClient = algoliasearch(
ALGOLIA_ADMIN_KEY,
);

const SearchBox = connectSearchBox((props: any): JSX.Element => {
const { refine, currentRefinement } = props;
interface SearchBoxProps {
refine: (...args: any[]) => any;
currentRefinement: string
isSearchStalled: boolean
toggleFilters: () => void
}

return (
const SearchBox = connectSearchBox(({
refine,
currentRefinement,
toggleFilters,
}: SearchBoxProps): JSX.Element => (
<S.InputSearchWrapper>
<Input
type="default"
placeholder="Buscar soluções"
icon={<Feather name="search" size={24} color="#a5a5a5" />}
onChangeText={(text) => refine(text)}
value={currentRefinement}
style={{ flex: 1 }}
/>
<S.SearchFilters onPress={toggleFilters} />
</S.InputSearchWrapper>
));

interface RefinementListProps {
refine: (value: string[]) => any
items: any
filters: {
[key: string]: any
}
}

const RefinementList = connectRefinementList(({
refine,
items,
filters = [],
}: RefinementListProps) => {
const [filteredItems, setFilteredItems] = useState<any>([]);

useEffect(() => {
if (filters.length) setFilteredItems(filters);
}, [filters]);

return (
items.map((item: any) => (
<S.OptionFilter
key={item.value}
onPress={() => refine(item.value)}
active={filteredItems.some((filter: string) => filter === item.label)}
>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<S.OptionFilterCount active={filteredItems.some((filter: string) => filter === item.label)}>
<S.OptionFilterLabel
active={filteredItems.some((filter: string) => filter === item.label)}
style={{ fontSize: 12, lineHeight: 20 }}
>
{item.count}
</S.OptionFilterLabel>
</S.OptionFilterCount>
<S.OptionFilterLabel>{item.label}</S.OptionFilterLabel>
</View>
</S.OptionFilter>
))
);
});

const Search = (): JSX.Element => {
const [tab, setTab] = useState<number>(1);
const indexSearch = [algoliaIndexes.technology, algoliaIndexes.service];

const [searchState, setSearchState] = useState<any>({});
const [showModalFilters, setShowModalFilters] = useState<boolean>(false);

const onChange = (idx: number) => setTab(idx);

return (
Expand All @@ -45,25 +107,95 @@ const Search = (): JSX.Element => {
<InstantSearch
searchClient={searchClient}
indexName={indexSearch[tab]}
searchState={searchState}
onSearchStateChange={setSearchState}
>
<S.Container>
<S.SearchWrapper>
<SearchBox />
<SearchBox toggleFilters={() => setShowModalFilters(true)} />
</S.SearchWrapper>
<Tabs
onSelect={onChange}
tabs={[
{
title: 'Tecnologias',
content: <SearchList index={algoliaIndexes.technology} type="technology" />,
content: (
<SearchList
index={algoliaIndexes.technology}
type="technology"
filters={searchState}
/>
),
},
{
title: 'Serviços',
content: <SearchList index={algoliaIndexes.service} type="service" />,
content: (
<SearchList
index={algoliaIndexes.service}
type="service"
filters={searchState}
/>
),
},
]}
/>
</S.Container>
<Modal
title="Filtros"
animationType="slide"
visible={showModalFilters}
onClose={() => setShowModalFilters(false)}
height="80%"
>
<ScrollView
showsVerticalScrollIndicator={false}
contentContainerStyle={{
paddingBottom: 24,
}}
>
<S.FiltersTitle>Tipo</S.FiltersTitle>
<S.FiltersWrapper>
<RefinementList
searchable
filters={searchState?.refinementList?.type}
defaultRefinement={searchState?.refinementList?.type ?? []}
attribute="type"
/>
</S.FiltersWrapper>
<S.FiltersTitle>Classificação</S.FiltersTitle>
<S.FiltersWrapper>
<RefinementList
searchable
filters={searchState?.refinementList?.classification}
attribute="classification"
/>
</S.FiltersWrapper>
<S.FiltersTitle>Dimensão</S.FiltersTitle>
<S.FiltersWrapper>
<RefinementList
searchable
filters={searchState?.refinementList?.dimension}
attribute="dimension"
/>
</S.FiltersWrapper>
<S.FiltersTitle>Instituição</S.FiltersTitle>
<S.FiltersWrapper>
<RefinementList
searchable
filters={searchState?.refinementList?.institution}
attribute="institution"
/>
</S.FiltersWrapper>
<S.FiltersTitle>Palavras-chave</S.FiltersTitle>
<S.FiltersWrapper>
<RefinementList
searchable
filters={searchState?.refinementList?.keywords}
attribute="keywords"
/>
</S.FiltersWrapper>
</ScrollView>
</Modal>
</InstantSearch>
</S.Wrapper>
);
Expand Down
17 changes: 0 additions & 17 deletions src/screens/Search/styles.ts

This file was deleted.

Loading