application-amicale/screens/Proximo/ProximoMainScreen.js

248 lines
7.2 KiB
JavaScript
Raw Normal View History

// @flow
import * as React from 'react';
import {View} from 'react-native'
2019-06-27 10:17:51 +02:00
import i18n from "i18n-js";
import WebSectionList from "../../components/WebSectionList";
import {List, withTheme} from 'react-native-paper';
import HeaderButton from "../../components/HeaderButton";
2019-06-27 10:17:51 +02:00
const DATA_URL = "https://etud.insa-toulouse.fr/~proximo/data/stock-v2.json";
2019-06-27 10:17:51 +02:00
type Props = {
navigation: Object,
}
type State = {
fetchedData: Object,
}
2019-06-29 15:43:57 +02:00
/**
2020-03-29 15:59:25 +02:00
* Class defining the main proximo screen.
* This screen shows the different categories of articles offered by proximo.
2019-06-29 15:43:57 +02:00
*/
class ProximoMainScreen extends React.Component<Props, State> {
articles: Object;
2020-02-23 18:15:30 +01:00
onPressSearchBtn: Function;
onPressAboutBtn: Function;
getRenderItem: Function;
createDataset: Function;
2020-02-23 18:15:30 +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);
this.getRenderItem = this.getRenderItem.bind(this);
this.createDataset = this.createDataset.bind(this);
this.colors = props.theme.colors;
}
2020-03-29 15:59:25 +02:00
/**
* Function used to sort items in the list.
* Makes the All category stick to the top and sorts the others by name ascending
*
* @param a
* @param b
* @return {number}
*/
2020-01-31 16:51:43 +01:00
static sortFinalData(a: Object, b: Object) {
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-29 15:59:25 +02:00
/**
* Creates header button
*/
2020-03-06 09:12:56 +01:00
componentDidMount() {
2020-03-29 15:59:25 +02:00
const rightButton = this.getHeaderButtons.bind(this);
2020-03-06 09:12:56 +01:00
this.props.navigation.setOptions({
headerRight: rightButton,
});
}
2020-03-29 15:59:25 +02:00
/**
* Callback used when the search button is pressed.
* This will open a new ProximoListScreen with all items displayed
*/
onPressSearchBtn() {
let searchScreenData = {
shouldFocusSearchBar: true,
data: {
type: {
id: "0",
name: i18n.t('proximoScreen.all'),
icon: 'star'
},
data: this.articles !== undefined ?
this.getAvailableArticles(this.articles, undefined) : []
},
};
this.props.navigation.navigate('ProximoListScreen', searchScreenData);
}
/**
* Callback used when the about button is pressed.
* This will open the ProximoAboutScreen
*/
onPressAboutBtn() {
this.props.navigation.navigate('ProximoAboutScreen');
}
/**
* Gets the header buttons
* @return {*}
*/
getHeaderButtons() {
return (
<View
style={{
flexDirection: 'row',
}}>
<HeaderButton icon={'magnify'} onPress={this.onPressSearchBtn}/>
<HeaderButton icon={'information'} onPress={this.onPressAboutBtn}/>
</View>
);
}
/**
* Extracts a key for the given category
*
* @param item The category to extract the key from
* @return {*} The extracted key
*/
getKeyExtractor(item: Object) {
2019-10-06 12:30:29 +02:00
return item !== undefined ? item.type['id'] : undefined;
}
2020-03-29 15:59:25 +02:00
/**
* Creates the dataset to be used in the FlatList
*
* @param fetchedData
* @return {*}
* */
createDataset(fetchedData: Object) {
return [
{
2019-09-16 18:54:50 +02:00
title: '',
data: this.generateData(fetchedData),
extraData: this.state,
keyExtractor: this.getKeyExtractor
}
];
}
2019-06-27 10:17:51 +02:00
2019-06-29 15:43:57 +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.
*
* @param fetchedData The array of articles represented by objects
2019-06-29 15:43:57 +02:00
* @returns {Array} The formatted dataset
*/
generateData(fetchedData: Object) {
2019-06-27 10:17:51 +02:00
let finalData = [];
this.articles = undefined;
if (fetchedData.types !== undefined && fetchedData.articles !== undefined) {
let types = fetchedData.types;
this.articles = fetchedData.articles;
finalData.push({
type: {
id: -1,
name: i18n.t('proximoScreen.all'),
icon: 'star'
},
data: this.getAvailableArticles(this.articles, undefined)
});
for (let i = 0; i < types.length; i++) {
finalData.push({
type: types[i],
data: this.getAvailableArticles(this.articles, types[i])
});
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;
}
/**
* 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
*/
getAvailableArticles(articles: Array<Object>, type: ?Object) {
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-03-29 15:59:25 +02:00
/**
* Gets the given category render item
*
* @param item The category to render
* @return {*}
*/
getRenderItem({item}: Object) {
let dataToSend = {
shouldFocusSearchBar: false,
data: item,
};
const subtitle = item.data.length + " " + (item.data.length > 1 ? i18n.t('proximoScreen.articles') : i18n.t('proximoScreen.article'));
const onPress = this.props.navigation.navigate.bind(this, 'ProximoListScreen', dataToSend);
if (item.data.length > 0) {
return (
<List.Item
title={item.type.name}
description={subtitle}
onPress={onPress}
left={props => <List.Icon
{...props}
icon={item.type.icon}
color={this.colors.primary}/>}
right={props => <List.Icon {...props} icon={'chevron-right'}/>}
/>
);
} else
return <View/>;
}
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}/>
);
2019-06-27 10:17:51 +02:00
}
}
export default withTheme(ProximoMainScreen);