2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2020-03-06 09:12:56 +01:00
|
|
|
import {Body, Content, H1, H3, Input, Item, Left, ListItem, Right, Text, Thumbnail} from 'native-base';
|
2020-01-31 16:51:43 +01:00
|
|
|
import {FlatList, Image, Platform, View} from "react-native";
|
2019-06-27 10:17:51 +02:00
|
|
|
import Touchable from 'react-native-platform-touchable';
|
2019-06-29 13:37:21 +02:00
|
|
|
import Menu, {MenuItem} from 'react-native-material-menu';
|
2019-06-27 10:17:51 +02:00
|
|
|
import i18n from "i18n-js";
|
2020-03-05 21:48:37 +01:00
|
|
|
import {MaterialCommunityIcons} from "@expo/vector-icons";
|
2019-08-05 14:05:51 +02:00
|
|
|
import ThemeManager from "../../utils/ThemeManager";
|
2020-01-26 17:51:15 +01:00
|
|
|
import {Modalize} from 'react-native-modalize';
|
2019-06-27 10:17:51 +02:00
|
|
|
|
|
|
|
const sortMode = {
|
|
|
|
price: "0",
|
|
|
|
name: '1',
|
|
|
|
};
|
|
|
|
|
|
|
|
function sortPrice(a, b) {
|
|
|
|
return a.price - b.price;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortPriceReverse(a, b) {
|
|
|
|
return b.price - a.price;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortName(a, b) {
|
|
|
|
if (a.name < b.name)
|
|
|
|
return -1;
|
|
|
|
if (a.name > b.name)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortNameReverse(a, b) {
|
|
|
|
if (a.name < b.name)
|
|
|
|
return 1;
|
|
|
|
if (a.name > b.name)
|
|
|
|
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 = {
|
|
|
|
currentSortMode: string,
|
|
|
|
isSortReversed: boolean,
|
|
|
|
sortPriceIcon: React.Node,
|
|
|
|
sortNameIcon: React.Node,
|
2019-09-12 12:01:01 +02:00
|
|
|
modalCurrentDisplayItem: Object,
|
2019-11-15 18:08:47 +01:00
|
|
|
currentlyDisplayedData: Array<Object>,
|
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.
|
|
|
|
*/
|
|
|
|
export default class ProximoListScreen extends React.Component<Props, State> {
|
2019-06-29 13:37:21 +02:00
|
|
|
|
2019-11-15 15:58:05 +01:00
|
|
|
modalRef: { current: null | Modalize };
|
2019-11-15 18:08:47 +01:00
|
|
|
originalData: Array<Object>;
|
2020-03-05 19:54:56 +01:00
|
|
|
shouldFocusSearchBar: boolean;
|
|
|
|
|
2020-02-23 18:15:30 +01:00
|
|
|
sortMenuRef: Menu;
|
|
|
|
|
|
|
|
onMenuRef: Function;
|
|
|
|
onSearchStringChange: Function;
|
|
|
|
onSelectSortModeName: Function;
|
|
|
|
onSelectSortModePrice: Function;
|
|
|
|
onSortMenuPress: Function;
|
|
|
|
renderItem: Function;
|
2019-06-27 10:17:51 +02:00
|
|
|
|
2020-01-31 16:51:43 +01:00
|
|
|
constructor(props: any) {
|
|
|
|
super(props);
|
|
|
|
this.modalRef = React.createRef();
|
2020-03-05 19:54:56 +01:00
|
|
|
this.originalData = this.props.route.params['data']['data'];
|
|
|
|
this.shouldFocusSearchBar = this.props.route.params['shouldFocusSearchBar'];
|
|
|
|
this.state = {
|
|
|
|
currentlyDisplayedData: this.originalData,
|
|
|
|
currentSortMode: sortMode.price,
|
|
|
|
isSortReversed: false,
|
|
|
|
sortPriceIcon: '',
|
|
|
|
sortNameIcon: '',
|
|
|
|
modalCurrentDisplayItem: {},
|
|
|
|
};
|
2020-02-23 18:15:30 +01:00
|
|
|
|
|
|
|
this.onMenuRef = this.onMenuRef.bind(this);
|
|
|
|
this.onSearchStringChange = this.onSearchStringChange.bind(this);
|
|
|
|
this.onSelectSortModeName = this.onSelectSortModeName.bind(this);
|
|
|
|
this.onSelectSortModePrice = this.onSelectSortModePrice.bind(this);
|
|
|
|
this.onSortMenuPress = this.onSortMenuPress.bind(this);
|
2020-02-11 01:05:24 +01:00
|
|
|
this.renderItem = this.renderItem.bind(this);
|
2020-01-31 16:51:43 +01:00
|
|
|
}
|
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Saves the reference to the sort menu for later use
|
|
|
|
*
|
|
|
|
* @param ref The menu reference
|
|
|
|
*/
|
2020-02-23 18:15:30 +01:00
|
|
|
onMenuRef(ref: Menu) {
|
|
|
|
this.sortMenuRef = ref;
|
2019-06-27 10:17:51 +02:00
|
|
|
};
|
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Sets the sort mode based on the one selected.
|
|
|
|
* If the selected mode is the current one, reverse it.
|
|
|
|
*
|
|
|
|
* @param mode The string representing the mode
|
|
|
|
*/
|
|
|
|
sortModeSelected(mode: string) {
|
2019-06-27 10:17:51 +02:00
|
|
|
let isReverse = this.state.isSortReversed;
|
|
|
|
if (mode === this.state.currentSortMode) // reverse mode
|
|
|
|
isReverse = !isReverse; // this.state not updating on this function cycle
|
|
|
|
else
|
|
|
|
isReverse = false;
|
|
|
|
this.setSortMode(mode, isReverse);
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Set the current sort mode.
|
|
|
|
*
|
|
|
|
* @param mode The string representing the mode
|
|
|
|
* @param isReverse Whether to use a reverse sort
|
|
|
|
*/
|
2019-06-29 13:37:21 +02:00
|
|
|
setSortMode(mode: string, isReverse: boolean) {
|
2019-06-27 10:17:51 +02:00
|
|
|
this.setState({
|
|
|
|
currentSortMode: mode,
|
|
|
|
isSortReversed: isReverse
|
|
|
|
});
|
2019-11-15 18:08:47 +01:00
|
|
|
let data = this.state.currentlyDisplayedData;
|
2019-06-27 10:17:51 +02:00
|
|
|
switch (mode) {
|
|
|
|
case sortMode.price:
|
|
|
|
if (isReverse) {
|
|
|
|
data.sort(sortPriceReverse);
|
|
|
|
} else {
|
|
|
|
data.sort(sortPrice);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case sortMode.name:
|
|
|
|
if (isReverse) {
|
|
|
|
data.sort(sortNameReverse);
|
|
|
|
} else {
|
|
|
|
data.sort(sortName);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.setupSortIcons(mode, isReverse);
|
2020-03-05 23:40:50 +01:00
|
|
|
if (this.sortMenuRef !== undefined)
|
|
|
|
this.sortMenuRef.hide();
|
2019-06-27 10:17:51 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Set the sort mode from state when components are ready
|
|
|
|
*/
|
2019-06-27 10:17:51 +02:00
|
|
|
componentDidMount() {
|
2020-03-05 23:40:50 +01:00
|
|
|
const button = this.getSortMenu.bind(this);
|
|
|
|
const title = this.getSearchBar.bind(this);
|
|
|
|
this.props.navigation.setOptions({
|
|
|
|
headerRight: button,
|
|
|
|
headerTitle: title,
|
|
|
|
});
|
2019-06-27 10:17:51 +02:00
|
|
|
this.setSortMode(this.state.currentSortMode, this.state.isSortReversed);
|
|
|
|
}
|
|
|
|
|
2020-03-05 23:40:50 +01:00
|
|
|
getSearchBar() {
|
|
|
|
return (
|
|
|
|
<Body style={{width: '100%'}}>
|
|
|
|
<Item
|
|
|
|
style={{
|
|
|
|
width: '100%',
|
|
|
|
marginBottom: 7
|
|
|
|
}}>
|
|
|
|
<MaterialCommunityIcons
|
|
|
|
name={'magnify'}
|
|
|
|
size={26}
|
|
|
|
color={ThemeManager.getCurrentThemeVariables().toolbarBtnColor}/>
|
|
|
|
<Input
|
|
|
|
ref="searchInput"
|
|
|
|
placeholder={i18n.t('proximoScreen.search')}
|
|
|
|
placeholderTextColor={ThemeManager.getCurrentThemeVariables().toolbarPlaceholderColor}
|
|
|
|
onChangeText={this.onSearchStringChange}/>
|
|
|
|
</Item>
|
|
|
|
</Body>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* get color depending on quantity available
|
|
|
|
*
|
|
|
|
* @param availableStock
|
|
|
|
* @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)
|
|
|
|
color = ThemeManager.getCurrentThemeVariables().brandSuccess;
|
|
|
|
else if (availableStock > 0)
|
|
|
|
color = ThemeManager.getCurrentThemeVariables().brandWarning;
|
|
|
|
else
|
|
|
|
color = ThemeManager.getCurrentThemeVariables().brandDanger;
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Set the sort menu icon based on the given mode.
|
|
|
|
*
|
|
|
|
* @param mode The string representing the mode
|
|
|
|
* @param isReverse Whether to use a reversed icon
|
|
|
|
*/
|
2019-06-29 13:37:21 +02:00
|
|
|
setupSortIcons(mode: string, isReverse: boolean) {
|
2019-06-27 10:17:51 +02:00
|
|
|
const downSortIcon =
|
2020-03-05 21:48:37 +01:00
|
|
|
<MaterialCommunityIcons
|
|
|
|
name={'sort-descending'}
|
|
|
|
color={'#000'}
|
|
|
|
size={26}/>;
|
2019-06-27 10:17:51 +02:00
|
|
|
const upSortIcon =
|
2020-03-05 21:48:37 +01:00
|
|
|
<MaterialCommunityIcons
|
|
|
|
name={'sort-ascending'}
|
|
|
|
color={'#000'}
|
|
|
|
size={26}/>;
|
2019-06-27 10:17:51 +02:00
|
|
|
switch (mode) {
|
|
|
|
case sortMode.price:
|
|
|
|
this.setState({sortNameIcon: ''});
|
|
|
|
if (isReverse) {
|
|
|
|
this.setState({sortPriceIcon: upSortIcon});
|
|
|
|
} else {
|
|
|
|
this.setState({sortPriceIcon: downSortIcon});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case sortMode.name:
|
|
|
|
this.setState({sortPriceIcon: ''});
|
|
|
|
if (isReverse) {
|
|
|
|
this.setState({sortNameIcon: upSortIcon});
|
|
|
|
} else {
|
|
|
|
this.setState({sortNameIcon: downSortIcon});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 18:08:47 +01:00
|
|
|
|
|
|
|
sanitizeString(str: string) {
|
|
|
|
return str.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns only the articles whose name contains str. Case and accents insensitive.
|
|
|
|
* @param str
|
|
|
|
* @returns {[]}
|
|
|
|
*/
|
|
|
|
filterData(str: string) {
|
|
|
|
let filteredData = [];
|
|
|
|
const testStr = this.sanitizeString(str);
|
|
|
|
const articles = this.originalData;
|
|
|
|
for (const article of articles) {
|
|
|
|
const name = this.sanitizeString(article.name);
|
|
|
|
if (name.includes(testStr)) {
|
|
|
|
filteredData.push(article)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredData;
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:15:30 +01:00
|
|
|
onSearchStringChange(str: string) {
|
2019-11-15 18:08:47 +01:00
|
|
|
this.setState({
|
|
|
|
currentlyDisplayedData: this.filterData(str)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:01:01 +02:00
|
|
|
getModalContent() {
|
|
|
|
return (
|
|
|
|
<View style={{
|
|
|
|
flex: 1,
|
|
|
|
padding: 20
|
|
|
|
}}>
|
|
|
|
<H1>{this.state.modalCurrentDisplayItem.name}</H1>
|
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
width: '100%',
|
|
|
|
marginTop: 10,
|
|
|
|
}}>
|
|
|
|
<H3 style={{
|
|
|
|
color: this.getStockColor(parseInt(this.state.modalCurrentDisplayItem.quantity)),
|
|
|
|
}}>
|
|
|
|
{this.state.modalCurrentDisplayItem.quantity + ' ' + i18n.t('proximoScreen.inStock')}
|
|
|
|
</H3>
|
|
|
|
<H3 style={{marginLeft: 'auto'}}>{this.state.modalCurrentDisplayItem.price}€</H3>
|
|
|
|
</View>
|
|
|
|
|
|
|
|
<Content>
|
|
|
|
<View style={{width: '100%', height: 150, marginTop: 20, marginBottom: 20}}>
|
|
|
|
<Image style={{flex: 1, resizeMode: "contain"}}
|
|
|
|
source={{uri: this.state.modalCurrentDisplayItem.image}}/>
|
|
|
|
</View>
|
|
|
|
<Text>{this.state.modalCurrentDisplayItem.description}</Text>
|
|
|
|
</Content>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-23 21:47:36 +01:00
|
|
|
onListItemPress(item: Object) {
|
2019-09-12 12:01:01 +02:00
|
|
|
this.setState({
|
|
|
|
modalCurrentDisplayItem: item
|
|
|
|
});
|
|
|
|
if (this.modalRef.current) {
|
|
|
|
this.modalRef.current.open();
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:01:07 +02:00
|
|
|
|
2020-02-23 18:15:30 +01:00
|
|
|
onSelectSortModeName() {
|
2020-02-11 01:05:24 +01:00
|
|
|
this.sortModeSelected(sortMode.name);
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:15:30 +01:00
|
|
|
onSelectSortModePrice() {
|
2020-02-11 01:05:24 +01:00
|
|
|
this.sortModeSelected(sortMode.price);
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:15:30 +01:00
|
|
|
onSortMenuPress() {
|
|
|
|
this.sortMenuRef.show();
|
2020-02-11 01:05:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-15 15:58:05 +01:00
|
|
|
getSortMenu() {
|
|
|
|
return (
|
|
|
|
<Menu
|
2020-02-23 18:15:30 +01:00
|
|
|
ref={this.onMenuRef}
|
2019-11-15 15:58:05 +01:00
|
|
|
button={
|
|
|
|
<Touchable
|
2020-03-05 23:40:50 +01:00
|
|
|
style={{padding: 6, marginRight: 10}}
|
2020-02-23 18:15:30 +01:00
|
|
|
onPress={this.onSortMenuPress}>
|
2020-03-05 21:48:37 +01:00
|
|
|
<MaterialCommunityIcons
|
2019-11-15 15:58:05 +01:00
|
|
|
color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
|
2020-03-05 21:48:37 +01:00
|
|
|
name={'sort'}
|
|
|
|
size={26}/>
|
2019-11-15 15:58:05 +01:00
|
|
|
</Touchable>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<MenuItem
|
2020-02-23 18:15:30 +01:00
|
|
|
onPress={this.onSelectSortModeName}>
|
2019-11-15 15:58:05 +01:00
|
|
|
{this.state.sortNameIcon}
|
|
|
|
{i18n.t('proximoScreen.sortName')}
|
|
|
|
</MenuItem>
|
|
|
|
<MenuItem
|
2020-02-23 18:15:30 +01:00
|
|
|
onPress={this.onSelectSortModePrice}>
|
2019-11-15 15:58:05 +01:00
|
|
|
{this.state.sortPriceIcon}
|
|
|
|
{i18n.t('proximoScreen.sortPrice')}
|
|
|
|
</MenuItem>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:15:30 +01:00
|
|
|
renderItem({item}: Object) {
|
2020-02-25 22:06:02 +01:00
|
|
|
const onListItemPress = this.onListItemPress.bind(this, item);
|
2020-02-11 01:05:24 +01:00
|
|
|
return (<ListItem
|
|
|
|
thumbnail
|
2020-02-25 22:06:02 +01:00
|
|
|
onPress={onListItemPress}
|
2020-02-11 01:05:24 +01:00
|
|
|
>
|
|
|
|
<Left>
|
|
|
|
<Thumbnail square source={{uri: item.image}}/>
|
|
|
|
</Left>
|
|
|
|
<Body>
|
|
|
|
<Text style={{marginLeft: 20}}>
|
|
|
|
{item.name}
|
|
|
|
</Text>
|
|
|
|
<Text note style={{
|
|
|
|
marginLeft: 20,
|
|
|
|
color: this.getStockColor(parseInt(item.quantity))
|
|
|
|
}}>
|
|
|
|
{item.quantity + ' ' + i18n.t('proximoScreen.inStock')}
|
|
|
|
</Text>
|
|
|
|
</Body>
|
|
|
|
<Right>
|
|
|
|
<Text style={{fontWeight: "bold"}}>
|
|
|
|
{item.price}€
|
|
|
|
</Text>
|
|
|
|
</Right>
|
|
|
|
</ListItem>);
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:15:30 +01:00
|
|
|
keyExtractor(item: Object) {
|
2020-02-11 01:05:24 +01:00
|
|
|
return item.name + item.code;
|
|
|
|
}
|
|
|
|
|
2019-06-27 10:17:51 +02:00
|
|
|
render() {
|
2020-02-11 01:05:24 +01:00
|
|
|
// console.log("rendering ProximoListScreen");
|
2019-06-27 10:17:51 +02:00
|
|
|
const nav = this.props.navigation;
|
|
|
|
return (
|
2020-03-06 09:12:56 +01:00
|
|
|
<View>
|
2019-09-12 12:01:01 +02:00
|
|
|
<Modalize ref={this.modalRef}
|
|
|
|
adjustToContentHeight
|
|
|
|
modalStyle={{backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor}}>
|
|
|
|
{this.getModalContent()}
|
|
|
|
</Modalize>
|
2020-01-26 19:27:20 +01:00
|
|
|
<FlatList
|
|
|
|
data={this.state.currentlyDisplayedData}
|
|
|
|
extraData={this.state.currentlyDisplayedData}
|
2020-02-11 01:05:24 +01:00
|
|
|
keyExtractor={this.keyExtractor}
|
2020-01-26 19:27:20 +01:00
|
|
|
style={{minHeight: 300, width: '100%'}}
|
2020-02-11 01:05:24 +01:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|