Files: e8d0ace8d7fb72ebd26bc05407e4ab61357f913a / App.js
2000 bytesRaw
1 | /** |
2 | * Sample React Native App |
3 | * https://github.com/facebook/react-native |
4 | * @flow |
5 | */ |
6 | |
7 | import React, { Component } from 'react' |
8 | import { |
9 | Button, |
10 | Text, |
11 | TextInput, |
12 | View, |
13 | WebView |
14 | } from 'react-native' |
15 | |
16 | import nodejs from 'nodejs-mobile-react-native' |
17 | |
18 | import RNFS from 'react-native-fs' |
19 | |
20 | const BASE_URI = 'http://localhost:8182' |
21 | |
22 | export default class App extends Component<{}> { |
23 | constructor (props) { |
24 | super(props) |
25 | |
26 | this.state = { |
27 | key: '', |
28 | uri: '' |
29 | } |
30 | |
31 | this.getDatFiles = this.getDatFiles.bind(this) |
32 | this.setPath = this.setPath.bind(this) |
33 | } |
34 | |
35 | componentWillMount () { |
36 | nodejs.start(); |
37 | nodejs.channel.addListener( |
38 | 'message', |
39 | () => { |
40 | this.setPath() |
41 | }, |
42 | this |
43 | ); |
44 | } |
45 | |
46 | async setPath () { |
47 | try { |
48 | await fetch(`${BASE_URI}/setPath`, { |
49 | method: 'POST', |
50 | headers: { |
51 | 'Content-Type': 'application/json' |
52 | }, |
53 | body: JSON.stringify({ |
54 | path: RNFS.DocumentDirectoryPath |
55 | }) |
56 | }) |
57 | } catch (err) { |
58 | alert(JSON.stringify(err)) |
59 | } |
60 | } |
61 | |
62 | async getDatFiles () { |
63 | const { key } = this.state |
64 | |
65 | try { |
66 | const response = await fetch(`${BASE_URI}/download/${key}`, { method: 'POST' }) |
67 | alert('Dat downloaded!') |
68 | |
69 | this.setState({ |
70 | uri: `${BASE_URI}/${key}` |
71 | }) |
72 | } catch (err) { |
73 | alert(JSON.stringify(err)) |
74 | } |
75 | } |
76 | |
77 | render() { |
78 | return ( |
79 | <View style={{ |
80 | paddingTop: 40, |
81 | paddingLeft: 20, |
82 | paddingRight: 20, |
83 | flex: 1, |
84 | justifyContent: 'center' |
85 | }}> |
86 | <TextInput |
87 | onChangeText={ text => this.setState({ key: text }) } |
88 | style={{ |
89 | height: 40, |
90 | borderColor: 'gray', |
91 | borderWidth: 1 |
92 | }} /> |
93 | <Button title="Download" |
94 | onPress={ () => this.getDatFiles() } /> |
95 | |
96 | <WebView style={{ |
97 | flex: 1, |
98 | height: 300 |
99 | }} source={{ uri: this.state.uri }} /> |
100 | </View> |
101 | ); |
102 | } |
103 | } |
Built with git-ssb-web