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