Skip to content
This repository has been archived by the owner on Mar 18, 2020. It is now read-only.

Busca pelo nome das localidades #2

Open
wants to merge 1 commit into
base: master
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
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"react-dom": "^16.10.2",
"react-helmet": "^5.2.1",
"rebass": "^4.0.6",
"string-similarity": "^3.0.0",
"theme-ui": "^0.2.44"
}
}
38 changes: 31 additions & 7 deletions src/components/LocalidadeList.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from 'react'
import { Box, Heading, Flex, Link, Text } from 'rebass'
import { Label, Select } from '@rebass/forms'
import { Label, Select, Input } from '@rebass/forms'
import stringSimilarity from 'string-similarity';

import localidadesUnfiltered from '../data/localidades.json'
import Localidade from './LocalidadeListItem'
Expand All @@ -25,14 +26,27 @@ const estados = [...new Set(localidades.map(l => l.estado))]

const LocalidadeList = () => {
const [estado, setEstado] = useState('Todos')
const [busca, setBusca] = useState('')

const handleChange = event => {
const handleStateChange = event => {
setEstado(event.target.value)
}

const locFiltrado = localidades.filter(
l => estado === 'Todos' || l.estado === estado,
)
const handleSearchChange = event => {
setBusca(event.target.value)
}

const locFiltrado = localidades.filter(l => {
return (
(estado === 'Todos' || l.estado === estado) &&
(busca
? stringSimilarity.compareTwoStrings(
busca.toLocaleLowerCase(),
l.nome.toLocaleLowerCase(),
) > 0.4
: true)
)
})

return (
<>
Expand All @@ -47,13 +61,13 @@ const LocalidadeList = () => {
</Link>
</Text>

<Box my={3} mb={5}>
<Box my={3} mb={2}>
<Label htmlFor="estado">Estado</Label>
<Select
value={estado}
id="estado"
name="estado"
onChange={handleChange}
onChange={handleStateChange}
>
<option>Todos</option>
{estados.map(e => (
Expand All @@ -64,6 +78,16 @@ const LocalidadeList = () => {
</Select>
</Box>

<Box my={3} mb={5}>
<Label htmlFor="busca">Buscar</Label>
Copy link

@enieber enieber Oct 27, 2019

Choose a reason for hiding this comment

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

talvez mudar o label de Buscar para Nome seria interessante

Copy link
Author

Choose a reason for hiding this comment

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

Boa ideia

<Input
value={busca}
id="busca"
name="busca"
onChange={handleSearchChange}
/>
</Box>

<Flex flexWrap="wrap" m={-1} justifyContent="center">
{locFiltrado.map(l => (
<Localidade key={`${l.lat}_${l.long}`} {...l} />
Expand Down