import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import algoliasearch from 'algoliasearch/lite';
import {
InstantSearch,
connectRefinementList,
connectSearchBox,
connectRange,
connectMenu,
} from 'react-instantsearch-native';
import MultiSlider from '@ptomasroos/react-native-multi-slider';
import Stats from './components/Stats';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
const styles = StyleSheet.create({
mainContainer: {
backgroundColor: 'white',
flexGrow: 1,
},
container: {
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'center',
},
});
class Filters extends Component {
static displayName = 'React Native example';
constructor(props) {
super(props);
this.onSearchStateChange = this.onSearchStateChange.bind(this);
this.state = {
searchState: props.searchState,
};
}
onSearchStateChange(nextState) {
const searchState = { ...this.state.searchState, ...nextState };
this.setState({ searchState });
this.props.onSearchStateChange(searchState);
}
render() {
return (
);
}
}
Filters.propTypes = {
searchState: PropTypes.object.isRequired,
onSearchStateChange: PropTypes.func.isRequired,
};
export default Filters;
class Range extends React.Component {
constructor(props) {
super(props);
this.state = {
currentValues: {
min: this.props.min,
max: this.props.max,
},
};
}
componentDidUpdate(prevProps) {
if (
this.props.canRefine &&
(prevProps.currentRefinement.min !== this.props.currentRefinement.min ||
prevProps.currentRefinement.max !== this.props.currentRefinement.max)
) {
this.setState({
currentValues: {
min: this.props.currentRefinement.min,
max: this.props.currentRefinement.max,
},
});
}
}
sliderOneValuesChange = (sliderState) => {
this.setState({
currentValues: { min: sliderState[0], max: sliderState[1] },
});
};
sliderOneValuesChangeFinish = (sliderState) => {
if (
this.props.currentRefinement.min !== sliderState[0] ||
this.props.currentRefinement.max !== sliderState[1]
) {
this.props.refine({
min: sliderState[0],
max: sliderState[1],
});
}
};
render() {
const slider =
this.props.min !== this.props.max ? (
) : null;
const content =
this.props.min !== this.props.max ? (
$ {Math.trunc(this.state.currentValues.min)}
{slider}
$ {Math.trunc(this.state.currentValues.max)}
) : (
{this.props.min
? `$ ${Math.trunc(this.state.currentValues.min)}`
: null}
);
return {content};
}
}
Range.propTypes = {
min: PropTypes.number,
max: PropTypes.number,
currentRefinement: PropTypes.shape({
min: PropTypes.number,
max: PropTypes.number,
}),
refine: PropTypes.func.isRequired,
canRefine: PropTypes.bool.isRequired,
};
const VirtualRefinementList = connectRefinementList(() => null);
const VirtualSearchBox = connectSearchBox(() => null);
const VirtualMenu = connectMenu(() => null);
const ConnectedRange = connectRange(Range);