Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.

ClubDisplayScreen.tsx 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import * as React from 'react';
  20. import { Linking, StyleSheet, View } from 'react-native';
  21. import {
  22. Avatar,
  23. Button,
  24. Card,
  25. Chip,
  26. Paragraph,
  27. withTheme,
  28. } from 'react-native-paper';
  29. import i18n from 'i18n-js';
  30. import { StackNavigationProp } from '@react-navigation/stack';
  31. import CustomHTML from '../../../components/Overrides/CustomHTML';
  32. import { TAB_BAR_HEIGHT } from '../../../components/Tabbar/CustomTabBar';
  33. import type { ClubCategoryType, ClubType } from './ClubListScreen';
  34. import CollapsibleScrollView from '../../../components/Collapsible/CollapsibleScrollView';
  35. import ImageGalleryButton from '../../../components/Media/ImageGalleryButton';
  36. import RequestScreen from '../../../components/Screens/RequestScreen';
  37. import ConnectionManager from '../../../managers/ConnectionManager';
  38. type PropsType = {
  39. navigation: StackNavigationProp<any>;
  40. route: {
  41. params?: {
  42. data?: ClubType;
  43. categories?: Array<ClubCategoryType>;
  44. clubId?: number;
  45. };
  46. };
  47. theme: ReactNativePaper.Theme;
  48. };
  49. type ResponseType = ClubType;
  50. const AMICALE_MAIL = 'clubs@amicale-insat.fr';
  51. const styles = StyleSheet.create({
  52. category: {
  53. marginRight: 5,
  54. },
  55. categoryContainer: {
  56. flexDirection: 'row',
  57. marginTop: 5,
  58. },
  59. card: {
  60. marginTop: 10,
  61. },
  62. icon: {
  63. backgroundColor: 'transparent',
  64. },
  65. emailButton: {
  66. marginLeft: 'auto',
  67. },
  68. scroll: {
  69. paddingLeft: 5,
  70. paddingRight: 5,
  71. },
  72. imageButton: {
  73. width: 300,
  74. height: 300,
  75. marginLeft: 'auto',
  76. marginRight: 'auto',
  77. marginTop: 10,
  78. marginBottom: 10,
  79. },
  80. });
  81. /**
  82. * Class defining a club event information page.
  83. * If called with data and categories navigation parameters, will use those to display the data.
  84. * If called with clubId parameter, will fetch the information on the server
  85. */
  86. class ClubDisplayScreen extends React.Component<PropsType> {
  87. displayData: ClubType | undefined;
  88. categories: Array<ClubCategoryType> | null;
  89. clubId: number;
  90. shouldFetchData: boolean;
  91. constructor(props: PropsType) {
  92. super(props);
  93. this.displayData = undefined;
  94. this.categories = null;
  95. this.clubId = props.route.params?.clubId ? props.route.params.clubId : 0;
  96. this.shouldFetchData = true;
  97. if (
  98. props.route.params &&
  99. props.route.params.data &&
  100. props.route.params.categories
  101. ) {
  102. this.displayData = props.route.params.data;
  103. this.categories = props.route.params.categories;
  104. this.clubId = props.route.params.data.id;
  105. this.shouldFetchData = false;
  106. }
  107. }
  108. /**
  109. * Gets the name of the category with the given ID
  110. *
  111. * @param id The category's ID
  112. * @returns {string|*}
  113. */
  114. getCategoryName(id: number): string {
  115. let categoryName = '';
  116. if (this.categories !== null) {
  117. this.categories.forEach((item: ClubCategoryType) => {
  118. if (id === item.id) {
  119. categoryName = item.name;
  120. }
  121. });
  122. }
  123. return categoryName;
  124. }
  125. /**
  126. * Gets the view for rendering categories
  127. *
  128. * @param categories The categories to display (max 2)
  129. * @returns {null|*}
  130. */
  131. getCategoriesRender(categories: Array<number | null>) {
  132. if (this.categories == null) {
  133. return null;
  134. }
  135. const final: Array<React.ReactNode> = [];
  136. categories.forEach((cat: number | null) => {
  137. if (cat != null) {
  138. final.push(
  139. <Chip style={styles.category} key={cat}>
  140. {this.getCategoryName(cat)}
  141. </Chip>
  142. );
  143. }
  144. });
  145. return <View style={styles.categoryContainer}>{final}</View>;
  146. }
  147. /**
  148. * Gets the view for rendering club managers if any
  149. *
  150. * @param managers The list of manager names
  151. * @param email The club contact email
  152. * @returns {*}
  153. */
  154. getManagersRender(managers: Array<string>, email: string | null) {
  155. const { props } = this;
  156. const managersListView: Array<React.ReactNode> = [];
  157. managers.forEach((item: string) => {
  158. managersListView.push(<Paragraph key={item}>{item}</Paragraph>);
  159. });
  160. const hasManagers = managers.length > 0;
  161. return (
  162. <Card
  163. style={{
  164. marginBottom: TAB_BAR_HEIGHT + 20,
  165. ...styles.card,
  166. }}
  167. >
  168. <Card.Title
  169. title={i18n.t('screens.clubs.managers')}
  170. subtitle={
  171. hasManagers
  172. ? i18n.t('screens.clubs.managersSubtitle')
  173. : i18n.t('screens.clubs.managersUnavailable')
  174. }
  175. left={(iconProps) => (
  176. <Avatar.Icon
  177. size={iconProps.size}
  178. style={styles.icon}
  179. color={
  180. hasManagers
  181. ? props.theme.colors.success
  182. : props.theme.colors.primary
  183. }
  184. icon="account-tie"
  185. />
  186. )}
  187. />
  188. <Card.Content>
  189. {managersListView}
  190. {ClubDisplayScreen.getEmailButton(email, hasManagers)}
  191. </Card.Content>
  192. </Card>
  193. );
  194. }
  195. /**
  196. * Gets the email button to contact the club, or the amicale if the club does not have any managers
  197. *
  198. * @param email The club contact email
  199. * @param hasManagers True if the club has managers
  200. * @returns {*}
  201. */
  202. static getEmailButton(email: string | null, hasManagers: boolean) {
  203. const destinationEmail =
  204. email != null && hasManagers ? email : AMICALE_MAIL;
  205. const text =
  206. email != null && hasManagers
  207. ? i18n.t('screens.clubs.clubContact')
  208. : i18n.t('screens.clubs.amicaleContact');
  209. return (
  210. <Card.Actions>
  211. <Button
  212. icon="email"
  213. mode="contained"
  214. onPress={() => {
  215. Linking.openURL(`mailto:${destinationEmail}`);
  216. }}
  217. style={styles.emailButton}
  218. >
  219. {text}
  220. </Button>
  221. </Card.Actions>
  222. );
  223. }
  224. getScreen = (data: ResponseType | undefined) => {
  225. if (data) {
  226. this.updateHeaderTitle(data);
  227. return (
  228. <CollapsibleScrollView style={styles.scroll} hasTab>
  229. {this.getCategoriesRender(data.category)}
  230. {data.logo !== null ? (
  231. <ImageGalleryButton
  232. images={[{ url: data.logo }]}
  233. style={styles.imageButton}
  234. />
  235. ) : (
  236. <View />
  237. )}
  238. {data.description !== null ? (
  239. // Surround description with div to allow text styling if the description is not html
  240. <Card.Content>
  241. <CustomHTML html={data.description} />
  242. </Card.Content>
  243. ) : (
  244. <View />
  245. )}
  246. {this.getManagersRender(data.responsibles, data.email)}
  247. </CollapsibleScrollView>
  248. );
  249. }
  250. return <View />;
  251. };
  252. /**
  253. * Updates the header title to match the given club
  254. *
  255. * @param data The club data
  256. */
  257. updateHeaderTitle(data: ClubType) {
  258. const { props } = this;
  259. props.navigation.setOptions({ title: data.name });
  260. }
  261. render() {
  262. if (this.shouldFetchData) {
  263. return (
  264. <RequestScreen
  265. request={() =>
  266. ConnectionManager.getInstance().authenticatedRequest<ResponseType>(
  267. 'clubs/info',
  268. { id: this.clubId }
  269. )
  270. }
  271. render={this.getScreen}
  272. />
  273. );
  274. }
  275. return this.getScreen(this.displayData);
  276. }
  277. }
  278. export default withTheme(ClubDisplayScreen);