application-amicale/screens/SelfMenuScreen.js

181 lines
6.4 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';
import ThemeManager from "../utils/ThemeManager";
import i18n from "i18n-js";
import WebSectionList from "../components/WebSectionList";
2020-03-08 12:50:18 +01:00
import {Card, Text} from 'react-native-paper';
import AprilFoolsManager from "../utils/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.
* This screen fetches data from etud to render the RU menu
*/
export default class SelfMenuScreen extends React.Component<Props> {
2019-09-19 22:19:47 +02:00
// Hard code strings as toLocaleDateString does not work on current android JS engine
daysOfWeek = [];
monthsOfYear = [];
2019-08-08 22:07:49 +02:00
getRenderItem: Function;
getRenderSectionHeader: Function;
createDataset: Function;
2019-08-08 22:07:49 +02:00
constructor() {
super();
2019-09-19 22:19:47 +02:00
this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.january"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.february"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.march"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.april"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.may"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.june"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.july"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.august"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.september"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.october"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.november"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.december"));
this.getRenderItem = this.getRenderItem.bind(this);
this.getRenderSectionHeader = this.getRenderSectionHeader.bind(this);
this.createDataset = this.createDataset.bind(this);
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
}
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
extraData: super.state,
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: this.getFormattedDate(fetchedData[i].date),
data: fetchedData[i].meal[0].foodcategory,
extraData: super.state,
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-09-19 22:19:47 +02:00
getFormattedDate(dateString: string) {
let dateArray = dateString.split('-');
let date = new Date();
date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
return this.daysOfWeek[date.getDay() - 1] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
}
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>
);
}
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: ThemeManager.getCurrentThemeVariables().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
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}/>
);
}
}