Skip to content

Commit 10d92f8

Browse files
authored
Merge pull request #2130 from StoDevX/generic-phone-call-and-email-funcs
Use generic phone call and email functions
2 parents 6850e43 + 5eac2e1 commit 10d92f8

File tree

12 files changed

+130
-104
lines changed

12 files changed

+130
-104
lines changed

source/views/building-hours/report/submit.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import jsYaml from 'js-yaml'
44
import type {BuildingType} from '../types'
5-
import {email} from 'react-native-communications'
5+
import {sendEmail} from '../../components/send-email'
66
import querystring from 'querystring'
77
import {GH_NEW_ISSUE_URL} from '../../../globals'
88

@@ -13,13 +13,11 @@ export function submitReport(current: BuildingType, suggestion: BuildingType) {
1313

1414
const body = makeEmailBody(before, after, current.name)
1515

16-
return email(
17-
18-
[],
19-
[],
20-
`[building] Suggestion for ${current.name}`,
16+
return sendEmail({
17+
18+
subject: `[building] Suggestion for ${current.name}`,
2119
body,
22-
)
20+
})
2321
}
2422

2523
function makeEmailBody(before: string, after: string, title: string): string {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @flow
2+
3+
import {Alert, Clipboard} from 'react-native'
4+
import {phonecall} from 'react-native-communications'
5+
6+
type Options = {|
7+
prompt?: boolean,
8+
|}
9+
10+
export function callPhone(phoneNumber: string, opts?: Options) {
11+
const {prompt = true} = opts || {}
12+
try {
13+
phonecall(phoneNumber, prompt)
14+
} catch (err) {
15+
Alert.alert(
16+
"Apologies, we couldn't call that number",
17+
`We were trying to call "${phoneNumber}".`,
18+
[
19+
{
20+
text: 'Darn',
21+
onPress: () => {},
22+
},
23+
{
24+
text: 'Copy number',
25+
onPress: () => Clipboard.setString(phoneNumber),
26+
},
27+
],
28+
)
29+
}
30+
}

source/views/components/open-url.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default function openUrl(url: string) {
7878
return genericOpen(url)
7979
}
8080
}
81+
export {openUrl}
8182

8283
export function trackedOpenUrl({url, id}: {url: string, id?: string}) {
8384
tracker.trackScreenView(id || url)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @flow
2+
3+
import {Alert, Clipboard} from 'react-native'
4+
import {email} from 'react-native-communications'
5+
6+
type Args = {|
7+
to?: Array<string>,
8+
cc?: Array<string>,
9+
bcc?: Array<string>,
10+
subject?: string,
11+
body?: string,
12+
|}
13+
14+
export function sendEmail(args: Args) {
15+
const {to = [], cc = [], bcc = [], subject = '', body = ''} = args
16+
try {
17+
email(to, cc, bcc, subject, body)
18+
} catch (err) {
19+
const toString = to.join(', ')
20+
21+
Alert.alert(
22+
"Apologies, we couldn't open an email client",
23+
`We were trying to email "${toString}".`,
24+
[
25+
{
26+
text: 'Darn',
27+
onPress: () => {},
28+
},
29+
{
30+
text: 'Copy addresses',
31+
onPress: () => Clipboard.setString(toString),
32+
},
33+
],
34+
)
35+
}
36+
}

source/views/contacts/contact-detail.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {contactImages} from '../../../images/contact-images'
55
import {Markdown} from '../components/markdown'
66
import {ListFooter} from '../components/list'
77
import glamorous from 'glamorous-native'
8-
import {phonecall} from 'react-native-communications'
8+
import {callPhone} from '../components/call-phone'
99
import {tracker} from '../../analytics'
1010
import {Button} from '../components/button'
11-
import openUrl from '../components/open-url'
11+
import {openUrl} from '../components/open-url'
1212
import type {ContactType} from './types'
1313
import {GH_NEW_ISSUE_URL} from '../../globals'
1414

@@ -41,8 +41,8 @@ function formatNumber(phoneNumber: string) {
4141

4242
function promptCall(buttonText: string, phoneNumber: string) {
4343
Alert.alert(buttonText, formatNumber(phoneNumber), [
44-
{text: 'Cancel', onPress: () => console.log('Call cancel pressed')},
45-
{text: 'Call', onPress: () => phonecall(phoneNumber, false)},
44+
{text: 'Cancel', onPress: () => {}},
45+
{text: 'Call', onPress: () => callPhone(phoneNumber, {prompt: false})},
4646
])
4747
}
4848

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
}

source/views/settings/sections/support.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,36 @@ import * as React from 'react'
33
import {Alert} from 'react-native'
44
import {Section} from 'react-native-tableview-simple'
55
import type {TopLevelViewPropsType} from '../../types'
6-
import {email} from 'react-native-communications'
6+
import {sendEmail} from '../../components/send-email'
77
import DeviceInfo from 'react-native-device-info'
88
import {version} from '../../../../package.json'
99
import {PushButtonCell} from '../../components/cells/push-button'
1010
import {refreshApp} from '../../../lib/refresh'
1111

1212
type Props = TopLevelViewPropsType
1313

14+
const getDeviceInfo = () => `
15+
16+
----- Please do not edit below here -----
17+
${DeviceInfo.getBrand()} ${DeviceInfo.getModel()}
18+
${DeviceInfo.getDeviceId()}
19+
${DeviceInfo.getSystemName()} ${version}
20+
${DeviceInfo.getReadableVersion()}
21+
`
22+
23+
const openEmail = () => {
24+
sendEmail({
25+
26+
subject: 'Support: All About Olaf',
27+
body: getDeviceInfo(),
28+
})
29+
}
30+
1431
export default class SupportSection extends React.PureComponent<Props> {
1532
onPressButton = (id: string) => {
1633
this.props.navigation.navigate(id)
1734
}
1835

19-
getDeviceInfo = () => `
20-
21-
----- Please do not edit below here -----
22-
${DeviceInfo.getBrand()} ${DeviceInfo.getModel()}
23-
${DeviceInfo.getDeviceId()}
24-
${DeviceInfo.getSystemName()} ${version}
25-
${DeviceInfo.getReadableVersion()}
26-
`
27-
28-
openEmail = () => {
29-
email(
30-
31-
null,
32-
null,
33-
'Support: All About Olaf',
34-
this.getDeviceInfo(),
35-
)
36-
}
37-
3836
onFaqButton = () => this.onPressButton('FaqView')
3937

4038
onResetButton = () => {
@@ -55,7 +53,7 @@ export default class SupportSection extends React.PureComponent<Props> {
5553
render() {
5654
return (
5755
<Section header="SUPPORT">
58-
<PushButtonCell onPress={this.openEmail} title="Contact Us" />
56+
<PushButtonCell onPress={openEmail} title="Contact Us" />
5957
<PushButtonCell onPress={this.onFaqButton} title="FAQs" />
6058
<PushButtonCell onPress={this.onResetButton} title="Reset Everything" />
6159
</Section>

source/views/sis/student-work/detail.android.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// @flow
22
import * as React from 'react'
33
import {Text, View, ScrollView, StyleSheet} from 'react-native'
4-
import {email} from 'react-native-communications'
4+
import {sendEmail} from '../../components/send-email'
55
import {Card} from '../../components/card'
66
import moment from 'moment'
7-
import openUrl from '../../components/open-url'
7+
import {openUrl} from '../../components/open-url'
88
import * as c from '../../components/colors'
99
import type {JobType} from './types'
1010
import {cleanJob, getContactName, getLinksFromJob} from './clean-job'
@@ -62,7 +62,9 @@ function Contact({job}: {job: JobType}) {
6262
return job.office || contactName ? (
6363
<Card header="Contact" style={styles.card}>
6464
<Text
65-
onPress={() => email([job.contactEmail], null, null, job.title, '')}
65+
onPress={() =>
66+
sendEmail({to: [job.contactEmail], subject: job.title, body: ''})
67+
}
6668
style={styles.cardBody}
6769
>
6870
{contactName} {job.title ? `(${job.title})` : ''}

source/views/sis/student-work/detail.ios.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// @flow
22
import * as React from 'react'
33
import {Text, ScrollView, StyleSheet} from 'react-native'
4-
import {email} from 'react-native-communications'
4+
import {sendEmail} from '../../components/send-email'
55
import {Cell, Section, TableView} from 'react-native-tableview-simple'
66
import moment from 'moment'
7-
import openUrl from '../../components/open-url'
7+
import {openUrl} from '../../components/open-url'
88
import * as c from '../../components/colors'
99
import type {JobType} from './types'
1010
import {cleanJob, getContactName, getLinksFromJob} from './clean-job'
@@ -39,12 +39,14 @@ function Information({job}: {job: JobType}) {
3939
accessory="DisclosureIndicator"
4040
cellStyle="LeftDetail"
4141
detail="Contact"
42-
onPress={() => email([job.contactEmail], null, null, job.title, '')}
42+
onPress={() =>
43+
sendEmail({to: [job.contactEmail], subject: job.title, body: ''})
44+
}
4345
title={getContactName(job).trim() || job.contactEmail}
4446
/>
4547
) : null
4648

47-
const ending = job.hoursPerWeek == 'Full-time' ? '' : ' hrs/week'
49+
const ending = job.hoursPerWeek === 'Full-time' ? '' : ' hrs/week'
4850
const hours = job.hoursPerWeek ? (
4951
<Cell
5052
cellStyle="LeftDetail"

source/views/streaming/radio/controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from 'react-native'
1212
import * as c from '../../components/colors'
1313
import {TabBarIcon} from '../../components/tabbar-icon'
14-
import {phonecall} from 'react-native-communications'
14+
import {callPhone} from '../../components/call-phone'
1515
import {Row} from '../../components/layout'
1616
import type {TopLevelViewPropsType} from '../../types'
1717
import {StreamPlayer} from './player'
@@ -84,7 +84,7 @@ export class KSTOView extends React.PureComponent<Props, State> {
8484
}
8585

8686
callStation = () => {
87-
phonecall(stationNumber, false)
87+
callPhone(stationNumber)
8888
}
8989

9090
renderPlayButton = (state: PlayState) => {

0 commit comments

Comments
 (0)