|
| 1 | +import React from 'react'; |
| 2 | +import { FlatList, Text, Modal, RefreshControl, StyleSheet, ScrollView, View, Clipboard, TouchableOpacity } from 'react-native'; |
| 3 | +import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome' |
| 4 | +import { faCopy } from '@fortawesome/free-solid-svg-icons' |
| 5 | + |
| 6 | +import SplashScreen from '../SplashScreen'; |
| 7 | +import Inspect from '../../components/Inspect.js'; |
| 8 | + |
| 9 | +import PropTypes from 'prop-types'; |
| 10 | + |
| 11 | +export default class WebsocketScreen extends React.Component |
| 12 | +{ |
| 13 | + constructor(props) { |
| 14 | + super(props); |
| 15 | + this.state = { |
| 16 | + page: 0, |
| 17 | + refreshing: false, |
| 18 | + data: [], |
| 19 | + api_id: undefined, |
| 20 | + modal: false, |
| 21 | + selected: undefined |
| 22 | + } |
| 23 | + |
| 24 | + this.onRefresh = this.onRefresh.bind(this); |
| 25 | + this.loadMore = this.loadMore.bind(this); |
| 26 | + } |
| 27 | + |
| 28 | + onRefresh() { |
| 29 | + this.setState({refreshing: true}); |
| 30 | + fetch(`${this.props.url}websocket/0`, { |
| 31 | + method: 'GET', |
| 32 | + headers: { |
| 33 | + Accept: 'application/json', |
| 34 | + 'Content-Type': 'application/json', |
| 35 | + 'authorization': `Bearer ${this.props.token}` |
| 36 | + } |
| 37 | + }).then((response) => response.json()).then((json) => { |
| 38 | + this.setState({ data: json }); |
| 39 | + this.setState({refreshing: false}); |
| 40 | + this.setState({page: 0}); |
| 41 | + }).catch((err) => { |
| 42 | + console.error(err); |
| 43 | + alert("Error: Could not connect"); |
| 44 | + this.setState({refreshing: false}); |
| 45 | + this.props.logout(); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + loadMore() { |
| 50 | + var current_page = this.state.page + 1; |
| 51 | + fetch(`${this.props.url}websocket/${current_page}`, { |
| 52 | + method: 'GET', |
| 53 | + headers: { |
| 54 | + Accept: 'application/json', |
| 55 | + 'Content-Type': 'application/json', |
| 56 | + 'authorization': `Bearer ${this.props.token}` |
| 57 | + } |
| 58 | + }).then((response) => response.json()).then((json) => { |
| 59 | + this.setState({data: [...this.state.data, ...json]}); |
| 60 | + this.setState({page: current_page}); |
| 61 | + }).catch((err) => { |
| 62 | + console.error(err); |
| 63 | + alert("Error: Could not connect"); |
| 64 | + this.props.logout(); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + componentDidMount() { |
| 69 | + this.onRefresh(); |
| 70 | + fetch(`${this.props.url}user/get_api`, { |
| 71 | + method: 'GET', |
| 72 | + headers: { |
| 73 | + Accept: 'application/json', |
| 74 | + 'Content-Type': 'application/json', |
| 75 | + 'authorization': `Bearer ${this.props.token}` |
| 76 | + } |
| 77 | + }).then((response) => response.json()).then((json) => { |
| 78 | + this.setState({ api_id: json.token }); |
| 79 | + }).catch((err) => { |
| 80 | + console.error(err); |
| 81 | + alert("Error: Could not connect"); |
| 82 | + this.props.logout(); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + render () { |
| 87 | + if (this.state.api_id == undefined) { |
| 88 | + return (<SplashScreen />); |
| 89 | + } |
| 90 | + if (this.state.data.length == 0) { |
| 91 | + return ( |
| 92 | + <View style={styles.container}> |
| 93 | + <ScrollView style={{width:'90%', height:'80%' }} |
| 94 | + refreshControl={<RefreshControl refreshing={this.state.refreshing} |
| 95 | + onRefresh={this.onRefresh}/>}> |
| 96 | + <View> |
| 97 | + <Text style={styles.header}>Waiting for requests on</Text> |
| 98 | + <TouchableOpacity style={styles.save_clip} |
| 99 | + onPress={() => Clipboard.setString(this.props.url.replace("https://", "wss://") + this.state.api_id)}> |
| 100 | + <Text style={styles.para}>{this.props.url.replace("https://", "wss://")}{this.state.api_id}</Text> |
| 101 | + <FontAwesomeIcon size={20} color={'#aaaaaa'} icon={ faCopy } /> |
| 102 | + </TouchableOpacity> |
| 103 | + </View> |
| 104 | + </ScrollView> |
| 105 | + </View> |
| 106 | + ); |
| 107 | + } |
| 108 | + return ( |
| 109 | + <View style={styles.container}> |
| 110 | + <Modal animationType={"slide"} transparent={true} |
| 111 | + visible={this.state.modal} onRequestClose={() => |
| 112 | + this.setState({modal: !this.state.modal})}> |
| 113 | + <Inspect selected={this.state.selected} run={() => |
| 114 | + this.setState({modal: !this.state.modal})} /> |
| 115 | + </Modal> |
| 116 | + <FlatList style={{width:'100%', height:'100%' }} |
| 117 | + refreshControl={<RefreshControl refreshing={this.state.refreshing} |
| 118 | + onRefresh={this.onRefresh}/>} keyExtractor={item => item._id} data={this.state.data} |
| 119 | + renderItem={({item}) => ( |
| 120 | + <Text style={styles.content}>{item.content}</Text> |
| 121 | + )} onEndReached={this.loadMore} onEndReachedThreshold={0.5} initialNumToRender={10} |
| 122 | + ListFooterComponent={<View style={{height:80}}></View>} |
| 123 | + ListHeaderComponent={<View style={{height:70}}></View>} /> |
| 124 | + </View> |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +WebsocketScreen.propTypes = { |
| 130 | + url: PropTypes.string, |
| 131 | + token: PropTypes.string, |
| 132 | + logout: PropTypes.function, |
| 133 | +} |
| 134 | + |
| 135 | +const styles = StyleSheet.create({ |
| 136 | + container: { |
| 137 | + flex: 1, |
| 138 | + width: '100%', |
| 139 | + height: '100%', |
| 140 | + backgroundColor: '#444333', |
| 141 | + alignItems: 'center', |
| 142 | + justifyContent: 'center', |
| 143 | + paddingBottom: 5, |
| 144 | + }, |
| 145 | + save_clip:{ |
| 146 | + justifyContent: 'center', |
| 147 | + flexDirection: 'row' |
| 148 | + }, |
| 149 | + header: { |
| 150 | + fontSize: 25, |
| 151 | + fontWeight: 'bold', |
| 152 | + color: '#fff', |
| 153 | + paddingLeft: 5, |
| 154 | + paddingRight: 10, |
| 155 | + marginBottom: 15, |
| 156 | + marginTop: 100, |
| 157 | + textAlign: 'center', |
| 158 | + width: '100%', |
| 159 | + }, |
| 160 | + para: { |
| 161 | + fontSize: 15, |
| 162 | + color: '#aaaaaa', |
| 163 | + paddingLeft: 5, |
| 164 | + paddingRight: 10, |
| 165 | + marginBottom: 15, |
| 166 | + textAlign: 'center', |
| 167 | + }, |
| 168 | + content: { |
| 169 | + fontSize: 15, |
| 170 | + color: '#fff', |
| 171 | + marginLeft: 10, |
| 172 | + marginRight: 10, |
| 173 | + marginTop: 0, |
| 174 | + marginBottom: 0, |
| 175 | + backgroundColor: '#333333', |
| 176 | + textAlign: 'left', |
| 177 | + }, |
| 178 | + logo: { |
| 179 | + width: 150, |
| 180 | + height: 150, |
| 181 | + marginBottom: 50, |
| 182 | + borderRadius: 30, |
| 183 | + }, |
| 184 | +}); |
0 commit comments