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.

DashboardItem.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // @flow
  2. import * as React from 'react';
  3. import {Body, Card, CardItem, H3, Left, Text, Thumbnail} from "native-base";
  4. import CustomMaterialIcon from "./CustomMaterialIcon";
  5. import {View} from "react-native";
  6. import ThemeManager from "../utils/ThemeManager";
  7. import HTML from "react-native-render-html";
  8. import {LinearGradient} from "expo-linear-gradient";
  9. import PlatformTouchable from "react-native-platform-touchable";
  10. const CARD_BORDER_RADIUS = 10;
  11. type Props = {
  12. isAvailable: boolean,
  13. icon: string,
  14. color: string,
  15. title: string,
  16. subtitle: React.Node,
  17. clickAction: Function,
  18. isSquare: boolean,
  19. isSquareLeft: boolean,
  20. displayEvent: ?Object,
  21. }
  22. export default class DashboardItem extends React.Component<Props> {
  23. static defaultProps = {
  24. isSquare: false,
  25. isSquareLeft: true,
  26. displayEvent: undefined,
  27. };
  28. /**
  29. * Convert the date string given by in the event list json to a date object
  30. * @param dateString
  31. * @return {Date}
  32. */
  33. stringToDate(dateString: ?string): ?Date {
  34. let date = new Date();
  35. if (dateString === undefined || dateString === null)
  36. date = undefined;
  37. else if (dateString.split(' ').length > 1) {
  38. let timeStr = dateString.split(' ')[1];
  39. date.setHours(parseInt(timeStr.split(':')[0]), parseInt(timeStr.split(':')[1]), 0);
  40. } else
  41. date = undefined;
  42. return date;
  43. }
  44. padStr(i: number) {
  45. return (i < 10) ? "0" + i : "" + i;
  46. }
  47. getFormattedEventTime(event: Object): string {
  48. let formattedStr = '';
  49. let startDate = this.stringToDate(event['date_begin']);
  50. let endDate = this.stringToDate(event['date_end']);
  51. if (startDate !== undefined && startDate !== null && endDate !== undefined && endDate !== null)
  52. formattedStr = this.padStr(startDate.getHours()) + ':' + this.padStr(startDate.getMinutes()) +
  53. ' - ' + this.padStr(endDate.getHours()) + ':' + this.padStr(endDate.getMinutes());
  54. else if (startDate !== undefined && startDate !== null)
  55. formattedStr = this.padStr(startDate.getHours()) + ':' + this.padStr(startDate.getMinutes());
  56. return formattedStr
  57. }
  58. getEventPreviewContainer() {
  59. if (this.props.displayEvent !== undefined && this.props.displayEvent !== null) {
  60. return (
  61. <View>
  62. <CardItem style={{
  63. paddingTop: 0,
  64. paddingBottom: 0,
  65. backgroundColor: 'transparent',
  66. }}>
  67. <Left>
  68. {this.props.displayEvent['logo'] !== '' && this.props.displayEvent['logo'] !== null ?
  69. <Thumbnail source={{uri: this.props.displayEvent['logo']}} square/> :
  70. <View/>}
  71. <Body>
  72. <Text>{this.props.displayEvent['title']}</Text>
  73. <Text note>{this.getFormattedEventTime(this.props.displayEvent)}</Text>
  74. </Body>
  75. </Left>
  76. </CardItem>
  77. <CardItem style={{
  78. borderRadius: CARD_BORDER_RADIUS,
  79. backgroundColor: 'transparent',
  80. }}>
  81. <Body style={{
  82. height: this.props.displayEvent['description'].length > 50 ? 70 : 20,
  83. overflow: 'hidden',
  84. }}>
  85. <HTML html={"<div>" + this.props.displayEvent['description'] + "</div>"}
  86. tagsStyles={{
  87. p: {
  88. color: ThemeManager.getCurrentThemeVariables().textColor,
  89. fontSize: ThemeManager.getCurrentThemeVariables().fontSizeBase,
  90. },
  91. div: {color: ThemeManager.getCurrentThemeVariables().textColor},
  92. }}/>
  93. <LinearGradient
  94. colors={['transparent', ThemeManager.getCurrentThemeVariables().cardDefaultBg]}
  95. end={[0, 0.6]}
  96. style={{
  97. position: 'absolute',
  98. width: '100%',
  99. height: 60,
  100. bottom: 0,
  101. }}>
  102. <View style={{
  103. marginLeft: 'auto',
  104. marginTop: 'auto',
  105. flexDirection: 'row'
  106. }}>
  107. <Text style={{
  108. marginTop: 'auto',
  109. marginBottom: 'auto',
  110. padding: 0,
  111. }}>
  112. Click to see more
  113. </Text>
  114. <CustomMaterialIcon icon={'chevron-right'}/>
  115. </View>
  116. </LinearGradient>
  117. </Body>
  118. </CardItem>
  119. </View>
  120. );
  121. } else
  122. return <View/>
  123. }
  124. getIcon() {
  125. return (
  126. <CustomMaterialIcon
  127. icon={this.props.icon}
  128. color={
  129. this.props.isAvailable ?
  130. this.props.color :
  131. ThemeManager.getCurrentThemeVariables().textDisabledColor
  132. }
  133. fontSize={this.props.isSquare ? 50 : 40}
  134. width={this.props.isSquare ? 50 : 40}/>
  135. );
  136. }
  137. getText() {
  138. return (
  139. <View style={{
  140. width: this.props.isSquare ? '100%' : 'auto',
  141. }}>
  142. <H3 style={{
  143. color: this.props.isAvailable ?
  144. ThemeManager.getCurrentThemeVariables().textColor :
  145. ThemeManager.getCurrentThemeVariables().listNoteColor,
  146. textAlign: this.props.isSquare ? 'center' : 'left',
  147. width: this.props.isSquare ? '100%' : 'auto',
  148. }}>
  149. {this.props.title}
  150. </H3>
  151. <Text style={{
  152. color: this.props.isAvailable ?
  153. ThemeManager.getCurrentThemeVariables().listNoteColor :
  154. ThemeManager.getCurrentThemeVariables().textDisabledColor,
  155. textAlign: this.props.isSquare ? 'center' : 'left',
  156. width: this.props.isSquare ? '100%' : 'auto',
  157. }}>
  158. {this.props.subtitle}
  159. </Text>
  160. </View>
  161. );
  162. }
  163. getContent() {
  164. if (this.props.isSquare) {
  165. return (
  166. <Body>
  167. <View style={{marginLeft: 'auto', marginRight: 'auto'}}>
  168. {this.getIcon()}
  169. </View>
  170. {this.getText()}
  171. </Body>
  172. );
  173. } else {
  174. return (
  175. <Left>
  176. {this.getIcon()}
  177. <Body>
  178. {this.getText()}
  179. </Body>
  180. </Left>
  181. );
  182. }
  183. }
  184. render() {
  185. let marginRight = 10;
  186. if (this.props.isSquare) {
  187. if (this.props.isSquareLeft)
  188. marginRight = '4%';
  189. else
  190. marginRight = 0
  191. }
  192. return (
  193. <Card style={{
  194. flex: 0,
  195. width: this.props.isSquare ? '48%' : 'auto',
  196. marginLeft: this.props.isSquare ? 0 : 10,
  197. marginRight: marginRight,
  198. marginTop: 10,
  199. borderRadius: CARD_BORDER_RADIUS,
  200. backgroundColor: ThemeManager.getCurrentThemeVariables().cardDefaultBg,
  201. overflow: 'hidden',
  202. }}>
  203. <PlatformTouchable
  204. onPress={this.props.clickAction}
  205. style={{
  206. zIndex: 100,
  207. minHeight: this.props.isSquare ? 150 : 'auto',
  208. }}
  209. >
  210. <View>
  211. <CardItem style={{
  212. backgroundColor: 'transparent',
  213. }}>
  214. {this.getContent()}
  215. </CardItem>
  216. {this.getEventPreviewContainer()}
  217. </View>
  218. </PlatformTouchable>
  219. </Card>
  220. );
  221. }
  222. }