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.9KB

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