2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2020-04-04 20:17:55 +02:00
|
|
|
import {FlatList, Image, Platform, ScrollView, View} from "react-native";
|
2019-06-27 10:17:51 +02:00
|
|
|
import i18n from "i18n-js";
|
2020-04-02 10:07:20 +02:00
|
|
|
import CustomModal from "../../components/Custom/CustomModal";
|
2020-04-04 20:17:55 +02:00
|
|
|
import {IconButton, RadioButton, Searchbar, Subheading, Text, Title, withTheme} from "react-native-paper";
|
|
|
|
import {stringMatchQuery} from "../../utils/Search";
|
|
|
|
import ProximoListItem from "../../components/Lists/ProximoListItem";
|
2019-06-27 10:17:51 +02:00
|
|
|
|
|
|
|
function sortPrice(a, b) {
|
|
|
|
return a.price - b.price;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortPriceReverse(a, b) {
|
|
|
|
return b.price - a.price;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortName(a, b) {
|
2020-03-07 22:31:08 +01:00
|
|
|
if (a.name.toLowerCase() < b.name.toLowerCase())
|
2019-06-27 10:17:51 +02:00
|
|
|
return -1;
|
2020-03-07 22:31:08 +01:00
|
|
|
if (a.name.toLowerCase() > b.name.toLowerCase())
|
2019-06-27 10:17:51 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortNameReverse(a, b) {
|
2020-03-07 22:31:08 +01:00
|
|
|
if (a.name.toLowerCase() < b.name.toLowerCase())
|
2019-06-27 10:17:51 +02:00
|
|
|
return 1;
|
2020-03-07 22:31:08 +01:00
|
|
|
if (a.name.toLowerCase() > b.name.toLowerCase())
|
2019-06-27 10:17:51 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
type Props = {
|
2020-03-05 19:54:56 +01:00
|
|
|
navigation: Object,
|
|
|
|
route: Object,
|
2019-06-29 13:37:21 +02:00
|
|
|
}
|
2019-06-27 10:17:51 +02:00
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
type State = {
|
2020-03-07 22:31:08 +01:00
|
|
|
currentSortMode: number,
|
|
|
|
modalCurrentDisplayItem: React.Node,
|
2020-04-04 20:17:55 +02:00
|
|
|
currentSearchString: string,
|
2019-06-29 13:37:21 +02:00
|
|
|
};
|
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Class defining proximo's article list of a certain category.
|
|
|
|
*/
|
2020-03-09 23:15:13 +01:00
|
|
|
class ProximoListScreen extends React.Component<Props, State> {
|
2019-06-29 13:37:21 +02:00
|
|
|
|
2020-03-08 14:19:47 +01:00
|
|
|
modalRef: Object;
|
2020-04-04 20:17:55 +02:00
|
|
|
listData: Array<Object>;
|
2020-03-05 19:54:56 +01:00
|
|
|
shouldFocusSearchBar: boolean;
|
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
colors: Object;
|
|
|
|
|
|
|
|
constructor(props) {
|
2020-01-31 16:51:43 +01:00
|
|
|
super(props);
|
2020-04-04 20:17:55 +02:00
|
|
|
this.listData = this.props.route.params['data']['data'];
|
2020-03-05 19:54:56 +01:00
|
|
|
this.shouldFocusSearchBar = this.props.route.params['shouldFocusSearchBar'];
|
|
|
|
this.state = {
|
2020-04-04 20:17:55 +02:00
|
|
|
currentSearchString: '',
|
2020-03-12 12:31:02 +01:00
|
|
|
currentSortMode: 3,
|
2020-03-08 14:29:57 +01:00
|
|
|
modalCurrentDisplayItem: null,
|
2020-03-05 19:54:56 +01:00
|
|
|
};
|
2020-02-23 18:15:30 +01:00
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
this.colors = props.theme.colors;
|
2020-01-31 16:51:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 14:29:57 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-29 15:59:25 +02:00
|
|
|
* Creates the header content
|
2020-03-08 14:29:57 +01:00
|
|
|
*/
|
|
|
|
componentDidMount() {
|
|
|
|
this.props.navigation.setOptions({
|
2020-04-04 20:17:55 +02:00
|
|
|
headerRight: this.getSortMenuButton,
|
|
|
|
headerTitle: this.getSearchBar,
|
2020-03-10 21:26:14 +01:00
|
|
|
headerBackTitleVisible: false,
|
|
|
|
headerTitleContainerStyle: Platform.OS === 'ios' ?
|
|
|
|
{marginHorizontal: 0, width: '70%'} :
|
|
|
|
{marginHorizontal: 0, right: 50, left: 50},
|
2020-03-08 14:29:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
2020-03-29 15:59:25 +02:00
|
|
|
* Gets the header search bar
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2020-04-04 20:17:55 +02:00
|
|
|
getSearchBar = () => {
|
2020-03-29 15:59:25 +02:00
|
|
|
return (
|
|
|
|
<Searchbar
|
|
|
|
placeholder={i18n.t('proximoScreen.search')}
|
|
|
|
onChangeText={this.onSearchStringChange}
|
|
|
|
/>
|
|
|
|
);
|
2020-04-04 20:17:55 +02:00
|
|
|
};
|
2020-03-29 15:59:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the sort menu header button
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2020-04-04 20:17:55 +02:00
|
|
|
getSortMenuButton = () => {
|
2020-03-29 15:59:25 +02:00
|
|
|
return (
|
|
|
|
<IconButton
|
|
|
|
icon="sort"
|
|
|
|
color={this.colors.text}
|
|
|
|
size={26}
|
|
|
|
onPress={this.onSortMenuPress}
|
|
|
|
/>
|
|
|
|
);
|
2020-04-04 20:17:55 +02:00
|
|
|
};
|
2020-03-29 15:59:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used when clicking on the sort menu button.
|
|
|
|
* It will open the modal to show a sort selection
|
|
|
|
*/
|
2020-04-04 20:17:55 +02:00
|
|
|
onSortMenuPress = () => {
|
2020-03-29 15:59:25 +02:00
|
|
|
this.setState({
|
|
|
|
modalCurrentDisplayItem: this.getModalSortMenu()
|
|
|
|
});
|
|
|
|
if (this.modalRef) {
|
|
|
|
this.modalRef.open();
|
|
|
|
}
|
2020-04-04 20:17:55 +02:00
|
|
|
};
|
2020-03-29 15:59:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current sort mode.
|
2019-06-29 15:43:57 +02:00
|
|
|
*
|
2020-03-07 22:31:08 +01:00
|
|
|
* @param mode The number representing the mode
|
2019-06-29 15:43:57 +02:00
|
|
|
*/
|
2020-03-07 22:31:08 +01:00
|
|
|
setSortMode(mode: number) {
|
2019-06-27 10:17:51 +02:00
|
|
|
this.setState({
|
|
|
|
currentSortMode: mode,
|
|
|
|
});
|
|
|
|
switch (mode) {
|
2020-03-07 22:31:08 +01:00
|
|
|
case 1:
|
2020-04-04 20:17:55 +02:00
|
|
|
this.listData.sort(sortPrice);
|
2020-03-07 22:31:08 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2020-04-04 20:17:55 +02:00
|
|
|
this.listData.sort(sortPriceReverse);
|
2019-06-27 10:17:51 +02:00
|
|
|
break;
|
2020-03-07 22:31:08 +01:00
|
|
|
case 3:
|
2020-04-04 20:17:55 +02:00
|
|
|
this.listData.sort(sortName);
|
2019-06-27 10:17:51 +02:00
|
|
|
break;
|
2020-03-07 22:31:08 +01:00
|
|
|
case 4:
|
2020-04-04 20:17:55 +02:00
|
|
|
this.listData.sort(sortNameReverse);
|
2020-03-07 22:31:08 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-03-08 14:19:47 +01:00
|
|
|
if (this.modalRef && mode !== this.state.currentSortMode) {
|
|
|
|
this.modalRef.close();
|
2019-06-27 10:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
2020-03-29 15:59:25 +02:00
|
|
|
* Gets a color depending on the quantity available
|
2019-08-06 13:28:11 +02:00
|
|
|
*
|
2020-03-29 15:59:25 +02:00
|
|
|
* @param availableStock The quantity available
|
2019-08-06 13:28:11 +02:00
|
|
|
* @return
|
|
|
|
*/
|
2019-08-05 23:02:33 +02:00
|
|
|
getStockColor(availableStock: number) {
|
2019-08-06 13:28:11 +02:00
|
|
|
let color: string;
|
2019-08-05 23:02:33 +02:00
|
|
|
if (availableStock > 3)
|
2020-03-09 23:15:13 +01:00
|
|
|
color = this.colors.success;
|
2019-08-05 23:02:33 +02:00
|
|
|
else if (availableStock > 0)
|
2020-03-09 23:15:13 +01:00
|
|
|
color = this.colors.warning;
|
2019-08-05 23:02:33 +02:00
|
|
|
else
|
2020-03-09 23:15:13 +01:00
|
|
|
color = this.colors.danger;
|
2019-08-05 23:02:33 +02:00
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Callback used when the search changes
|
|
|
|
*
|
|
|
|
* @param str The new search string
|
|
|
|
*/
|
2020-04-04 20:17:55 +02:00
|
|
|
onSearchStringChange = (str: string) => {
|
|
|
|
this.setState({currentSearchString: str})
|
|
|
|
};
|
2019-11-15 18:08:47 +01:00
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Gets the modal content depending on the given article
|
|
|
|
*
|
|
|
|
* @param item The article to display
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2020-03-07 22:31:08 +01:00
|
|
|
getModalItemContent(item: Object) {
|
2019-09-12 12:01:01 +02:00
|
|
|
return (
|
|
|
|
<View style={{
|
|
|
|
flex: 1,
|
|
|
|
padding: 20
|
|
|
|
}}>
|
2020-03-07 22:31:08 +01:00
|
|
|
<Title>{item.name}</Title>
|
2019-09-12 12:01:01 +02:00
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
width: '100%',
|
|
|
|
marginTop: 10,
|
|
|
|
}}>
|
2020-03-06 23:15:01 +01:00
|
|
|
<Subheading style={{
|
2020-03-07 22:31:08 +01:00
|
|
|
color: this.getStockColor(parseInt(item.quantity)),
|
2019-09-12 12:01:01 +02:00
|
|
|
}}>
|
2020-03-07 22:31:08 +01:00
|
|
|
{item.quantity + ' ' + i18n.t('proximoScreen.inStock')}
|
2020-03-06 23:15:01 +01:00
|
|
|
</Subheading>
|
2020-03-07 22:31:08 +01:00
|
|
|
<Subheading style={{marginLeft: 'auto'}}>{item.price}€</Subheading>
|
2019-09-12 12:01:01 +02:00
|
|
|
</View>
|
|
|
|
|
2020-03-06 23:15:01 +01:00
|
|
|
<ScrollView>
|
2019-09-12 12:01:01 +02:00
|
|
|
<View style={{width: '100%', height: 150, marginTop: 20, marginBottom: 20}}>
|
|
|
|
<Image style={{flex: 1, resizeMode: "contain"}}
|
2020-03-07 22:31:08 +01:00
|
|
|
source={{uri: item.image}}/>
|
2019-09-12 12:01:01 +02:00
|
|
|
</View>
|
2020-03-07 22:31:08 +01:00
|
|
|
<Text>{item.description}</Text>
|
2020-03-06 23:15:01 +01:00
|
|
|
</ScrollView>
|
2019-09-12 12:01:01 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Gets the modal content to display a sort menu
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2020-03-07 22:31:08 +01:00
|
|
|
getModalSortMenu() {
|
|
|
|
return (
|
|
|
|
<View style={{
|
|
|
|
flex: 1,
|
|
|
|
padding: 20
|
|
|
|
}}>
|
2020-03-08 14:48:44 +01:00
|
|
|
<Title style={{marginBottom: 10}}>{i18n.t('proximoScreen.sortOrder')}</Title>
|
2020-03-07 22:31:08 +01:00
|
|
|
<RadioButton.Group
|
|
|
|
onValueChange={value => this.setSortMode(value)}
|
|
|
|
value={this.state.currentSortMode}
|
|
|
|
>
|
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
2020-03-08 14:41:04 +01:00
|
|
|
justifyContent: 'flex-start',
|
2020-03-07 22:31:08 +01:00
|
|
|
alignItems: 'center'
|
|
|
|
}}>
|
|
|
|
<RadioButton value={1}/>
|
2020-03-08 14:41:04 +01:00
|
|
|
<Text>{i18n.t('proximoScreen.sortPrice')}</Text>
|
2020-03-07 22:31:08 +01:00
|
|
|
</View>
|
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
2020-03-08 14:41:04 +01:00
|
|
|
justifyContent: 'flex-start',
|
2020-03-07 22:31:08 +01:00
|
|
|
alignItems: 'center'
|
|
|
|
}}>
|
|
|
|
<RadioButton value={2}/>
|
2020-03-08 14:41:04 +01:00
|
|
|
<Text>{i18n.t('proximoScreen.sortPriceReverse')}</Text>
|
2020-03-07 22:31:08 +01:00
|
|
|
</View>
|
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
2020-03-08 14:41:04 +01:00
|
|
|
justifyContent: 'flex-start',
|
2020-03-07 22:31:08 +01:00
|
|
|
alignItems: 'center'
|
|
|
|
}}>
|
|
|
|
<RadioButton value={3}/>
|
2020-03-08 14:41:04 +01:00
|
|
|
<Text>{i18n.t('proximoScreen.sortName')}</Text>
|
2020-03-07 22:31:08 +01:00
|
|
|
</View>
|
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
2020-03-08 14:41:04 +01:00
|
|
|
justifyContent: 'flex-start',
|
2020-03-07 22:31:08 +01:00
|
|
|
alignItems: 'center'
|
|
|
|
}}>
|
|
|
|
<RadioButton value={4}/>
|
2020-03-08 14:41:04 +01:00
|
|
|
<Text>{i18n.t('proximoScreen.sortNameReverse')}</Text>
|
2020-03-07 22:31:08 +01:00
|
|
|
</View>
|
|
|
|
</RadioButton.Group>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Callback used when clicking an article in the list.
|
|
|
|
* It opens the modal to show detailed information about the article
|
|
|
|
*
|
|
|
|
* @param item The article pressed
|
|
|
|
*/
|
2020-02-23 21:47:36 +01:00
|
|
|
onListItemPress(item: Object) {
|
2019-09-12 12:01:01 +02:00
|
|
|
this.setState({
|
2020-03-07 22:31:08 +01:00
|
|
|
modalCurrentDisplayItem: this.getModalItemContent(item)
|
2019-09-12 12:01:01 +02:00
|
|
|
});
|
2020-03-08 14:19:47 +01:00
|
|
|
if (this.modalRef) {
|
|
|
|
this.modalRef.open();
|
2019-09-12 12:01:01 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:01:07 +02:00
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Gets a render item for the given article
|
|
|
|
*
|
|
|
|
* @param item The article to render
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2020-04-04 20:17:55 +02:00
|
|
|
renderItem = ({item}: Object) => {
|
|
|
|
if (stringMatchQuery(item.name, this.state.currentSearchString)) {
|
|
|
|
const onPress = this.onListItemPress.bind(this, item);
|
|
|
|
const color = this.getStockColor(parseInt(item.quantity));
|
|
|
|
return (
|
|
|
|
<ProximoListItem
|
|
|
|
item={item}
|
|
|
|
onPress={onPress}
|
|
|
|
color={color}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else
|
|
|
|
return null;
|
|
|
|
};
|
2020-02-11 01:05:24 +01:00
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Extracts a key for the given article
|
|
|
|
*
|
|
|
|
* @param item The article to extract the key from
|
|
|
|
* @return {*} The extracted key
|
|
|
|
*/
|
2020-02-23 18:15:30 +01:00
|
|
|
keyExtractor(item: Object) {
|
2020-02-11 01:05:24 +01:00
|
|
|
return item.name + item.code;
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Callback used when receiving the modal ref
|
|
|
|
*
|
|
|
|
* @param ref
|
|
|
|
*/
|
2020-04-04 20:17:55 +02:00
|
|
|
onModalRef = (ref: Object) => {
|
2020-03-08 14:19:47 +01:00
|
|
|
this.modalRef = ref;
|
2020-04-04 20:17:55 +02:00
|
|
|
};
|
2020-03-08 14:19:47 +01:00
|
|
|
|
2019-06-27 10:17:51 +02:00
|
|
|
render() {
|
|
|
|
return (
|
2020-03-07 22:52:55 +01:00
|
|
|
<View style={{
|
|
|
|
height: '100%'
|
|
|
|
}}>
|
2020-03-08 14:19:47 +01:00
|
|
|
<CustomModal onRef={this.onModalRef}>
|
2020-03-07 22:31:08 +01:00
|
|
|
{this.state.modalCurrentDisplayItem}
|
2020-03-08 14:19:47 +01:00
|
|
|
</CustomModal>
|
2020-04-04 20:17:55 +02:00
|
|
|
{/*$FlowFixMe*/}
|
|
|
|
<FlatList
|
|
|
|
data={this.listData}
|
|
|
|
extraData={this.state.currentSearchString + this.state.currentSortMode}
|
2020-02-11 01:05:24 +01:00
|
|
|
keyExtractor={this.keyExtractor}
|
|
|
|
renderItem={this.renderItem}
|
2020-01-26 19:27:20 +01:00
|
|
|
/>
|
2020-03-06 09:12:56 +01:00
|
|
|
</View>
|
2019-06-27 10:17:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 23:15:13 +01:00
|
|
|
|
|
|
|
export default withTheme(ProximoListScreen);
|