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

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