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.

FeedItem.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 {Button, Card, Text, TouchableRipple} from 'react-native-paper';
  21. import {Image, View} from 'react-native';
  22. import Autolink from 'react-native-autolink';
  23. import i18n from 'i18n-js';
  24. import type {FeedItemType} from '../../screens/Home/HomeScreen';
  25. import NewsSourcesConstants, {
  26. AvailablePages,
  27. } from '../../constants/NewsSourcesConstants';
  28. import type {NewsSourceType} from '../../constants/NewsSourcesConstants';
  29. import ImageGalleryButton from '../Media/ImageGalleryButton';
  30. import {useNavigation} from '@react-navigation/native';
  31. type PropsType = {
  32. item: FeedItemType;
  33. height: number;
  34. };
  35. /**
  36. * Converts a dateString using Unix Timestamp to a formatted date
  37. *
  38. * @param dateString {string} The Unix Timestamp representation of a date
  39. * @return {string} The formatted output date
  40. */
  41. function getFormattedDate(dateString: number): string {
  42. const date = new Date(dateString * 1000);
  43. return date.toLocaleString();
  44. }
  45. /**
  46. * Component used to display a feed item
  47. */
  48. function FeedItem(props: PropsType) {
  49. const navigation = useNavigation();
  50. const onPress = () => {
  51. navigation.navigate('feed-information', {
  52. data: item,
  53. date: getFormattedDate(props.item.time),
  54. });
  55. };
  56. const {item, height} = props;
  57. const image = item.image !== '' && item.image != null ? item.image : null;
  58. const pageSource: NewsSourceType =
  59. NewsSourcesConstants[item.page_id as AvailablePages];
  60. const cardMargin = 10;
  61. const cardHeight = height - 2 * cardMargin;
  62. const imageSize = 250;
  63. const titleHeight = 80;
  64. const actionsHeight = 60;
  65. const textHeight =
  66. image != null
  67. ? cardHeight - titleHeight - actionsHeight - imageSize
  68. : cardHeight - titleHeight - actionsHeight;
  69. return (
  70. <Card
  71. style={{
  72. margin: cardMargin,
  73. height: cardHeight,
  74. }}>
  75. <TouchableRipple style={{flex: 1}} onPress={onPress}>
  76. <View>
  77. <Card.Title
  78. title={pageSource.name}
  79. subtitle={getFormattedDate(item.time)}
  80. left={() => (
  81. <Image
  82. source={pageSource.icon}
  83. style={{
  84. width: 48,
  85. height: 48,
  86. }}
  87. />
  88. )}
  89. style={{height: titleHeight}}
  90. />
  91. {image != null ? (
  92. <ImageGalleryButton
  93. images={[{url: image}]}
  94. style={{
  95. width: imageSize,
  96. height: imageSize,
  97. marginLeft: 'auto',
  98. marginRight: 'auto',
  99. }}
  100. />
  101. ) : null}
  102. <Card.Content>
  103. {item.message !== undefined ? (
  104. <Autolink<typeof Text>
  105. text={item.message}
  106. hashtag="facebook"
  107. component={Text}
  108. style={{height: textHeight}}
  109. />
  110. ) : null}
  111. </Card.Content>
  112. <Card.Actions style={{height: actionsHeight}}>
  113. <Button onPress={onPress} icon="plus" style={{marginLeft: 'auto'}}>
  114. {i18n.t('screens.home.dashboard.seeMore')}
  115. </Button>
  116. </Card.Actions>
  117. </View>
  118. </TouchableRipple>
  119. </Card>
  120. );
  121. }
  122. export default React.memo(FeedItem, () => true);