Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import { useQuery, useSubscription } from '@apollo/react-hooks';
  2. import React from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { ActivityIndicator, StyleSheet, View } from 'react-native';
  5. import { NavigationParams, NavigationScreenProp, NavigationState } from 'react-navigation';
  6. import { ScreenProps } from '../../../../App';
  7. import CustomIcon from '../../../assets/icons/CustomIcon';
  8. import { CustomButton } from '../../../components/button/CustomButton';
  9. import Header from '../../../components/text/Header';
  10. import SubHeader from '../../../components/text/SubHeader';
  11. import Constant from '../../../constants/Constant';
  12. import { SUBSCRIBE_GATEWAY, USER } from '../../../queries/queries';
  13. import {
  14. retrieveGatewayPings,
  15. retrieveGatewayPingsVariables,
  16. } from '../../../screens/Setup/__generated__/retrieveGatewayPings';
  17.  
  18. import { user } from '../../HomeScreen/__generated__/user';
  19.  
  20. interface Props {
  21. navigation: NavigationScreenProp<NavigationState, NavigationParams>;
  22. screenProps: ScreenProps;
  23. }
  24.  
  25. function createToastAndSetFound(navigation: NavigationScreenProp<NavigationState, NavigationParams>, setFound: any) {
  26. setFound(true);
  27. setTimeout(() => {
  28. navigation.navigate('PairingSensors');
  29. }, 200);
  30. }
  31.  
  32. const SearchingYourHub = ({ navigation, screenProps }: Props) => {
  33. const { t, i18n } = useTranslation(['onboarding']);
  34. const { data: userData, error, fetchMore } = useQuery<user>(USER, { fetchPolicy: 'cache-first' });
  35. if (!(userData && userData.user)) return null;
  36. const user = userData.user;
  37.  
  38. const [found, setFound] = useState(false);
  39.  
  40. const { data } = useSubscription<retrieveGatewayPings, retrieveGatewayPingsVariables>(SUBSCRIBE_GATEWAY, {
  41. variables: { gatewayId: user.gateway.id },
  42. onSubscriptionData: ({ client }) => {
  43. fetchMore({
  44. updateQuery: () => {
  45. userData && screenProps.updateState({ user: userData.user });
  46. if (
  47. (data && data.retrieveGatewayPings) ||
  48. (userData && userData.user && userData.user.gateway.status === 'ACTIVE')
  49. ) {
  50. createToastAndSetFound(navigation, setFound);
  51. }
  52. },
  53. });
  54. },
  55. });
  56. return (
  57. <View style={styles.container}>
  58. <View style={[styles.circle, found ? styles.solid : styles.dashed]}>
  59. <CustomIcon name="hub" size={18} style={styles.image} />
  60. </View>
  61. <Header>Test</Header>
  62. <SubHeader>Test</SubHeader>
  63. {(!found && <ActivityIndicator size="large" color="#0000ff" />) || (
  64. <View style={styles.buttonContainer}>
  65. <CustomButton
  66. title={t('next', { ns: 'fields', defaultValue: 'Next', lng: i18n.language })}
  67. onPress={}
  68. style={styles.buttonStyle}
  69. textStyle={styles.buttonTextStyle}
  70. />
  71. </View>
  72. )}
  73. </View>
  74. );
  75. };
  76.  
  77. export default SearchingYourHub;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement