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.

EquipmentListItem.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // @flow
  2. import * as React from 'react';
  3. import {Avatar, List, withTheme} from 'react-native-paper';
  4. import i18n from 'i18n-js';
  5. import {StackNavigationProp} from '@react-navigation/stack';
  6. import type {CustomThemeType} from '../../../managers/ThemeManager';
  7. import type {DeviceType} from '../../../screens/Amicale/Equipment/EquipmentListScreen';
  8. import {
  9. getFirstEquipmentAvailability,
  10. getRelativeDateString,
  11. isEquipmentAvailable,
  12. } from '../../../utils/EquipmentBooking';
  13. type PropsType = {
  14. navigation: StackNavigationProp,
  15. userDeviceRentDates: [string, string],
  16. item: DeviceType,
  17. height: number,
  18. theme: CustomThemeType,
  19. };
  20. class EquipmentListItem extends React.Component<PropsType> {
  21. shouldComponentUpdate(nextProps: PropsType): boolean {
  22. const {userDeviceRentDates} = this.props;
  23. return nextProps.userDeviceRentDates !== userDeviceRentDates;
  24. }
  25. render(): React.Node {
  26. const {item, userDeviceRentDates, navigation, height, theme} = this.props;
  27. const isRented = userDeviceRentDates != null;
  28. const isAvailable = isEquipmentAvailable(item);
  29. const firstAvailability = getFirstEquipmentAvailability(item);
  30. let onPress;
  31. if (isRented)
  32. onPress = () => {
  33. navigation.navigate('equipment-confirm', {
  34. item,
  35. dates: userDeviceRentDates,
  36. });
  37. };
  38. else
  39. onPress = () => {
  40. navigation.navigate('equipment-rent', {item});
  41. };
  42. let description;
  43. if (isRented) {
  44. const start = new Date(userDeviceRentDates[0]);
  45. const end = new Date(userDeviceRentDates[1]);
  46. if (start.getTime() !== end.getTime())
  47. description = i18n.t('screens.equipment.bookingPeriod', {
  48. begin: getRelativeDateString(start),
  49. end: getRelativeDateString(end),
  50. });
  51. else
  52. description = i18n.t('screens.equipment.bookingDay', {
  53. date: getRelativeDateString(start),
  54. });
  55. } else if (isAvailable)
  56. description = i18n.t('screens.equipment.bail', {cost: item.caution});
  57. else
  58. description = i18n.t('screens.equipment.available', {
  59. date: getRelativeDateString(firstAvailability),
  60. });
  61. let icon;
  62. if (isRented) icon = 'bookmark-check';
  63. else if (isAvailable) icon = 'check-circle-outline';
  64. else icon = 'update';
  65. let color;
  66. if (isRented) color = theme.colors.warning;
  67. else if (isAvailable) color = theme.colors.success;
  68. else color = theme.colors.primary;
  69. return (
  70. <List.Item
  71. title={item.name}
  72. description={description}
  73. onPress={onPress}
  74. left={({size}: {size: number}): React.Node => (
  75. <Avatar.Icon
  76. size={size}
  77. style={{
  78. backgroundColor: 'transparent',
  79. }}
  80. icon={icon}
  81. color={color}
  82. />
  83. )}
  84. right={(): React.Node => (
  85. <Avatar.Icon
  86. style={{
  87. marginTop: 'auto',
  88. marginBottom: 'auto',
  89. backgroundColor: 'transparent',
  90. }}
  91. size={48}
  92. icon="chevron-right"
  93. />
  94. )}
  95. style={{
  96. height,
  97. justifyContent: 'center',
  98. }}
  99. />
  100. );
  101. }
  102. }
  103. export default withTheme(EquipmentListItem);