application-amicale/screens/SelfMenuScreen.js

179 lines
5.3 KiB
JavaScript
Raw Normal View History

// @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";
import {Card, Text, withTheme} from 'react-native-paper';
2020-03-30 15:28:08 +02:00
import AprilFoolsManager from "../managers/AprilFoolsManager";
const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/menu/menu_data.json";
type Props = {
navigation: Object,
}
/**
2019-09-19 22:19:47 +02:00
* Class defining the app's menu screen.
*/
class SelfMenuScreen extends React.Component<Props> {
getRenderItem: Function;
getRenderSectionHeader: Function;
createDataset: Function;
colors: Object;
constructor(props) {
super(props);
2019-09-19 22:19:47 +02:00
this.getRenderItem = this.getRenderItem.bind(this);
this.getRenderSectionHeader = this.getRenderSectionHeader.bind(this);
this.createDataset = this.createDataset.bind(this);
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) {
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: '',
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(
{
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
}
2020-03-29 15:59:25 +02:00
/**
* Gets the render section header
*
* @param section The section to render the header from
* @return {*}
*/
getRenderSectionHeader({section}: Object) {
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>
);
}
2020-03-29 15:59:25 +02:00
/**
* Gets a FlatList render item
*
* @param item The item to render
* @return {*}
*/
getRenderItem({item}: Object) {
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,
borderBottomColor: this.colors.primary,
2020-03-08 14:36:20 +01:00
marginTop: 5,
2020-03-08 12:50:18 +01:00
marginBottom: 5,
}}/>
<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,
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>)}
</Card.Content>
2019-09-19 22:19:47 +02:00
</Card>
);
}
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();
}
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}
renderItem={this.getRenderItem}
renderSectionHeader={this.getRenderSectionHeader}
stickyHeader={true}/>
);
}
}
export default withTheme(SelfMenuScreen);