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

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