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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import {Button, withTheme} from 'react-native-paper';
  5. import {StackNavigationProp} from '@react-navigation/stack';
  6. import i18n from 'i18n-js';
  7. import AuthenticatedScreen from '../../../components/Amicale/AuthenticatedScreen';
  8. import type {ClubType} from '../Clubs/ClubListScreen';
  9. import EquipmentListItem from '../../../components/Lists/Equipment/EquipmentListItem';
  10. import MascotPopup from '../../../components/Mascot/MascotPopup';
  11. import {MASCOT_STYLE} from '../../../components/Mascot/Mascot';
  12. import AsyncStorageManager from '../../../managers/AsyncStorageManager';
  13. import CollapsibleFlatList from '../../../components/Collapsible/CollapsibleFlatList';
  14. import type {ApiGenericDataType} from '../../../utils/WebData';
  15. type PropsType = {
  16. navigation: StackNavigationProp,
  17. };
  18. type StateType = {
  19. mascotDialogVisible: boolean,
  20. };
  21. export type DeviceType = {
  22. id: number,
  23. name: string,
  24. caution: number,
  25. booked_at: Array<{begin: string, end: string}>,
  26. };
  27. export type RentedDeviceType = {
  28. device_id: number,
  29. device_name: string,
  30. begin: string,
  31. end: string,
  32. };
  33. const LIST_ITEM_HEIGHT = 64;
  34. class EquipmentListScreen extends React.Component<PropsType, StateType> {
  35. userRents: null | Array<RentedDeviceType>;
  36. authRef: {current: null | AuthenticatedScreen};
  37. canRefresh: boolean;
  38. constructor(props: PropsType) {
  39. super(props);
  40. this.state = {
  41. mascotDialogVisible: AsyncStorageManager.getBool(
  42. AsyncStorageManager.PREFERENCES.equipmentShowBanner.key,
  43. ),
  44. };
  45. this.canRefresh = false;
  46. this.authRef = React.createRef();
  47. props.navigation.addListener('focus', this.onScreenFocus);
  48. }
  49. onScreenFocus = () => {
  50. if (this.canRefresh && this.authRef.current != null)
  51. this.authRef.current.reload();
  52. this.canRefresh = true;
  53. };
  54. getRenderItem = ({item}: {item: DeviceType}): React.Node => {
  55. const {navigation} = this.props;
  56. return (
  57. <EquipmentListItem
  58. navigation={navigation}
  59. item={item}
  60. userDeviceRentDates={this.getUserDeviceRentDates(item)}
  61. height={LIST_ITEM_HEIGHT}
  62. />
  63. );
  64. };
  65. getUserDeviceRentDates(item: DeviceType): [number, number] | null {
  66. let dates = null;
  67. if (this.userRents != null) {
  68. this.userRents.forEach((device: RentedDeviceType) => {
  69. if (item.id === device.device_id) {
  70. dates = [device.begin, device.end];
  71. }
  72. });
  73. }
  74. return dates;
  75. }
  76. /**
  77. * Gets the list header, with explains this screen's purpose
  78. *
  79. * @returns {*}
  80. */
  81. getListHeader(): React.Node {
  82. return (
  83. <View
  84. style={{
  85. width: '100%',
  86. marginTop: 10,
  87. marginBottom: 10,
  88. }}>
  89. <Button
  90. mode="contained"
  91. icon="help-circle"
  92. onPress={this.showMascotDialog}
  93. style={{
  94. marginRight: 'auto',
  95. marginLeft: 'auto',
  96. }}>
  97. {i18n.t('screens.equipment.mascotDialog.title')}
  98. </Button>
  99. </View>
  100. );
  101. }
  102. keyExtractor = (item: ClubType): string => item.id.toString();
  103. /**
  104. * Gets the main screen component with the fetched data
  105. *
  106. * @param data The data fetched from the server
  107. * @returns {*}
  108. */
  109. getScreen = (data: Array<ApiGenericDataType | null>): React.Node => {
  110. const [allDevices, userRents] = data;
  111. if (userRents != null) this.userRents = userRents.locations;
  112. return (
  113. <CollapsibleFlatList
  114. keyExtractor={this.keyExtractor}
  115. renderItem={this.getRenderItem}
  116. ListHeaderComponent={this.getListHeader()}
  117. data={allDevices != null ? allDevices.devices : null}
  118. />
  119. );
  120. };
  121. showMascotDialog = () => {
  122. this.setState({mascotDialogVisible: true});
  123. };
  124. hideMascotDialog = () => {
  125. AsyncStorageManager.set(
  126. AsyncStorageManager.PREFERENCES.equipmentShowBanner.key,
  127. false,
  128. );
  129. this.setState({mascotDialogVisible: false});
  130. };
  131. render(): React.Node {
  132. const {props, state} = this;
  133. return (
  134. <View style={{flex: 1}}>
  135. <AuthenticatedScreen
  136. navigation={props.navigation}
  137. ref={this.authRef}
  138. requests={[
  139. {
  140. link: 'location/all',
  141. params: {},
  142. mandatory: true,
  143. },
  144. {
  145. link: 'location/my',
  146. params: {},
  147. mandatory: false,
  148. },
  149. ]}
  150. renderFunction={this.getScreen}
  151. />
  152. <MascotPopup
  153. visible={state.mascotDialogVisible}
  154. title={i18n.t('screens.equipment.mascotDialog.title')}
  155. message={i18n.t('screens.equipment.mascotDialog.message')}
  156. icon="vote"
  157. buttons={{
  158. action: null,
  159. cancel: {
  160. message: i18n.t('screens.equipment.mascotDialog.button'),
  161. icon: 'check',
  162. onPress: this.hideMascotDialog,
  163. },
  164. }}
  165. emotion={MASCOT_STYLE.WINK}
  166. />
  167. </View>
  168. );
  169. }
  170. }
  171. export default withTheme(EquipmentListScreen);