2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2019-09-12 12:01:01 +02:00
|
|
|
import {Body, Container, Content, Left, ListItem, Right, Text, Thumbnail, H1, H3} from 'native-base';
|
2019-06-27 10:17:51 +02:00
|
|
|
import CustomHeader from "../../components/CustomHeader";
|
2019-09-12 12:01:01 +02:00
|
|
|
import {FlatList, Platform, View, Image} 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";
|
2019-08-05 14:05:51 +02:00
|
|
|
import CustomMaterialIcon from "../../components/CustomMaterialIcon";
|
|
|
|
import ThemeManager from "../../utils/ThemeManager";
|
2019-09-12 12:01:01 +02: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 = {
|
|
|
|
navigation: Object
|
|
|
|
}
|
2019-06-27 10:17:51 +02:00
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
type State = {
|
|
|
|
navData: Array<Object>,
|
|
|
|
currentSortMode: string,
|
|
|
|
isSortReversed: boolean,
|
|
|
|
sortPriceIcon: React.Node,
|
|
|
|
sortNameIcon: React.Node,
|
2019-09-12 12:01:01 +02:00
|
|
|
modalCurrentDisplayItem: 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-09-12 15:01:07 +02:00
|
|
|
modalRef: { current: null | Modalize };
|
|
|
|
|
|
|
|
constructor(props: any) {
|
|
|
|
super(props);
|
|
|
|
this.modalRef = React.createRef();
|
|
|
|
}
|
2019-09-12 12:01:01 +02:00
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
state = {
|
|
|
|
navData: this.props.navigation.getParam('data', []).sort(sortPrice),
|
|
|
|
currentSortMode: sortMode.price,
|
|
|
|
isSortReversed: false,
|
|
|
|
sortPriceIcon: '',
|
|
|
|
sortNameIcon: '',
|
2019-09-12 12:01:01 +02:00
|
|
|
modalCurrentDisplayItem: {}
|
2019-06-29 13:37:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
_menu: Menu;
|
2019-06-27 10:17:51 +02:00
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Saves the reference to the sort menu for later use
|
|
|
|
*
|
|
|
|
* @param ref The menu reference
|
|
|
|
*/
|
2019-06-29 13:37:21 +02:00
|
|
|
setMenuRef = (ref: Menu) => {
|
2019-06-27 10:17:51 +02:00
|
|
|
this._menu = ref;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
let data = this.state.navData;
|
|
|
|
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.setState({
|
|
|
|
navData: data,
|
|
|
|
});
|
|
|
|
this.setupSortIcons(mode, isReverse);
|
|
|
|
this._menu.hide();
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
this.setSortMode(this.state.currentSortMode, this.state.isSortReversed);
|
|
|
|
}
|
|
|
|
|
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 =
|
2019-08-05 14:05:51 +02:00
|
|
|
<CustomMaterialIcon
|
|
|
|
icon={'sort-descending'}/>;
|
2019-06-27 10:17:51 +02:00
|
|
|
const upSortIcon =
|
2019-08-05 14:05:51 +02:00
|
|
|
<CustomMaterialIcon
|
|
|
|
icon={'sort-ascending'}/>;
|
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-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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
showItemDetails(item: Object) {
|
|
|
|
this.setState({
|
|
|
|
modalCurrentDisplayItem: item
|
|
|
|
});
|
|
|
|
if (this.modalRef.current) {
|
|
|
|
this.modalRef.current.open();
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:01:07 +02:00
|
|
|
|
2019-06-27 10:17:51 +02:00
|
|
|
render() {
|
|
|
|
const nav = this.props.navigation;
|
2019-09-12 12:01:01 +02:00
|
|
|
const navType = nav.getParam('type', '{name: "Error"}');
|
2019-06-27 10:17:51 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Container>
|
2019-09-12 12:01:01 +02:00
|
|
|
<Modalize ref={this.modalRef}
|
|
|
|
adjustToContentHeight
|
|
|
|
modalStyle={{backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor}}>
|
|
|
|
{this.getModalContent()}
|
|
|
|
</Modalize>
|
|
|
|
<CustomHeader hasBackButton={true} navigation={nav} title={navType.name} rightButton={
|
2019-08-07 11:33:45 +02:00
|
|
|
<Menu
|
|
|
|
ref={this.setMenuRef}
|
|
|
|
button={
|
|
|
|
<Touchable
|
|
|
|
style={{padding: 6}}
|
|
|
|
onPress={() =>
|
|
|
|
this._menu.show()
|
|
|
|
}>
|
|
|
|
<CustomMaterialIcon
|
|
|
|
color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
|
|
|
|
icon={'sort'}/>
|
|
|
|
</Touchable>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<MenuItem
|
|
|
|
onPress={() => this.sortModeSelected(sortMode.name)}>
|
|
|
|
{this.state.sortNameIcon}
|
|
|
|
{i18n.t('proximoScreen.sortName')}
|
|
|
|
</MenuItem>
|
|
|
|
<MenuItem
|
|
|
|
onPress={() => this.sortModeSelected(sortMode.price)}>
|
|
|
|
{this.state.sortPriceIcon}
|
|
|
|
{i18n.t('proximoScreen.sortPrice')}
|
|
|
|
</MenuItem>
|
|
|
|
</Menu>
|
2019-06-27 10:17:51 +02:00
|
|
|
}/>
|
|
|
|
|
|
|
|
<Content>
|
|
|
|
<FlatList
|
|
|
|
data={this.state.navData}
|
|
|
|
extraData={this.state.navData}
|
2019-09-12 12:01:01 +02:00
|
|
|
keyExtractor={(item) => item.name}
|
2019-06-27 10:17:51 +02:00
|
|
|
style={{minHeight: 300, width: '100%'}}
|
|
|
|
renderItem={({item}) =>
|
|
|
|
<ListItem
|
|
|
|
thumbnail
|
|
|
|
onPress={() => {
|
2019-09-12 12:01:01 +02:00
|
|
|
this.showItemDetails(item);
|
2019-06-27 10:17:51 +02:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Left>
|
2019-08-01 11:04:27 +02:00
|
|
|
<Thumbnail square source={{uri: item.image}}/>
|
2019-06-28 11:35:15 +02:00
|
|
|
</Left>
|
|
|
|
<Body>
|
2019-06-27 10:17:51 +02:00
|
|
|
<Text style={{marginLeft: 20}}>
|
|
|
|
{item.name}
|
|
|
|
</Text>
|
2019-08-05 23:02:33 +02:00
|
|
|
<Text note style={{
|
|
|
|
marginLeft: 20,
|
2019-08-06 13:28:11 +02:00
|
|
|
color: this.getStockColor(parseInt(item.quantity))
|
|
|
|
}}>
|
2019-08-01 11:04:27 +02:00
|
|
|
{item.quantity + ' ' + i18n.t('proximoScreen.inStock')}
|
|
|
|
</Text>
|
2019-06-28 11:35:15 +02:00
|
|
|
</Body>
|
2019-09-12 15:09:28 +02:00
|
|
|
<Right>
|
|
|
|
<Text style={{fontWeight: "bold"}}>
|
2019-06-27 10:17:51 +02:00
|
|
|
{item.price}€
|
|
|
|
</Text>
|
|
|
|
</Right>
|
|
|
|
</ListItem>}
|
|
|
|
/>
|
|
|
|
</Content>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|