Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sidebar.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // @flow
  2. import * as React from 'react';
  3. import {Dimensions, FlatList, Image, Platform, StyleSheet, View,} from 'react-native';
  4. import i18n from "i18n-js";
  5. import {openBrowser} from "../../utils/WebBrowser";
  6. import {Drawer, TouchableRipple, withTheme} from "react-native-paper";
  7. import ConnectionManager from "../../managers/ConnectionManager";
  8. import LogoutDialog from "../Amicale/LogoutDialog";
  9. const deviceWidth = Dimensions.get("window").width;
  10. const LIST_ITEM_HEIGHT = 48;
  11. type Props = {
  12. navigation: Object,
  13. state: Object,
  14. theme: Object,
  15. };
  16. type State = {
  17. isLoggedIn: boolean,
  18. dialogVisible: boolean,
  19. activeRoute: string;
  20. };
  21. /**
  22. * Component used to render the drawer menu content
  23. */
  24. class SideBar extends React.Component<Props, State> {
  25. dataSet: Array<Object>;
  26. colors: Object;
  27. /**
  28. * Generate the dataset
  29. *
  30. * @param props
  31. */
  32. constructor(props: Props) {
  33. super(props);
  34. // Dataset used to render the drawer
  35. this.dataSet = [
  36. {
  37. name: i18n.t('screens.home'),
  38. route: "Main",
  39. icon: "home",
  40. },
  41. {
  42. name: i18n.t('sidenav.divider4'),
  43. route: "Divider4"
  44. },
  45. {
  46. name: i18n.t('screens.login'),
  47. route: "LoginScreen",
  48. icon: "login",
  49. onlyWhenLoggedOut: true,
  50. shouldEmphasis: true,
  51. },
  52. {
  53. name: i18n.t('screens.profile'),
  54. route: "ProfileScreen",
  55. icon: "account",
  56. onlyWhenLoggedIn: true,
  57. },
  58. {
  59. name: i18n.t('clubs.clubList'),
  60. route: "ClubListScreen",
  61. icon: "account-group",
  62. onlyWhenLoggedIn: true,
  63. },
  64. {
  65. name: i18n.t('screens.logout'),
  66. route: 'disconnect',
  67. action: this.showDisconnectDialog,
  68. icon: "logout",
  69. onlyWhenLoggedIn: true,
  70. },
  71. {
  72. name: i18n.t('sidenav.divider2'),
  73. route: "Divider2"
  74. },
  75. {
  76. name: i18n.t('screens.menuSelf'),
  77. route: "SelfMenuScreen",
  78. icon: "silverware-fork-knife",
  79. },
  80. {
  81. name: i18n.t('screens.availableRooms'),
  82. route: "AvailableRoomScreen",
  83. icon: "calendar-check",
  84. },
  85. {
  86. name: i18n.t('screens.bib'),
  87. route: "BibScreen",
  88. icon: "book",
  89. },
  90. {
  91. name: i18n.t('screens.bluemind'),
  92. route: "BlueMindScreen",
  93. link: "https://etud-mel.insa-toulouse.fr/webmail/",
  94. icon: "email",
  95. },
  96. {
  97. name: i18n.t('screens.ent'),
  98. route: "EntScreen",
  99. link: "https://ent.insa-toulouse.fr/",
  100. icon: "notebook",
  101. },
  102. {
  103. name: i18n.t('sidenav.divider1'),
  104. route: "Divider1"
  105. },
  106. {
  107. name: "Amicale",
  108. route: "AmicaleScreen",
  109. link: "https://amicale-insat.fr/",
  110. icon: "alpha-a-box",
  111. },
  112. {
  113. name: "Élus Étudiants",
  114. route: "ElusEtudScreen",
  115. link: "https://etud.insa-toulouse.fr/~eeinsat/",
  116. icon: "alpha-e-box",
  117. },
  118. {
  119. name: "Wiketud",
  120. route: "WiketudScreen",
  121. link: "https://wiki.etud.insa-toulouse.fr",
  122. icon: "wikipedia",
  123. },
  124. {
  125. name: "Tutor'INSA",
  126. route: "TutorInsaScreen",
  127. link: "https://www.etud.insa-toulouse.fr/~tutorinsa/",
  128. icon: "school",
  129. },
  130. {
  131. name: i18n.t('sidenav.divider3'),
  132. route: "Divider3"
  133. },
  134. {
  135. name: i18n.t('screens.settings'),
  136. route: "SettingsScreen",
  137. icon: "settings",
  138. },
  139. {
  140. name: i18n.t('screens.about'),
  141. route: "AboutScreen",
  142. icon: "information",
  143. },
  144. ];
  145. this.colors = props.theme.colors;
  146. ConnectionManager.getInstance().addLoginStateListener(this.onLoginStateChange);
  147. this.props.navigation.addListener('state', this.onRouteChange);
  148. this.state = {
  149. isLoggedIn: ConnectionManager.getInstance().isLoggedIn(),
  150. dialogVisible: false,
  151. activeRoute: 'Main',
  152. };
  153. }
  154. onRouteChange = (event) => {
  155. if (event.data.state.routes !== undefined) {
  156. const route = event.data.state.routes[0]; // get the current route (ROOT)
  157. if (route.state !== undefined) {
  158. const state = route.state; // Get the Drawer's state if it exists
  159. // Get the current route name. This will only show Drawer routes.
  160. // Tab routes will be shown as 'Main'
  161. const routeName = state.routeNames[state.index];
  162. if (this.state.activeRoute !== routeName)
  163. this.setState({activeRoute: routeName});
  164. }
  165. }
  166. };
  167. showDisconnectDialog = () => this.setState({dialogVisible: true});
  168. hideDisconnectDialog = () => this.setState({dialogVisible: false});
  169. onLoginStateChange = (isLoggedIn: boolean) => this.setState({isLoggedIn: isLoggedIn});
  170. /**
  171. * Callback when a drawer item is pressed.
  172. * It will either navigate to the associated screen, or open the browser to the associated link
  173. *
  174. * @param item The item pressed
  175. */
  176. onListItemPress(item: Object) {
  177. if (item.link !== undefined)
  178. openBrowser(item.link, this.colors.primary);
  179. else if (item.action !== undefined)
  180. item.action();
  181. else
  182. this.props.navigation.navigate(item.route);
  183. }
  184. /**
  185. * Key extractor for list items
  186. *
  187. * @param item The item to extract the key from
  188. * @return {string} The extracted key
  189. */
  190. listKeyExtractor(item: Object): string {
  191. return item.route;
  192. }
  193. /**
  194. * Gets the render item for the given list item
  195. *
  196. * @param item The item to render
  197. * @return {*}
  198. */
  199. getRenderItem = ({item}: Object) => {
  200. const onListItemPress = this.onListItemPress.bind(this, item);
  201. const onlyWhenLoggedOut = item.onlyWhenLoggedOut !== undefined && item.onlyWhenLoggedOut === true;
  202. const onlyWhenLoggedIn = item.onlyWhenLoggedIn !== undefined && item.onlyWhenLoggedIn === true;
  203. const shouldEmphasis = item.shouldEmphasis !== undefined && item.shouldEmphasis === true;
  204. if (onlyWhenLoggedIn && !this.state.isLoggedIn || onlyWhenLoggedOut && this.state.isLoggedIn)
  205. return null;
  206. else if (item.icon !== undefined) {
  207. return (
  208. <Drawer.Item
  209. label={item.name}
  210. active={this.state.activeRoute === item.route}
  211. icon={item.icon}
  212. onPress={onListItemPress}
  213. style={{
  214. height: LIST_ITEM_HEIGHT,
  215. justifyContent: 'center',
  216. }}
  217. />
  218. );
  219. } else {
  220. return (
  221. <Drawer.Item
  222. label={item.name}
  223. style={{
  224. height: LIST_ITEM_HEIGHT,
  225. justifyContent: 'center',
  226. }}
  227. />
  228. );
  229. }
  230. };
  231. itemLayout = (data, index) => ({length: LIST_ITEM_HEIGHT, offset: LIST_ITEM_HEIGHT * index, index});
  232. render() {
  233. const onPress = this.onListItemPress.bind(this, {route: 'TetrisScreen'});
  234. return (
  235. <View style={{height: '100%'}}>
  236. <TouchableRipple
  237. onPress={onPress}
  238. >
  239. <Image
  240. source={require("../../../assets/drawer-cover.png")}
  241. style={styles.drawerCover}
  242. />
  243. </TouchableRipple>
  244. {/*$FlowFixMe*/}
  245. <FlatList
  246. data={this.dataSet}
  247. extraData={this.state.isLoggedIn.toString() + this.state.activeRoute}
  248. keyExtractor={this.listKeyExtractor}
  249. renderItem={this.getRenderItem}
  250. // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
  251. getItemLayout={this.itemLayout}
  252. />
  253. <LogoutDialog
  254. {...this.props}
  255. visible={this.state.dialogVisible}
  256. onDismiss={this.hideDisconnectDialog}
  257. />
  258. </View>
  259. );
  260. }
  261. }
  262. const styles = StyleSheet.create({
  263. drawerCover: {
  264. height: deviceWidth / 3,
  265. width: 2 * deviceWidth / 3,
  266. position: "relative",
  267. marginBottom: 10,
  268. marginTop: 20
  269. },
  270. text: {
  271. fontWeight: Platform.OS === "ios" ? "500" : "400",
  272. fontSize: 16,
  273. marginLeft: 20
  274. },
  275. badgeText: {
  276. fontSize: Platform.OS === "ios" ? 13 : 11,
  277. fontWeight: "400",
  278. textAlign: "center",
  279. marginTop: Platform.OS === "android" ? -3 : undefined
  280. }
  281. });
  282. export default withTheme(SideBar);