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.tsx 3.7KB

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