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.

EquipmentListScreen.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // @flow
  2. import * as React from 'react';
  3. import {Animated, Image} from "react-native";
  4. import {Card, Paragraph, withTheme} from 'react-native-paper';
  5. import AuthenticatedScreen from "../../../components/Amicale/AuthenticatedScreen";
  6. import {Collapsible} from "react-navigation-collapsible";
  7. import {withCollapsible} from "../../../utils/withCollapsible";
  8. import {StackNavigationProp} from "@react-navigation/stack";
  9. import type {CustomTheme} from "../../../managers/ThemeManager";
  10. import i18n from "i18n-js";
  11. import type {club} from "../Clubs/ClubListScreen";
  12. import EquipmentListItem from "../../../components/Lists/Equipment/EquipmentListItem";
  13. type Props = {
  14. navigation: StackNavigationProp,
  15. theme: CustomTheme,
  16. collapsibleStack: Collapsible,
  17. }
  18. export type Device = {
  19. id: number,
  20. name: string,
  21. caution: number,
  22. booked_at: Array<{begin: string, end: string}>,
  23. };
  24. export type RentedDevice = {
  25. device_id: number,
  26. device_name: string,
  27. begin: string,
  28. end: string,
  29. }
  30. const ICON_AMICALE = require('../../../../assets/amicale.png');
  31. const LIST_ITEM_HEIGHT = 64;
  32. class EquipmentListScreen extends React.Component<Props> {
  33. data: Array<Device>;
  34. userRents: Array<RentedDevice>;
  35. authRef: { current: null | AuthenticatedScreen };
  36. canRefresh: boolean;
  37. constructor(props: Props) {
  38. super(props);
  39. this.canRefresh = false;
  40. this.authRef = React.createRef();
  41. this.props.navigation.addListener('focus', this.onScreenFocus);
  42. }
  43. onScreenFocus = () => {
  44. if (this.canRefresh && this.authRef.current != null)
  45. this.authRef.current.reload();
  46. this.canRefresh = true;
  47. };
  48. getRenderItem = ({item}: { item: Device }) => {
  49. return (
  50. <EquipmentListItem
  51. navigation={this.props.navigation}
  52. item={item}
  53. userDeviceRentDates={this.getUserDeviceRentDates(item)}
  54. height={LIST_ITEM_HEIGHT}/>
  55. );
  56. };
  57. getUserDeviceRentDates(item: Device) {
  58. let dates = null;
  59. for (let i = 0; i < this.userRents.length; i++) {
  60. let device = this.userRents[i];
  61. if (item.id === device.device_id) {
  62. dates = [device.begin, device.end];
  63. break;
  64. }
  65. }
  66. return dates;
  67. }
  68. /**
  69. * Gets the list header, with explains this screen's purpose
  70. *
  71. * @returns {*}
  72. */
  73. getListHeader() {
  74. return <Card style={{margin: 5}}>
  75. <Card.Title
  76. title={i18n.t('screens.equipment.title')}
  77. left={(props) => <Image
  78. {...props}
  79. style={{
  80. width: props.size,
  81. height: props.size,
  82. }}
  83. source={ICON_AMICALE}
  84. />}
  85. />
  86. <Card.Content>
  87. <Paragraph>
  88. {i18n.t('screens.equipment.message')}
  89. </Paragraph>
  90. </Card.Content>
  91. </Card>;
  92. }
  93. keyExtractor = (item: club) => item.id.toString();
  94. /**
  95. * Gets the main screen component with the fetched data
  96. *
  97. * @param data The data fetched from the server
  98. * @returns {*}
  99. */
  100. getScreen = (data: Array<{ [key: string]: any } | null>) => {
  101. if (data[0] != null) {
  102. const fetchedData = data[0];
  103. if (fetchedData != null)
  104. this.data = fetchedData["devices"];
  105. }
  106. if (data[1] != null) {
  107. const fetchedData = data[1];
  108. if (fetchedData != null)
  109. this.userRents = fetchedData["locations"];
  110. }
  111. const {containerPaddingTop, scrollIndicatorInsetTop, onScroll} = this.props.collapsibleStack;
  112. return (
  113. <Animated.FlatList
  114. keyExtractor={this.keyExtractor}
  115. renderItem={this.getRenderItem}
  116. ListHeaderComponent={this.getListHeader()}
  117. data={this.data}
  118. // Animations
  119. onScroll={onScroll}
  120. contentContainerStyle={{
  121. paddingTop: containerPaddingTop,
  122. minHeight: '100%'
  123. }}
  124. scrollIndicatorInsets={{top: scrollIndicatorInsetTop}}
  125. />
  126. )
  127. };
  128. render() {
  129. return (
  130. <AuthenticatedScreen
  131. {...this.props}
  132. ref={this.authRef}
  133. requests={[
  134. {
  135. link: 'location/all',
  136. params: {},
  137. mandatory: true,
  138. },
  139. {
  140. link: 'location/my',
  141. params: {},
  142. mandatory: false,
  143. }
  144. ]}
  145. renderFunction={this.getScreen}
  146. />
  147. );
  148. }
  149. }
  150. export default withCollapsible(withTheme(EquipmentListScreen));