2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2019-07-26 14:14:01 +02:00
|
|
|
import {Badge, Body, H2, Left, ListItem, Right, Text} from 'native-base';
|
2019-06-27 10:17:51 +02:00
|
|
|
import i18n from "i18n-js";
|
2019-06-28 11:35:15 +02:00
|
|
|
import CustomMaterialIcon from "../../components/CustomMaterialIcon";
|
2019-07-26 14:14:01 +02:00
|
|
|
import FetchedDataSectionList from "../../components/FetchedDataSectionList";
|
2019-06-27 10:17:51 +02:00
|
|
|
|
|
|
|
const DATA_URL = "https://etud.insa-toulouse.fr/~vergnet/appli-amicale/dataProximo.json";
|
|
|
|
|
|
|
|
const typesIcons = {
|
|
|
|
Nouveau: "alert-decagram",
|
|
|
|
Alimentaire: "food",
|
|
|
|
Boissons: "bottle-wine",
|
|
|
|
Utilitaires: "notebook",
|
|
|
|
Default: "information-outline",
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-07-26 14:14:01 +02:00
|
|
|
export default class ProximoMainScreen extends FetchedDataSectionList {
|
|
|
|
|
2019-07-30 20:40:17 +02:00
|
|
|
constructor() {
|
2019-07-31 19:11:30 +02:00
|
|
|
super(DATA_URL, 0);
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getHeaderTranslation() {
|
|
|
|
return i18n.t("screens.proximo");
|
|
|
|
}
|
|
|
|
|
|
|
|
getUpdateToastTranslations() {
|
|
|
|
return [i18n.t("proximoScreen.listUpdated"), i18n.t("proximoScreen.listUpdateFail")];
|
|
|
|
}
|
2019-06-29 13:37:21 +02:00
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
getKeyExtractor(item: Object) {
|
2019-07-28 12:40:35 +02:00
|
|
|
return item !== undefined ? item.type : undefined;
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createDataset(fetchedData: Object) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
title: i18n.t('proximoScreen.listTitle'),
|
|
|
|
data: ProximoMainScreen.generateData(fetchedData),
|
|
|
|
extraData: super.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-07-26 14:14:01 +02:00
|
|
|
static generateData(fetchedData: Object) {
|
2019-06-27 10:17:51 +02:00
|
|
|
let finalData = [];
|
2019-07-26 14:14:01 +02:00
|
|
|
if (fetchedData.types !== undefined && fetchedData.articles !== undefined) {
|
|
|
|
let types = fetchedData.types;
|
|
|
|
let articles = fetchedData.articles;
|
|
|
|
for (let i = 0; i < types.length; i++) {
|
|
|
|
finalData.push({
|
|
|
|
type: types[i],
|
|
|
|
data: []
|
|
|
|
});
|
|
|
|
for (let k = 0; k < articles.length; k++) {
|
|
|
|
if (articles[k]['type'].includes(types[i])) {
|
|
|
|
finalData[i].data.push(articles[k]);
|
|
|
|
}
|
2019-06-27 10:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return finalData;
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
getRenderItem(item: Object, section : Object, data : Object) {
|
2019-06-27 10:17:51 +02:00
|
|
|
return (
|
2019-07-26 14:14:01 +02:00
|
|
|
<ListItem
|
|
|
|
button
|
|
|
|
thumbnail
|
|
|
|
onPress={() => {
|
|
|
|
this.props.navigation.navigate('ProximoListScreen', item);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Left>
|
|
|
|
<CustomMaterialIcon
|
|
|
|
icon={typesIcons[item.type] ? typesIcons[item.type] : typesIcons.Default}
|
|
|
|
fontSize={30}
|
|
|
|
/>
|
|
|
|
</Left>
|
|
|
|
<Body>
|
|
|
|
<Text>
|
|
|
|
{item.type}
|
|
|
|
</Text>
|
|
|
|
<Badge><Text>
|
|
|
|
{item.data.length} {item.data.length > 1 ? i18n.t('proximoScreen.articles') : i18n.t('proximoScreen.article')}
|
|
|
|
</Text></Badge>
|
|
|
|
</Body>
|
|
|
|
<Right>
|
|
|
|
<CustomMaterialIcon icon="chevron-right"/>
|
|
|
|
</Right>
|
|
|
|
</ListItem>
|
2019-06-27 10:17:51 +02:00
|
|
|
);
|
|
|
|
}
|
2019-07-26 14:14:01 +02:00
|
|
|
|
|
|
|
getRenderSectionHeader(title: String) {
|
|
|
|
return <H2 style={{textAlign: 'center', paddingVertical: 10}}>{title}</H2>;
|
|
|
|
}
|
2019-06-27 10:17:51 +02:00
|
|
|
}
|
|
|
|
|