|
| 1 | +/** |
| 2 | + * @format |
| 3 | + * @flow strict-local |
| 4 | + */ |
| 5 | +import React from 'react'; |
| 6 | +import {useTranslation} from 'react-i18next'; |
| 7 | +import {Button, RefreshControl} from 'react-native'; |
| 8 | +import {Colors} from '../../styles'; |
| 9 | +import {FlatList} from 'react-native-gesture-handler'; |
| 10 | + |
| 11 | +import DeviceRow from '../../components/devices/DeviceRow'; |
| 12 | +import DeviceIcons from '../../components/devices/DeviceIcons'; |
| 13 | + |
| 14 | +import {FormatBytes, LastUploadTime} from '../../util'; |
| 15 | + |
| 16 | +const DeviceList: () => React$Node = ({devices, refreshing, onRefresh}) => { |
| 17 | + const {t} = useTranslation(['api', 'devices']); |
| 18 | + |
| 19 | + const hasError = device => !!device.status.error; |
| 20 | + |
| 21 | + const statusForManualTransfer = deviceID => { |
| 22 | + // Note: Axivity, eBedSensor, & McRoberts data transfer is at end |
| 23 | + return ['AX6', 'BED', 'MMM'].includes(deviceID) |
| 24 | + ? t('devices:status.manualTransfer') |
| 25 | + : t('devices:status.noData'); |
| 26 | + }; |
| 27 | + |
| 28 | + const setSyncStatus = device => { |
| 29 | + const {days, hours} = LastUploadTime(device.status.data.lastUploaded); |
| 30 | + const lastUploaded = |
| 31 | + days > 0 |
| 32 | + ? t('devices:status.daysAgo', {days}) |
| 33 | + : t('devices:status.hoursAgo', {hours}); |
| 34 | + const filesize = FormatBytes(device.status.data.size); |
| 35 | + |
| 36 | + const status = !device.status.data.isOnDevice |
| 37 | + ? statusForManualTransfer(device.id) |
| 38 | + : t('devices:status.sync', { |
| 39 | + filesize, |
| 40 | + lastUploaded, |
| 41 | + }); |
| 42 | + |
| 43 | + return hasError(device) ? t(`${device.status.error}.message`) : status; |
| 44 | + }; |
| 45 | + |
| 46 | + const renderDeviceIcons = device => { |
| 47 | + if (hasError(device)) { |
| 48 | + return ( |
| 49 | + <Button |
| 50 | + title={t(`${device.status.error}.action`)} |
| 51 | + color={Colors.PRIMARY} |
| 52 | + // TODO: navigate to appropriate SupportDoc#Header |
| 53 | + // onPress={() => {}} |
| 54 | + /> |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + return <DeviceIcons key={device.id} status={device.status.hardware} />; |
| 59 | + }; |
| 60 | + |
| 61 | + const renderItem = ({item: device}) => ( |
| 62 | + <DeviceRow |
| 63 | + key={device.id} |
| 64 | + name={device.name} |
| 65 | + image={device.image} |
| 66 | + status={setSyncStatus(device)} |
| 67 | + children={renderDeviceIcons(device)} |
| 68 | + isError={hasError(device)} |
| 69 | + /> |
| 70 | + ); |
| 71 | + |
| 72 | + const compareByError = (a, b) => !hasError(a) || hasError(b); |
| 73 | + |
| 74 | + return ( |
| 75 | + <FlatList |
| 76 | + alwaysBounceVertical={true} |
| 77 | + showsVerticalScrollIndicator={false} |
| 78 | + refreshControl={ |
| 79 | + <RefreshControl |
| 80 | + enabled={true} |
| 81 | + colors={[Colors.PRIMARY]} |
| 82 | + refreshing={refreshing} |
| 83 | + onRefresh={onRefresh} |
| 84 | + /> |
| 85 | + } |
| 86 | + data={devices.sort(compareByError)} |
| 87 | + renderItem={renderItem} |
| 88 | + keyExtractor={device => device.name} |
| 89 | + /> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +export default DeviceList; |
0 commit comments