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.

ClubDisplayScreen.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // @flow
  2. import * as React from 'react';
  3. import {Linking, View} from 'react-native';
  4. import {
  5. Avatar,
  6. Button,
  7. Card,
  8. Chip,
  9. Paragraph,
  10. withTheme,
  11. } from 'react-native-paper';
  12. import ImageModal from 'react-native-image-modal';
  13. import i18n from 'i18n-js';
  14. import {StackNavigationProp} from '@react-navigation/stack';
  15. import AuthenticatedScreen from '../../../components/Amicale/AuthenticatedScreen';
  16. import CustomHTML from '../../../components/Overrides/CustomHTML';
  17. import CustomTabBar from '../../../components/Tabbar/CustomTabBar';
  18. import type {ClubCategoryType, ClubType} from './ClubListScreen';
  19. import type {CustomThemeType} from '../../../managers/ThemeManager';
  20. import {ERROR_TYPE} from '../../../utils/WebData';
  21. import CollapsibleScrollView from '../../../components/Collapsible/CollapsibleScrollView';
  22. import type {ApiGenericDataType} from '../../../utils/WebData';
  23. type PropsType = {
  24. navigation: StackNavigationProp,
  25. route: {
  26. params?: {
  27. data?: ClubType,
  28. categories?: Array<ClubCategoryType>,
  29. clubId?: number,
  30. },
  31. ...
  32. },
  33. theme: CustomThemeType,
  34. };
  35. const AMICALE_MAIL = 'clubs@amicale-insat.fr';
  36. /**
  37. * Class defining a club event information page.
  38. * If called with data and categories navigation parameters, will use those to display the data.
  39. * If called with clubId parameter, will fetch the information on the server
  40. */
  41. class ClubDisplayScreen extends React.Component<PropsType> {
  42. displayData: ClubType | null;
  43. categories: Array<ClubCategoryType> | null;
  44. clubId: number;
  45. shouldFetchData: boolean;
  46. constructor(props: PropsType) {
  47. super(props);
  48. if (props.route.params != null) {
  49. if (
  50. props.route.params.data != null &&
  51. props.route.params.categories != null
  52. ) {
  53. this.displayData = props.route.params.data;
  54. this.categories = props.route.params.categories;
  55. this.clubId = props.route.params.data.id;
  56. this.shouldFetchData = false;
  57. } else if (props.route.params.clubId != null) {
  58. this.displayData = null;
  59. this.categories = null;
  60. this.clubId = props.route.params.clubId;
  61. this.shouldFetchData = true;
  62. }
  63. }
  64. }
  65. /**
  66. * Gets the name of the category with the given ID
  67. *
  68. * @param id The category's ID
  69. * @returns {string|*}
  70. */
  71. getCategoryName(id: number): string {
  72. let categoryName = '';
  73. if (this.categories !== null) {
  74. this.categories.forEach((item: ClubCategoryType) => {
  75. if (id === item.id) categoryName = item.name;
  76. });
  77. }
  78. return categoryName;
  79. }
  80. /**
  81. * Gets the view for rendering categories
  82. *
  83. * @param categories The categories to display (max 2)
  84. * @returns {null|*}
  85. */
  86. getCategoriesRender(categories: Array<number | null>): React.Node {
  87. if (this.categories == null) return null;
  88. const final = [];
  89. categories.forEach((cat: number | null) => {
  90. if (cat != null) {
  91. final.push(
  92. <Chip style={{marginRight: 5}} key={cat}>
  93. {this.getCategoryName(cat)}
  94. </Chip>,
  95. );
  96. }
  97. });
  98. return <View style={{flexDirection: 'row', marginTop: 5}}>{final}</View>;
  99. }
  100. /**
  101. * Gets the view for rendering club managers if any
  102. *
  103. * @param managers The list of manager names
  104. * @param email The club contact email
  105. * @returns {*}
  106. */
  107. getManagersRender(managers: Array<string>, email: string | null): React.Node {
  108. const {props} = this;
  109. const managersListView = [];
  110. managers.forEach((item: string) => {
  111. managersListView.push(<Paragraph key={item}>{item}</Paragraph>);
  112. });
  113. const hasManagers = managers.length > 0;
  114. return (
  115. <Card
  116. style={{marginTop: 10, marginBottom: CustomTabBar.TAB_BAR_HEIGHT + 20}}>
  117. <Card.Title
  118. title={i18n.t('screens.clubs.managers')}
  119. subtitle={
  120. hasManagers
  121. ? i18n.t('screens.clubs.managersSubtitle')
  122. : i18n.t('screens.clubs.managersUnavailable')
  123. }
  124. left={({size}: {size: number}): React.Node => (
  125. <Avatar.Icon
  126. size={size}
  127. style={{backgroundColor: 'transparent'}}
  128. color={
  129. hasManagers
  130. ? props.theme.colors.success
  131. : props.theme.colors.primary
  132. }
  133. icon="account-tie"
  134. />
  135. )}
  136. />
  137. <Card.Content>
  138. {managersListView}
  139. {ClubDisplayScreen.getEmailButton(email, hasManagers)}
  140. </Card.Content>
  141. </Card>
  142. );
  143. }
  144. /**
  145. * Gets the email button to contact the club, or the amicale if the club does not have any managers
  146. *
  147. * @param email The club contact email
  148. * @param hasManagers True if the club has managers
  149. * @returns {*}
  150. */
  151. static getEmailButton(
  152. email: string | null,
  153. hasManagers: boolean,
  154. ): React.Node {
  155. const destinationEmail =
  156. email != null && hasManagers ? email : AMICALE_MAIL;
  157. const text =
  158. email != null && hasManagers
  159. ? i18n.t('screens.clubs.clubContact')
  160. : i18n.t('screens.clubs.amicaleContact');
  161. return (
  162. <Card.Actions>
  163. <Button
  164. icon="email"
  165. mode="contained"
  166. onPress={() => {
  167. Linking.openURL(`mailto:${destinationEmail}`);
  168. }}
  169. style={{marginLeft: 'auto'}}>
  170. {text}
  171. </Button>
  172. </Card.Actions>
  173. );
  174. }
  175. getScreen = (response: Array<ApiGenericDataType | null>): React.Node => {
  176. const {props} = this;
  177. let data: ClubType | null = null;
  178. if (response[0] != null) {
  179. [data] = response;
  180. this.updateHeaderTitle(data);
  181. }
  182. if (data != null) {
  183. return (
  184. <CollapsibleScrollView style={{paddingLeft: 5, paddingRight: 5}} hasTab>
  185. {this.getCategoriesRender(data.category)}
  186. {data.logo !== null ? (
  187. <View
  188. style={{
  189. marginLeft: 'auto',
  190. marginRight: 'auto',
  191. marginTop: 10,
  192. marginBottom: 10,
  193. }}>
  194. <ImageModal
  195. resizeMode="contain"
  196. imageBackgroundColor={props.theme.colors.background}
  197. style={{
  198. width: 300,
  199. height: 300,
  200. }}
  201. source={{
  202. uri: data.logo,
  203. }}
  204. />
  205. </View>
  206. ) : (
  207. <View />
  208. )}
  209. {data.description !== null ? (
  210. // Surround description with div to allow text styling if the description is not html
  211. <Card.Content>
  212. <CustomHTML html={data.description} />
  213. </Card.Content>
  214. ) : (
  215. <View />
  216. )}
  217. {this.getManagersRender(data.responsibles, data.email)}
  218. </CollapsibleScrollView>
  219. );
  220. }
  221. return null;
  222. };
  223. /**
  224. * Updates the header title to match the given club
  225. *
  226. * @param data The club data
  227. */
  228. updateHeaderTitle(data: ClubType) {
  229. const {props} = this;
  230. props.navigation.setOptions({title: data.name});
  231. }
  232. render(): React.Node {
  233. const {props} = this;
  234. if (this.shouldFetchData)
  235. return (
  236. <AuthenticatedScreen
  237. navigation={props.navigation}
  238. requests={[
  239. {
  240. link: 'clubs/info',
  241. params: {id: this.clubId},
  242. mandatory: true,
  243. },
  244. ]}
  245. renderFunction={this.getScreen}
  246. errorViewOverride={[
  247. {
  248. errorCode: ERROR_TYPE.BAD_INPUT,
  249. message: i18n.t('screens.clubs.invalidClub'),
  250. icon: 'account-question',
  251. showRetryButton: false,
  252. },
  253. ]}
  254. />
  255. );
  256. return this.getScreen([this.displayData]);
  257. }
  258. }
  259. export default withTheme(ClubDisplayScreen);