Newer
Older
/*Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana
This file is part of Plataforma Integrada MEC.
Plataforma Integrada MEC is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Plataforma Integrada MEC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Plataforma Integrada MEC. If not, see <http://www.gnu.org/licenses/>.*/
Vinícius de Lima Gonçalves
committed
import React, { useState, useEffect, useContext } from 'react'
Vinícius de Lima Gonçalves
committed
import IconSearch from '@material-ui/icons/Search'
Vinícius de Lima Gonçalves
committed
import { RadioGroup, Radio, FormControl, Select, MenuItem, Button, FormControlLabel, TextField, Link } from '@material-ui/core'
Vinícius de Lima Gonçalves
committed
import styled from 'styled-components'
import { Store } from '../Store';
Vinícius de Lima Gonçalves
committed
const dividerStyled = {
background: '#e0e0e0',
width: '1px',
content: "",
display: 'block',
top: '0',
bottom: '0',
right: '0',
minHeight: '70px',
margin: '0 20px'
Vinícius de Lima Gonçalves
committed
}
const DividerVertical = () => <em style={dividerStyled}></em>
const ButtonStyled = styled(Button)`
text-transform: capitalize !important;
`
const IconSearchStyled = styled(IconSearch)`
color: ${props => props.contrast === "" ? "#16b8dd" : "white"};
Vinícius de Lima Gonçalves
committed
`
margin: 0 2vw !important;
.MuiOutlinedInput-root {
&.Mui-focused fieldset {
border-color: ${props => props.contrast === "" ? "#00bcd4" : "yellow"};
}
fieldset {
border-color: ${props => props.contrast === "" ? "#666" : "white"};
}
color: ${props => props.contrast === "" ? "#666" : "white"};
color: ${props => props.contrast === "" ? "#00bcd4" : "yellow"};
}
label.Mui-focused.Mui-error {
color : red;
}
Vinícius de Lima Gonçalves
committed
`
const FormLearnObjControlLabelStyled = styled(FormControlLabel)`
*{
text-transform: uppercase;
color: ${props => props.contrast === "" ? "#ff8a17 !important" : "yellow !important"};
text-decoration: ${props => props.contrast === "" ? "none" : "underline"};
font-weight: bolder;
}
`
const FormCollectionControlLabelStyled = styled(FormControlLabel)`
*{
text-transform: uppercase;
color: ${props => props.contrast === "" ? "#673ab7 !important" : "yellow !important"};
text-decoration: ${props => props.contrast === "" ? "none" : "underline"};
font-weight: bolder;
}
`
const FormUserControlLabelStyled = styled(FormControlLabel)`
*{
text-transform: uppercase;
color: ${props => props.contrast === "" ? "#00bcd4 !important" : "yellow !important"};
text-decoration: ${props => props.contrast === "" ? "none" : "underline"};
font-weight: bolder;
}
Vinícius de Lima Gonçalves
committed
`
Vinícius de Lima Gonçalves
committed
`
Vinícius de Lima Gonçalves
committed
Vinícius de Lima Gonçalves
committed
const SelectStyled = styled(Select)`
margin-right: 2vw;
*{
text-transform: uppercase;
color: ${props => props.contrast === "" ? "#ff8a17 !important" : "yellow !important"};
text-decoration: ${props => props.contrast === "" ? "none" : "underline"};
font-weight: bolder;
}
Vinícius de Lima Gonçalves
committed
`
const MenuItemStyled = styled(MenuItem)`
text-transform: uppercase;
Vinícius de Lima Gonçalves
committed
`
export default function SearchBar(props) {
Vinícius de Lima Gonçalves
committed
Vinícius de Lima Gonçalves
committed
const [searchClass, setSearchClass] = useState('LearningObject')
Vinícius de Lima Gonçalves
committed
Vinícius de Lima Gonçalves
committed
useEffect(() => {
if (window.location.pathname.includes('busca')) {
const urlParams = new URLSearchParams(window.location.search)
const urlQuery = urlParams.get('query')
const urlSearchClass = urlParams.get('search_class')
if (searchClass !== urlSearchClass || query !== urlQuery) {
setQuery(urlQuery)
setSearchClass(urlSearchClass)
}
Vinícius de Lima Gonçalves
committed
}
}, [])
useEffect(() => setGoSearch(false), [goSearch])
const handleChange = (event) => {
setQuery(event.target.value)
Vinícius de Lima Gonçalves
committed
const handleKeyDown = (event) => {
if (event.key === 'Enter' || event.type === 'click') {
dispatch({
type: 'SAVE_SEARCH',
newSearch: {
query: query !== '' ? query : '*',
class: searchClass
}
})
setGoSearch(true)
}
const linkTarget = {
pathname: `/busca?page=0&results_per_page=12&order=review_average&query=${state.search.query}&search_class=${state.search.class}`,
key: uuidv4(), // we could use Math.random, but that's not guaranteed unique.
state: {
applied: true
}
};
return (
<Grid container style={state.contrast === "" ? { paddingTop: "1em" } : { backgroundColor: "black", paddingTop: "1em" }}>
<Grid container item xs={12} sm={6} md={6} lg={6} xl={6}>
{goSearch && <Redirect to={`/busca?page=0&results_per_page=12&order=review_average&query=${state.search.query}&search_class=${state.search.class}`} />}
<TextFieldStyled
contrast={state.contrast}
id="standard-search"
label="O que você está buscando?"
type="search"
margin="normal"
variant="outlined"
value={query}
InputProps={state.contrast === "" ? { className: classes.lightTextField } : { className: classes.darkTextField }}
onChange={handleChange}
onKeyPress={handleKeyDown}
/>
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<Grid container item justify="center" alignItems="center" xs={12} sm={6} md={6} lg={6} xl={6}>
{state.windowSize.width >= 960 ?
<React.Fragment>
<Grid container item justify="center" alignItems="center" xs={12} sm={1} md={1} lg={1} xl={1}>
<Link
to={linkTarget}
>
<ButtonStyled onClick={handleKeyDown} ><IconSearchStyled contrast={state.contrast} /></ButtonStyled>
</Link>
</Grid>
<Grid container item justify="center" alignItems="center" xs={12} sm={3} md={3} lg={3} xl={3}>
<span style={state.contrast === "" ? {} : { color: "white" }}>Pressione "Enter" ou click na lupa</span>
</Grid>
<Grid container item justify="center" alignItems="center" xs={12} sm={1} md={1} lg={1} xl={1}>
<DividerVertical />
</Grid>
<Grid container item justify="center" alignItems="center" xs={12} sm={7} md={7} lg={7} xl={7}>
<RadioGroup row={true}
aria-label="Tipo"
name="types" value={searchClass}
onChange={
(event) => setSearchClass(event.target.value)
}
justify="center" alignItems="center"
>
<FormLearnObjControlLabelStyled contrast={state.contrast} value="LearningObject" control={<RadioLearnObjStyled contrast={state.contrast} />} label="Recursos" />
<FormCollectionControlLabelStyled contrast={state.contrast} value="Collection" control={<RadioCollectionStyled contrast={state.contrast} />} label="Coleções" />
<FormUserControlLabelStyled contrast={state.contrast} value="User" control={<RadioUserStyled contrast={state.contrast} />} label="Usuários" />
</RadioGroup>
</Grid>
</React.Fragment>
:
<React.Fragment>
<Grid container item justify="center" alignItems="center" xs={5} sm={5} md={5} lg={5} xl={5}>
<FormControl>
<SelectStyled
contrast={state.contrast}
value={searchClass}
onChange={(event) => setSearchClass(event.target.value)}
>
<MenuItemStyled style={state.contrast === "" ? { color: "#ff7f00" } : { color: "yellow", backgroundColor: "black", textDecoration: "underline" }} value="LearningObject" aria-label="Recursos">Recursos</MenuItemStyled>
<MenuItemStyled style={state.contrast === "" ? { color: "#673ab7" } : { color: "yellow", backgroundColor: "black", textDecoration: "underline" }} value="Collection" aria-label="Coleções">Coleções</MenuItemStyled>
<MenuItemStyled style={state.contrast === "" ? { color: "#00bcd4" } : { color: "yellow", backgroundColor: "black", textDecoration: "underline" }} value="User" aria-label="Usuários">Usuários</MenuItemStyled>
</SelectStyled>
</FormControl>
</Grid>
<Grid container item justify="center" alignItems="center" xs={2} sm={2} md={2} lg={2} xl={2}>
<DividerVertical />
</Grid>
<Grid container item justify="center" alignItems="center" xs={5} sm={5} md={5} lg={5} xl={5}>
<Link
to={linkTarget}
>
<ButtonStyled onClick={handleKeyDown} ><IconSearchStyled contrast={state.contrast} /></ButtonStyled>
</Link>
</Grid>
</React.Fragment>