Skip to content

Commit 879ef3b

Browse files
committed
extract callPhone and sendEmail from Tool.js
1 parent 739e82f commit 879ef3b

File tree

3 files changed

+66
-49
lines changed

3 files changed

+66
-49
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// @flow
2+
3+
import {Alert, Clipboard} from 'react-native'
4+
import {phonecall} from 'react-native-communications'
5+
6+
export function callPhone(phoneNumber: string) {
7+
try {
8+
phonecall(phoneNumber, true)
9+
} catch (err) {
10+
Alert.alert(
11+
"Apologies, we couldn't call that number",
12+
`We were trying to call "${phoneNumber}".`,
13+
[
14+
{
15+
text: 'Darn',
16+
onPress: () => {},
17+
},
18+
{
19+
text: 'Copy number',
20+
onPress: () => Clipboard.setString(phoneNumber),
21+
},
22+
],
23+
)
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @flow
2+
3+
import {Alert, Clipboard} from 'react-native'
4+
import {email} from 'react-native-communications'
5+
6+
export function sendEmail(args: {to: Array<string>, cc: Array<string>, bcc: Array<string>, subject: string, body: string}) {
7+
const {to, cc, bcc, subject, body} = args
8+
try {
9+
email(to, cc, bcc, subject, body)
10+
} catch (err) {
11+
const toString = to.join(', ')
12+
13+
Alert.alert(
14+
"Apologies, we couldn't open an email client",
15+
`We were trying to email "${toString}".`,
16+
[
17+
{
18+
text: 'Darn',
19+
onPress: () => {},
20+
},
21+
{
22+
text: 'Copy addresses',
23+
onPress: () => Clipboard.setString(toString),
24+
},
25+
],
26+
)
27+
}
28+
}

source/views/help/tool.js

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,44 @@
11
// @flow
22

33
import * as React from 'react'
4-
import {Alert, StyleSheet, Clipboard} from 'react-native'
4+
import {StyleSheet} from 'react-native'
55
import {Card} from '../components/card'
66
import {Button} from '../components/button'
77
import {Markdown} from '../components/markdown'
8-
import actualOpenUrl from '../components/open-url'
9-
import {email, phonecall} from 'react-native-communications'
8+
import {openUrl} from '../components/open-url'
9+
import {sendEmail} from '../components/send-email'
10+
import {callPhone} from '../components/call-phone'
1011
import type {
1112
ToolOptions,
1213
CallPhoneButtonParams,
1314
SendEmailButtonParams,
1415
OpenUrlButtonParams,
1516
} from './types'
1617

17-
function callPhone(params: CallPhoneButtonParams) {
18-
try {
19-
phonecall(params.number, true)
20-
} catch (err) {
21-
Alert.alert(
22-
"Apologies, we couldn't call that number",
23-
`We were trying to call "${params.number}".`,
24-
[
25-
{
26-
text: 'Darn',
27-
onPress: () => {},
28-
},
29-
{
30-
text: 'Copy addresses',
31-
onPress: () => Clipboard.setString(params.number),
32-
},
33-
],
34-
)
35-
}
18+
function handleCallPhone(params: CallPhoneButtonParams) {
19+
callPhone(params.number)
3620
}
3721

38-
function sendEmail(params: SendEmailButtonParams) {
22+
function handleSendEmail(params: SendEmailButtonParams) {
3923
let {to, cc = [], bcc = [], subject, body} = params
4024
to = Array.isArray(to) ? to : [to]
4125
cc = Array.isArray(cc) ? cc : [cc]
4226
bcc = Array.isArray(bcc) ? bcc : [bcc]
43-
44-
try {
45-
email(to, cc, bcc, subject, body)
46-
} catch (err) {
47-
const toString = to.join(', ')
48-
49-
Alert.alert(
50-
"Apologies, we couldn't open an email client",
51-
`We were trying to email "${toString}".`,
52-
[
53-
{
54-
text: 'Darn',
55-
onPress: () => {},
56-
},
57-
{
58-
text: 'Copy addresses',
59-
onPress: () => Clipboard.setString(toString),
60-
},
61-
],
62-
)
63-
}
27+
sendEmail({to, cc, bcc, subject, body})
6428
}
6529

66-
function openUrl(params: OpenUrlButtonParams) {
67-
return actualOpenUrl(params.url)
30+
function handleOpenUrl(params: OpenUrlButtonParams) {
31+
return openUrl(params.url)
6832
}
6933

7034
function handleButtonPress(btn) {
7135
switch (btn.action) {
7236
case 'open-url':
73-
return openUrl(btn.params)
37+
return handleOpenUrl(btn.params)
7438
case 'send-email':
75-
return sendEmail(btn.params)
39+
return handleSendEmail(btn.params)
7640
case 'call-phone':
77-
return callPhone(btn.params)
41+
return handleCallPhone(btn.params)
7842
default:
7943
;(btn.action: empty)
8044
}

0 commit comments

Comments
 (0)