2019-08-08 13:57:03 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2019-09-19 22:19:47 +02:00
|
|
|
import {View} from 'react-native';
|
2020-03-30 15:28:08 +02:00
|
|
|
import DateManager from "../managers/DateManager";
|
2020-04-02 10:07:20 +02:00
|
|
|
import WebSectionList from "../components/Lists/WebSectionList";
|
2020-03-09 23:15:13 +01:00
|
|
|
import {Card, Text, withTheme} from 'react-native-paper';
|
2020-03-30 15:28:08 +02:00
|
|
|
import AprilFoolsManager from "../managers/AprilFoolsManager";
|
2019-08-08 13:57:03 +02:00
|
|
|
|
2020-02-23 17:21:34 +01:00
|
|
|
const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/menu/menu_data.json";
|
2019-08-08 13:57:03 +02:00
|
|
|
|
2020-03-05 19:54:56 +01:00
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:57:03 +02:00
|
|
|
/**
|
2019-09-19 22:19:47 +02:00
|
|
|
* Class defining the app's menu screen.
|
2019-08-08 13:57:03 +02:00
|
|
|
*/
|
2020-03-09 23:15:13 +01:00
|
|
|
class SelfMenuScreen extends React.Component<Props> {
|
2019-08-08 13:57:03 +02:00
|
|
|
|
2020-03-05 19:54:56 +01:00
|
|
|
getRenderItem: Function;
|
|
|
|
getRenderSectionHeader: Function;
|
|
|
|
createDataset: Function;
|
2020-03-09 23:15:13 +01:00
|
|
|
colors: Object;
|
2020-03-05 19:54:56 +01:00
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-09-19 22:19:47 +02:00
|
|
|
|
2020-03-05 19:54:56 +01:00
|
|
|
this.getRenderItem = this.getRenderItem.bind(this);
|
|
|
|
this.getRenderSectionHeader = this.getRenderSectionHeader.bind(this);
|
|
|
|
this.createDataset = this.createDataset.bind(this);
|
2020-03-09 23:15:13 +01:00
|
|
|
this.colors = props.theme.colors;
|
2019-09-19 22:19:47 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Extract a key for the given item
|
|
|
|
*
|
|
|
|
* @param item The item to extract the key from
|
|
|
|
* @return {*} The extracted key
|
|
|
|
*/
|
2019-09-19 22:19:47 +02:00
|
|
|
getKeyExtractor(item: Object) {
|
2019-10-06 12:44:40 +02:00
|
|
|
return item !== undefined ? item['name'] : undefined;
|
2019-09-19 22:19:47 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Creates the dataset to be used in the FlatList
|
|
|
|
*
|
|
|
|
* @param fetchedData
|
|
|
|
* @return {[]}
|
|
|
|
*/
|
2019-09-19 22:19:47 +02:00
|
|
|
createDataset(fetchedData: Object) {
|
|
|
|
let result = [];
|
|
|
|
// Prevent crash by giving a default value when fetchedData is empty (not yet available)
|
|
|
|
if (Object.keys(fetchedData).length === 0) {
|
|
|
|
result = [
|
|
|
|
{
|
|
|
|
title: '',
|
2019-10-06 12:44:40 +02:00
|
|
|
data: [],
|
2019-09-19 22:19:47 +02:00
|
|
|
keyExtractor: this.getKeyExtractor
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2020-03-08 12:50:18 +01:00
|
|
|
if (AprilFoolsManager.getInstance().isAprilFoolsEnabled() && fetchedData.length > 0)
|
|
|
|
fetchedData[0].meal[0].foodcategory = AprilFoolsManager.getFakeMenuItem(fetchedData[0].meal[0].foodcategory);
|
2019-09-19 22:19:47 +02:00
|
|
|
// fetched data is an array here
|
|
|
|
for (let i = 0; i < fetchedData.length; i++) {
|
|
|
|
result.push(
|
|
|
|
{
|
2020-03-21 18:46:12 +01:00
|
|
|
title: DateManager.getInstance().getTranslatedDate(fetchedData[i].date),
|
2019-09-19 22:19:47 +02:00
|
|
|
data: fetchedData[i].meal[0].foodcategory,
|
2019-10-06 12:30:29 +02:00
|
|
|
keyExtractor: this.getKeyExtractor,
|
2019-09-19 22:19:47 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return result
|
2019-08-08 22:07:49 +02:00
|
|
|
}
|
2019-08-08 13:57:03 +02:00
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Gets the render section header
|
|
|
|
*
|
|
|
|
* @param section The section to render the header from
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2020-03-05 19:54:56 +01:00
|
|
|
getRenderSectionHeader({section}: Object) {
|
2019-08-08 13:57:03 +02:00
|
|
|
return (
|
2020-03-08 00:22:46 +01:00
|
|
|
<Card style={{
|
2020-03-08 14:36:20 +01:00
|
|
|
width: '95%',
|
2020-03-08 00:22:46 +01:00
|
|
|
marginLeft: 'auto',
|
|
|
|
marginRight: 'auto',
|
2020-03-08 14:36:20 +01:00
|
|
|
marginTop: 5,
|
|
|
|
marginBottom: 5,
|
2020-03-08 00:22:46 +01:00
|
|
|
elevation: 4,
|
|
|
|
}}>
|
|
|
|
<Card.Title
|
|
|
|
title={section.title}
|
|
|
|
titleStyle={{
|
|
|
|
textAlign: 'center'
|
|
|
|
}}
|
|
|
|
subtitleStyle={{
|
|
|
|
textAlign: 'center'
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
paddingLeft: 0,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Card>
|
2019-08-08 13:57:03 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Gets a FlatList render item
|
|
|
|
*
|
|
|
|
* @param item The item to render
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2020-03-05 19:54:56 +01:00
|
|
|
getRenderItem({item}: Object) {
|
2019-08-08 13:57:03 +02:00
|
|
|
return (
|
2019-09-19 22:19:47 +02:00
|
|
|
<Card style={{
|
|
|
|
flex: 0,
|
2020-03-08 14:36:20 +01:00
|
|
|
marginHorizontal: 10,
|
|
|
|
marginVertical: 5,
|
2019-09-19 22:19:47 +02:00
|
|
|
}}>
|
2020-03-08 12:50:18 +01:00
|
|
|
<Card.Title
|
2020-03-08 14:36:20 +01:00
|
|
|
style={{marginTop: 5}}
|
2020-03-08 12:50:18 +01:00
|
|
|
title={item.name}
|
|
|
|
/>
|
|
|
|
<View style={{
|
|
|
|
width: '80%',
|
|
|
|
marginLeft: 'auto',
|
|
|
|
marginRight: 'auto',
|
|
|
|
borderBottomWidth: 1,
|
2020-03-09 23:15:13 +01:00
|
|
|
borderBottomColor: this.colors.primary,
|
2020-03-08 14:36:20 +01:00
|
|
|
marginTop: 5,
|
2020-03-08 12:50:18 +01:00
|
|
|
marginBottom: 5,
|
|
|
|
}}/>
|
2020-03-06 23:15:01 +01:00
|
|
|
<Card.Content>
|
2020-01-31 16:51:43 +01:00
|
|
|
{item.dishes.map((object) =>
|
2019-09-19 22:19:47 +02:00
|
|
|
<View>
|
|
|
|
{object.name !== "" ?
|
|
|
|
<Text style={{
|
|
|
|
marginTop: 5,
|
2020-03-06 23:15:01 +01:00
|
|
|
marginBottom: 5,
|
|
|
|
textAlign: 'center'
|
2019-11-08 17:07:45 +01:00
|
|
|
}}>{this.formatName(object.name)}</Text>
|
2019-09-19 22:19:47 +02:00
|
|
|
: <View/>}
|
|
|
|
</View>)}
|
2020-03-06 23:15:01 +01:00
|
|
|
</Card.Content>
|
2019-09-19 22:19:47 +02:00
|
|
|
</Card>
|
2019-08-08 13:57:03 +02:00
|
|
|
);
|
|
|
|
}
|
2019-09-19 22:19:47 +02:00
|
|
|
|
2020-03-29 15:59:25 +02:00
|
|
|
/**
|
|
|
|
* Formats the given string to make sure it starts with a capital letter
|
|
|
|
*
|
|
|
|
* @param name The string to format
|
|
|
|
* @return {string} The formatted string
|
|
|
|
*/
|
2019-11-08 17:07:45 +01:00
|
|
|
formatName(name: String) {
|
|
|
|
return name.charAt(0) + name.substr(1).toLowerCase();
|
|
|
|
}
|
|
|
|
|
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}
|
2020-03-05 19:54:56 +01:00
|
|
|
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}
|
|
|
|
renderItem={this.getRenderItem}
|
|
|
|
renderSectionHeader={this.getRenderSectionHeader}
|
|
|
|
stickyHeader={true}/>
|
2020-03-05 19:54:56 +01:00
|
|
|
);
|
|
|
|
}
|
2019-08-08 13:57:03 +02:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:15:13 +01:00
|
|
|
export default withTheme(SelfMenuScreen);
|