forked from sipgate-io/fax-sample-app-minimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
139 lines (123 loc) · 3.34 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useState} from 'react';
import {createFaxModule, sipgateIO} from 'sipgateio';
import {SafeAreaView, TextInput, Button, Alert, View} from 'react-native';
import {Picker} from '@react-native-picker/picker';
import * as RNFS from 'react-native-fs';
import DocumentPicker from 'react-native-document-picker';
import {Buffer} from 'buffer';
const App = () => {
const [tokenId, setTokenId] = useState('');
const [token, setToken] = useState('');
const [client, setClient] = useState(null);
const [faxlines, setFaxlines] = useState([]);
const [recipient, setRecipient] = useState('');
const [selectedFaxline, setSelectedFaxline] = useState(null);
const [selectedFile, setSelectedFile] = useState(null);
const login = async () => {
const client = sipgateIO({
tokenId,
token,
});
await client.getAuthenticatedWebuserId();
const faxModule = createFaxModule(client);
const lines = await faxModule.getFaxlines();
if (lines.length === 0) {
Alert.alert('No Fax Lines found');
} else {
setFaxlines(lines);
setSelectedFaxline(lines[0]);
}
setClient(client);
};
const sendFax = async () => {
const file = await RNFS.readFile(selectedFile.uri, 'base64');
const buffer = Buffer.from(file, 'base64');
const fax = {
to: recipient,
fileContent: buffer,
filename: selectFile.name,
faxlineId: selectedFaxline.id,
};
try {
const faxModule = createFaxModule(client);
await faxModule.send(fax);
Alert.alert('Fax sent successfully!');
} catch (err) {
Alert.alert('Error', err.message);
}
};
const selectFile = async () => {
try {
const file = await DocumentPicker.pick({
type: [DocumentPicker.types.pdf],
});
setSelectedFile(file);
} catch (err) {
if (!DocumentPicker.isCancel(err)) {
Alert.alert('Error', err.message);
}
}
};
const renderLogin = () => {
return (
<View>
<TextInput
placeholder="TokenId"
value={tokenId}
onChangeText={setTokenId}
/>
<TextInput
placeholder="Token"
value={token}
onChangeText={setToken}
secureTextEntry
/>
<Button
title="Login"
onPress={() => login().catch((e) => Alert.alert('Error', e.message))}
/>
</View>
);
};
const renderMain = () => {
return (
<View>
<Picker
selectedValue={selectedFaxline}
onValueChange={setSelectedFaxline}>
{faxlines.map((line) => (
<Picker.Item key={line.id} label={line.alias} value={line.id} />
))}
</Picker>
<TextInput
placeholder="Recipient"
value={recipient}
onChangeText={setRecipient}
/>
<Button
title={selectedFile?.name || 'Choose File...'}
onPress={selectFile}
/>
<Button
title="Send"
onPress={sendFax}
disabled={!selectedFaxline || !recipient || !selectedFile}
/>
</View>
);
};
return (
<SafeAreaView>
{!client && renderLogin()}
{client && renderMain()}
</SafeAreaView>
);
};
export default App;