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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. data: Array<DeviceType>;
  36. userRents: Array<RentedDeviceType>;
  37. authRef: {current: null | AuthenticatedScreen};
  38. canRefresh: boolean;
  39. constructor(props: PropsType) {
  40. super(props);
  41. this.state = {
  42. mascotDialogVisible: AsyncStorageManager.getBool(
  43. AsyncStorageManager.PREFERENCES.equipmentShowBanner.key,
  44. ),
  45. };
  46. this.canRefresh = false;
  47. this.authRef = React.createRef();
  48. props.navigation.addListener('focus', this.onScreenFocus);
  49. }
  50. onScreenFocus = () => {
  51. if (this.canRefresh && this.authRef.current != null)
  52. this.authRef.current.reload();
  53. this.canRefresh = true;
  54. };
  55. getRenderItem = ({item}: {item: DeviceType}): React.Node => {
  56. const {navigation} = this.props;
  57. return (
  58. <EquipmentListItem
  59. navigation={navigation}
  60. item={item}
  61. userDeviceRentDates={this.getUserDeviceRentDates(item)}
  62. height={LIST_ITEM_HEIGHT}
  63. />
  64. );
  65. };
  66. getUserDeviceRentDates(item: DeviceType): [number, number] | null {
  67. let dates = null;
  68. this.userRents.forEach((device: RentedDeviceType) => {
  69. if (item.id === device.device_id) {
  70. dates = [device.begin, device.end];
  71. }
  72. });
  73. return dates;
  74. }
  75. /**
  76. * Gets the list header, with explains this screen's purpose
  77. *
  78. * @returns {*}
  79. */
  80. getListHeader(): React.Node {
  81. return (
  82. <View
  83. style={{
  84. width: '100%',
  85. marginTop: 10,
  86. marginBottom: 10,
  87. }}>
  88. <Button
  89. mode="contained"
  90. icon="help-circle"
  91. onPress={this.showMascotDialog}
  92. style={{
  93. marginRight: 'auto',
  94. marginLeft: 'auto',
  95. }}>
  96. {i18n.t('screens.equipment.mascotDialog.title')}
  97. </Button>
  98. </View>
  99. );
  100. }
  101. keyExtractor = (item: ClubType): string => item.id.toString();
  102. /**
  103. * Gets the main screen component with the fetched data
  104. *
  105. * @param data The data fetched from the server
  106. * @returns {*}
  107. */
  108. getScreen = (data: Array<ApiGenericDataType | null>): React.Node => {
  109. if (data[0] != null) {
  110. const fetchedData = data[0];
  111. if (fetchedData != null) this.data = fetchedData.devices;
  112. }
  113. if (data[1] != null) {
  114. const fetchedData = data[1];
  115. if (fetchedData != null) this.userRents = fetchedData.locations;
  116. }
  117. return (
  118. <CollapsibleFlatList
  119. keyExtractor={this.keyExtractor}
  120. renderItem={this.getRenderItem}
  121. ListHeaderComponent={this.getListHeader()}
  122. data={this.data}
  123. />
  124. );
  125. };
  126. showMascotDialog = () => {
  127. this.setState({mascotDialogVisible: true});
  128. };
  129. hideMascotDialog = () => {
  130. AsyncStorageManager.set(
  131. AsyncStorageManager.PREFERENCES.equipmentShowBanner.key,
  132. false,
  133. );
  134. this.setState({mascotDialogVisible: false});
  135. };
  136. render(): React.Node {
  137. const {props, state} = this;
  138. return (
  139. <View style={{flex: 1}}>
  140. <AuthenticatedScreen
  141. navigation={props.navigation}
  142. ref={this.authRef}
  143. requests={[
  144. {
  145. link: 'location/all',
  146. params: {},
  147. mandatory: true,
  148. },
  149. {
  150. link: 'location/my',
  151. params: {},
  152. mandatory: false,
  153. },
  154. ]}
  155. renderFunction={this.getScreen}
  156. />
  157. <MascotPopup
  158. visible={state.mascotDialogVisible}
  159. title={i18n.t('screens.equipment.mascotDialog.title')}
  160. message={i18n.t('screens.equipment.mascotDialog.message')}
  161. icon="vote"
  162. buttons={{
  163. action: null,
  164. cancel: {
  165. message: i18n.t('screens.equipment.mascotDialog.button'),
  166. icon: 'check',
  167. onPress: this.hideMascotDialog,
  168. },
  169. }}
  170. emotion={MASCOT_STYLE.WINK}
  171. />
  172. </View>
  173. );
  174. }
  175. }
  176. export default withTheme(EquipmentListScreen);