2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2020-03-06 23:15:01 +01:00
|
|
|
import {View} from 'react-native'
|
2019-06-27 10:17:51 +02:00
|
|
|
import i18n from "i18n-js";
|
2020-03-05 19:54:56 +01:00
|
|
|
import WebSectionList from "../../components/WebSectionList";
|
2020-03-09 23:15:13 +01:00
|
|
|
import {List, withTheme} from 'react-native-paper';
|
2020-03-07 11:49:32 +01:00
|
|
|
import HeaderButton from "../../components/HeaderButton";
|
2019-06-27 10:17:51 +02:00
|
|
|
|
2020-02-23 17:21:34 +01:00
|
|
|
const DATA_URL = "https://etud.insa-toulouse.fr/~proximo/data/stock-v2.json";
|
2019-06-27 10:17:51 +02:00
|
|
|
|
2020-03-05 19:54:56 +01:00
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
fetchedData: Object,
|
|
|
|
}
|
2019-06-29 13:37:21 +02:00
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Class defining the main proximo screen. This screen shows the different categories of articles
|
|
|
|
* offered by proximo.
|
|
|
|
*/
|
2020-03-09 23:15:13 +01:00
|
|
|
class ProximoMainScreen extends React.Component<Props, State> {
|
2020-03-05 19:54:56 +01:00
|
|
|
|
|
|
|
articles: Object;
|
2019-07-26 14:14:01 +02:00
|
|
|
|
2020-02-23 18:15:30 +01:00
|
|
|
onPressSearchBtn: Function;
|
|
|
|
onPressAboutBtn: Function;
|
2020-03-05 19:54:56 +01:00
|
|
|
getRenderItem: Function;
|
|
|
|
createDataset: Function;
|
2020-02-23 18:15:30 +01:00
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
colors: Object;
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-02-11 01:05:24 +01:00
|
|
|
this.onPressSearchBtn = this.onPressSearchBtn.bind(this);
|
|
|
|
this.onPressAboutBtn = this.onPressAboutBtn.bind(this);
|
2020-03-05 19:54:56 +01:00
|
|
|
this.getRenderItem = this.getRenderItem.bind(this);
|
|
|
|
this.createDataset = this.createDataset.bind(this);
|
2020-03-09 23:15:13 +01:00
|
|
|
this.colors = props.theme.colors;
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
2020-01-31 16:51:43 +01:00
|
|
|
static sortFinalData(a: Object, b: Object) {
|
2020-02-23 17:21:34 +01:00
|
|
|
let str1 = a.type.name.toLowerCase();
|
|
|
|
let str2 = b.type.name.toLowerCase();
|
|
|
|
|
|
|
|
// Make 'All' category with id -1 stick to the top
|
|
|
|
if (a.type.id === -1)
|
|
|
|
return -1;
|
|
|
|
if (b.type.id === -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Sort others by name ascending
|
|
|
|
if (str1 < str2)
|
|
|
|
return -1;
|
|
|
|
if (str1 > str2)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2020-01-31 16:51:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 09:12:56 +01:00
|
|
|
componentDidMount() {
|
|
|
|
const rightButton = this.getRightButton.bind(this);
|
|
|
|
this.props.navigation.setOptions({
|
|
|
|
headerRight: rightButton,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
getKeyExtractor(item: Object) {
|
2019-10-06 12:30:29 +02:00
|
|
|
return item !== undefined ? item.type['id'] : undefined;
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createDataset(fetchedData: Object) {
|
|
|
|
return [
|
|
|
|
{
|
2019-09-16 18:54:50 +02:00
|
|
|
title: '',
|
2019-11-15 19:29:25 +01:00
|
|
|
data: this.generateData(fetchedData),
|
2020-03-05 19:54:56 +01:00
|
|
|
extraData: this.state,
|
2019-07-31 14:22:36 +02:00
|
|
|
keyExtractor: this.getKeyExtractor
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2019-06-27 10:17:51 +02:00
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
2019-07-26 14:14:01 +02:00
|
|
|
* Generate the data using types and FetchedData.
|
2019-06-29 15:43:57 +02:00
|
|
|
* This will group items under the same type.
|
|
|
|
*
|
2019-07-26 14:14:01 +02:00
|
|
|
* @param fetchedData The array of articles represented by objects
|
2019-06-29 15:43:57 +02:00
|
|
|
* @returns {Array} The formatted dataset
|
|
|
|
*/
|
2019-11-15 19:29:25 +01:00
|
|
|
generateData(fetchedData: Object) {
|
2019-06-27 10:17:51 +02:00
|
|
|
let finalData = [];
|
2020-03-05 19:54:56 +01:00
|
|
|
this.articles = undefined;
|
2019-07-26 14:14:01 +02:00
|
|
|
if (fetchedData.types !== undefined && fetchedData.articles !== undefined) {
|
|
|
|
let types = fetchedData.types;
|
2020-03-05 19:54:56 +01:00
|
|
|
this.articles = fetchedData.articles;
|
2019-11-14 16:20:06 +01:00
|
|
|
finalData.push({
|
|
|
|
type: {
|
2020-02-23 17:21:34 +01:00
|
|
|
id: -1,
|
2019-11-14 16:20:06 +01:00
|
|
|
name: i18n.t('proximoScreen.all'),
|
|
|
|
icon: 'star'
|
|
|
|
},
|
2020-03-05 19:54:56 +01:00
|
|
|
data: this.getAvailableArticles(this.articles, undefined)
|
2019-11-14 16:20:06 +01:00
|
|
|
});
|
2019-07-26 14:14:01 +02:00
|
|
|
for (let i = 0; i < types.length; i++) {
|
|
|
|
finalData.push({
|
|
|
|
type: types[i],
|
2020-03-05 19:54:56 +01:00
|
|
|
data: this.getAvailableArticles(this.articles, types[i])
|
2019-07-26 14:14:01 +02:00
|
|
|
});
|
2019-11-14 16:20:06 +01:00
|
|
|
|
2019-06-27 10:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-17 11:07:58 +02:00
|
|
|
finalData.sort(ProximoMainScreen.sortFinalData);
|
2019-06-27 10:17:51 +02:00
|
|
|
return finalData;
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:20:06 +01:00
|
|
|
/**
|
|
|
|
* Get an array of available articles (in stock) of the given type
|
|
|
|
*
|
|
|
|
* @param articles The list of all articles
|
|
|
|
* @param type The type of articles to find (undefined for any type)
|
|
|
|
* @return {Array} The array of available articles
|
|
|
|
*/
|
2019-11-15 19:29:25 +01:00
|
|
|
getAvailableArticles(articles: Array<Object>, type: ?Object) {
|
2019-11-14 16:20:06 +01:00
|
|
|
let availableArticles = [];
|
|
|
|
for (let k = 0; k < articles.length; k++) {
|
|
|
|
if ((type !== undefined && type !== null && articles[k]['type'].includes(type['id'])
|
|
|
|
|| type === undefined)
|
|
|
|
&& parseInt(articles[k]['quantity']) > 0) {
|
|
|
|
availableArticles.push(articles[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return availableArticles;
|
|
|
|
}
|
|
|
|
|
2020-02-11 01:05:24 +01:00
|
|
|
onPressSearchBtn() {
|
2019-11-15 18:41:25 +01:00
|
|
|
let searchScreenData = {
|
|
|
|
shouldFocusSearchBar: true,
|
|
|
|
data: {
|
|
|
|
type: {
|
|
|
|
id: "0",
|
|
|
|
name: i18n.t('proximoScreen.all'),
|
|
|
|
icon: 'star'
|
|
|
|
},
|
2020-03-05 19:54:56 +01:00
|
|
|
data: this.articles !== undefined ?
|
|
|
|
this.getAvailableArticles(this.articles, undefined) : []
|
2019-11-15 18:41:25 +01:00
|
|
|
},
|
|
|
|
};
|
2020-02-11 01:05:24 +01:00
|
|
|
this.props.navigation.navigate('ProximoListScreen', searchScreenData);
|
|
|
|
}
|
2019-11-15 18:41:25 +01:00
|
|
|
|
2020-02-11 01:05:24 +01:00
|
|
|
onPressAboutBtn() {
|
|
|
|
this.props.navigation.navigate('ProximoAboutScreen');
|
|
|
|
}
|
2019-11-15 18:41:25 +01:00
|
|
|
|
2020-02-11 01:05:24 +01:00
|
|
|
getRightButton() {
|
2019-08-08 18:25:13 +02:00
|
|
|
return (
|
2019-11-15 18:41:25 +01:00
|
|
|
<View
|
|
|
|
style={{
|
2020-03-05 23:40:50 +01:00
|
|
|
flexDirection: 'row',
|
2019-11-15 18:41:25 +01:00
|
|
|
}}>
|
2020-03-07 11:49:32 +01:00
|
|
|
<HeaderButton icon={'magnify'} onPress={this.onPressSearchBtn}/>
|
|
|
|
<HeaderButton icon={'information'} onPress={this.onPressAboutBtn}/>
|
2019-11-15 18:41:25 +01:00
|
|
|
</View>
|
2019-08-08 18:25:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-06 09:12:56 +01:00
|
|
|
|
2020-03-05 19:54:56 +01:00
|
|
|
getRenderItem({item}: Object) {
|
2019-11-15 18:41:25 +01:00
|
|
|
let dataToSend = {
|
|
|
|
shouldFocusSearchBar: false,
|
|
|
|
data: item,
|
|
|
|
};
|
2020-03-06 23:15:01 +01:00
|
|
|
const subtitle = item.data.length + " " + (item.data.length > 1 ? i18n.t('proximoScreen.articles') : i18n.t('proximoScreen.article'));
|
2020-02-23 21:47:36 +01:00
|
|
|
const onPress = this.props.navigation.navigate.bind(this, 'ProximoListScreen', dataToSend);
|
2019-08-01 11:04:27 +02:00
|
|
|
if (item.data.length > 0) {
|
|
|
|
return (
|
2020-03-06 23:15:01 +01:00
|
|
|
<List.Item
|
|
|
|
title={item.type.name}
|
|
|
|
description={subtitle}
|
2020-02-23 21:47:36 +01:00
|
|
|
onPress={onPress}
|
2020-03-06 23:15:01 +01:00
|
|
|
left={props => <List.Icon
|
|
|
|
{...props}
|
|
|
|
icon={item.type.icon}
|
2020-03-09 23:15:13 +01:00
|
|
|
color={this.colors.primary}/>}
|
2020-03-06 23:15:01 +01:00
|
|
|
right={props => <List.Icon {...props} icon={'chevron-right'}/>}
|
|
|
|
/>
|
2019-08-01 11:04:27 +02:00
|
|
|
);
|
2020-03-05 19:54:56 +01:00
|
|
|
} else
|
2019-08-01 11:04:27 +02:00
|
|
|
return <View/>;
|
2020-03-05 19:54:56 +01:00
|
|
|
}
|
2019-08-01 11:04:27 +02:00
|
|
|
|
2020-03-05 19:54:56 +01:00
|
|
|
render() {
|
|
|
|
const nav = this.props.navigation;
|
|
|
|
return (
|
2020-03-06 09:12:56 +01:00
|
|
|
<WebSectionList
|
|
|
|
createDataset={this.createDataset}
|
|
|
|
navigation={nav}
|
2020-03-08 15:50:34 +01:00
|
|
|
autoRefreshTime={0}
|
|
|
|
refreshOnFocus={false}
|
2020-03-06 09:12:56 +01:00
|
|
|
fetchUrl={DATA_URL}
|
2020-03-07 09:15:25 +01:00
|
|
|
renderItem={this.getRenderItem}/>
|
2020-03-05 19:54:56 +01:00
|
|
|
);
|
2019-06-27 10:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
export default withTheme(ProximoMainScreen);
|