application-amicale/components/Sidebar.js

218 lines
6.1 KiB
JavaScript
Raw Normal View History

// @flow
import * as React from 'react';
import {Dimensions, FlatList, Image, Linking, Platform, StyleSheet} from 'react-native';
2020-01-31 16:51:43 +01:00
import {Badge, Container, Left, ListItem, Right, Text} from "native-base";
2019-06-25 22:20:24 +02:00
import i18n from "i18n-js";
2019-06-28 11:35:15 +02:00
import CustomMaterialIcon from '../components/CustomMaterialIcon';
import ThemeManager from "../utils/ThemeManager";
2019-06-25 22:20:24 +02:00
const deviceWidth = Dimensions.get("window").width;
2019-06-25 22:20:24 +02:00
const drawerCover = require("../assets/drawer-cover.png");
type Props = {
navigation: Object,
};
type State = {
active: string,
};
2019-06-29 15:43:57 +02:00
/**
* Class used to define a navigation drawer
*/
export default class SideBar extends React.Component<Props, State> {
dataSet: Array<Object>;
2019-06-25 22:20:24 +02:00
2019-06-29 15:43:57 +02:00
state = {
active: 'Home',
};
/**
* Generate the datasets
*
* @param props
*/
constructor(props: Props) {
2019-06-25 22:20:24 +02:00
super(props);
// Dataset used to render the drawer
// If the link field is defined, clicking on the item will open the link
2019-06-25 22:20:24 +02:00
this.dataSet = [
{
name: i18n.t('sidenav.divider1'),
route: "Divider1"
},
2019-07-31 14:43:19 +02:00
{
name: "Amicale",
route: "AmicaleScreen",
icon: "alpha-a-box",
},
2020-01-29 13:53:05 +01:00
{
name: "Élus Étudiants",
route: "ElusEtudScreen",
icon: "alpha-e-box",
},
2019-06-27 10:45:16 +02:00
{
name: "Wiketud",
route: "WiketudScreen",
icon: "wikipedia",
2019-06-27 10:45:16 +02:00
},
2019-09-19 22:33:28 +02:00
{
name: "Tutor'INSA",
route: "TutorInsaScreen",
2019-09-19 22:33:28 +02:00
icon: "school",
},
{
name: i18n.t('sidenav.divider2'),
route: "Divider2"
},
{
name: i18n.t('screens.bluemind'),
route: "BlueMindScreen",
icon: "email",
},
{
name: i18n.t('screens.ent'),
route: "EntScreen",
icon: "notebook",
},
{
2019-11-07 17:58:02 +01:00
name: i18n.t('screens.availableRooms'),
route: "AvailableRoomScreen",
icon: "calendar-check",
2019-09-19 22:33:28 +02:00
},
{
name: i18n.t('screens.menuSelf'),
route: "SelfMenuScreen",
icon: "silverware-fork-knife",
},
{
name: i18n.t('sidenav.divider3'),
route: "Divider3"
},
{
name: i18n.t('screens.settings'),
route: "SettingsScreen",
icon: "settings",
},
{
name: i18n.t('screens.about'),
route: "AboutScreen",
icon: "information",
},
2019-06-25 22:20:24 +02:00
];
2020-02-11 01:05:24 +01:00
this.getRenderItem = this.getRenderItem.bind(this);
2019-06-25 22:20:24 +02:00
}
2020-02-11 01:05:24 +01:00
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
return nextState.active !== this.state.active;
}
onListItemPress(item) {
if (item.link !== undefined)
Linking.openURL(item.link).catch((err) => console.error('Error opening link', err));
else
this.navigateToScreen(item.route);
}
listKeyExtractor(item) {
return item.route;
}
getRenderItem({item}: Object) {
if (item.icon !== undefined) {
return (
<ListItem
button
noBorder
selected={this.state.active === item.route}
2020-02-11 01:05:24 +01:00
onPress={this.onListItemPress.bind(this, item)}
>
<Left>
<CustomMaterialIcon
icon={item.icon}
active={this.state.active === item.route}
/>
<Text style={styles.text}>
{item.name}
</Text>
</Left>
{item.types &&
<Right style={{flex: 1}}>
<Badge
style={{
borderRadius: 3,
height: 25,
width: 72,
backgroundColor: item.bg
}}
>
<Text
style={styles.badgeText}
>{`${item.types} Types`}</Text>
</Badge>
</Right>}
</ListItem>
);
} else {
return (
<ListItem itemDivider>
<Text>{item.name}</Text>
</ListItem>
);
}
}
2019-06-29 15:43:57 +02:00
/**
* Navigate to the selected route
2019-06-29 15:43:57 +02:00
* @param route {string} The route name to navigate to
*/
navigateToScreen(route: string) {
2019-06-25 22:20:24 +02:00
this.props.navigation.navigate(route);
};
render() {
2020-02-11 01:05:24 +01:00
// console.log("rendering SideBar");
2019-06-25 22:20:24 +02:00
return (
<Container style={{
backgroundColor: ThemeManager.getCurrentThemeVariables().sideMenuBgColor,
}}>
<Image source={drawerCover} style={styles.drawerCover}/>
<FlatList
data={this.dataSet}
extraData={this.state}
2020-02-11 01:05:24 +01:00
keyExtractor={this.listKeyExtractor}
renderItem={this.getRenderItem}
/>
2019-06-25 22:20:24 +02:00
</Container>
);
}
}
const styles = StyleSheet.create({
drawerCover: {
height: deviceWidth / 3,
width: 2 * deviceWidth / 3,
2019-06-25 22:20:24 +02:00
position: "relative",
marginBottom: 10,
marginTop: 20
},
text: {
fontWeight: Platform.OS === "ios" ? "500" : "400",
fontSize: 16,
marginLeft: 20
},
badgeText: {
fontSize: Platform.OS === "ios" ? 13 : 11,
fontWeight: "400",
textAlign: "center",
marginTop: Platform.OS === "android" ? -3 : undefined
}
});