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.

EquipmentListScreen.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 { StyleSheet, View } from 'react-native';
  21. import { Button } from 'react-native-paper';
  22. import { StackNavigationProp } from '@react-navigation/stack';
  23. import i18n from 'i18n-js';
  24. import EquipmentListItem from '../../../components/Lists/Equipment/EquipmentListItem';
  25. import MascotPopup from '../../../components/Mascot/MascotPopup';
  26. import { MASCOT_STYLE } from '../../../components/Mascot/Mascot';
  27. import AsyncStorageManager from '../../../managers/AsyncStorageManager';
  28. import GENERAL_STYLES from '../../../constants/Styles';
  29. import ConnectionManager from '../../../managers/ConnectionManager';
  30. import { ApiRejectType } from '../../../utils/WebData';
  31. import WebSectionList from '../../../components/Screens/WebSectionList';
  32. type PropsType = {
  33. navigation: StackNavigationProp<any>;
  34. };
  35. type StateType = {
  36. mascotDialogVisible: boolean;
  37. };
  38. export type DeviceType = {
  39. id: number;
  40. name: string;
  41. caution: number;
  42. booked_at: Array<{ begin: string; end: string }>;
  43. };
  44. export type RentedDeviceType = {
  45. device_id: number;
  46. device_name: string;
  47. begin: string;
  48. end: string;
  49. };
  50. type ResponseType = {
  51. devices: Array<DeviceType>;
  52. locations?: Array<RentedDeviceType>;
  53. };
  54. const LIST_ITEM_HEIGHT = 64;
  55. const styles = StyleSheet.create({
  56. headerContainer: {
  57. width: '100%',
  58. marginTop: 10,
  59. marginBottom: 10,
  60. },
  61. });
  62. class EquipmentListScreen extends React.Component<PropsType, StateType> {
  63. userRents: null | Array<RentedDeviceType>;
  64. constructor(props: PropsType) {
  65. super(props);
  66. this.userRents = null;
  67. this.state = {
  68. mascotDialogVisible: AsyncStorageManager.getBool(
  69. AsyncStorageManager.PREFERENCES.equipmentShowMascot.key
  70. ),
  71. };
  72. }
  73. getRenderItem = ({ item }: { item: DeviceType }) => {
  74. const { navigation } = this.props;
  75. return (
  76. <EquipmentListItem
  77. navigation={navigation}
  78. item={item}
  79. userDeviceRentDates={this.getUserDeviceRentDates(item)}
  80. height={LIST_ITEM_HEIGHT}
  81. />
  82. );
  83. };
  84. getUserDeviceRentDates(item: DeviceType): [string, string] | null {
  85. let dates = null;
  86. if (this.userRents != null) {
  87. this.userRents.forEach((device: RentedDeviceType) => {
  88. if (item.id === device.device_id) {
  89. dates = [device.begin, device.end];
  90. }
  91. });
  92. }
  93. return dates;
  94. }
  95. /**
  96. * Gets the list header, with explains this screen's purpose
  97. *
  98. * @returns {*}
  99. */
  100. getListHeader() {
  101. return (
  102. <View style={styles.headerContainer}>
  103. <Button
  104. mode="contained"
  105. icon="help-circle"
  106. onPress={this.showMascotDialog}
  107. style={GENERAL_STYLES.centerHorizontal}
  108. >
  109. {i18n.t('screens.equipment.mascotDialog.title')}
  110. </Button>
  111. </View>
  112. );
  113. }
  114. keyExtractor = (item: DeviceType): string => item.id.toString();
  115. createDataset = (data: ResponseType | undefined) => {
  116. if (data) {
  117. const userRents = data.locations;
  118. if (userRents) {
  119. this.userRents = userRents;
  120. }
  121. return [{ title: '', data: data.devices }];
  122. } else {
  123. return [];
  124. }
  125. };
  126. showMascotDialog = () => {
  127. this.setState({ mascotDialogVisible: true });
  128. };
  129. hideMascotDialog = () => {
  130. AsyncStorageManager.set(
  131. AsyncStorageManager.PREFERENCES.equipmentShowMascot.key,
  132. false
  133. );
  134. this.setState({ mascotDialogVisible: false });
  135. };
  136. request = () => {
  137. return new Promise(
  138. (
  139. resolve: (data: ResponseType) => void,
  140. reject: (error: ApiRejectType) => void
  141. ) => {
  142. ConnectionManager.getInstance()
  143. .authenticatedRequest<{ devices: Array<DeviceType> }>('location/all')
  144. .then((devicesData) => {
  145. ConnectionManager.getInstance()
  146. .authenticatedRequest<{
  147. locations: Array<RentedDeviceType>;
  148. }>('location/my')
  149. .then((rentsData) => {
  150. resolve({
  151. devices: devicesData.devices,
  152. locations: rentsData.locations,
  153. });
  154. })
  155. .catch(() =>
  156. resolve({
  157. devices: devicesData.devices,
  158. })
  159. );
  160. })
  161. .catch(reject);
  162. }
  163. );
  164. };
  165. render() {
  166. const { state } = this;
  167. return (
  168. <View style={GENERAL_STYLES.flex}>
  169. <WebSectionList
  170. request={this.request}
  171. createDataset={this.createDataset}
  172. keyExtractor={this.keyExtractor}
  173. renderItem={this.getRenderItem}
  174. renderListHeaderComponent={() => this.getListHeader()}
  175. />
  176. <MascotPopup
  177. visible={state.mascotDialogVisible}
  178. title={i18n.t('screens.equipment.mascotDialog.title')}
  179. message={i18n.t('screens.equipment.mascotDialog.message')}
  180. icon="vote"
  181. buttons={{
  182. cancel: {
  183. message: i18n.t('screens.equipment.mascotDialog.button'),
  184. icon: 'check',
  185. onPress: this.hideMascotDialog,
  186. },
  187. }}
  188. emotion={MASCOT_STYLE.WINK}
  189. />
  190. </View>
  191. );
  192. }
  193. }
  194. export default EquipmentListScreen;