Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.8KB

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