skolplattformen-backup/apps/skolplattformen-sthlm/components/contactMenu.component.tsx

128 lines
3.5 KiB
TypeScript
Raw Normal View History

/* eslint-disable react-native-a11y/has-accessibility-hint */
2021-10-05 15:44:14 +00:00
import { Classmate } from '@skolplattformen/api-skolplattformen'
import {
Button,
MenuGroup,
MenuItem,
OverflowMenu,
} from '@ui-kitten/components'
import React from 'react'
import { Linking, StyleSheet } from 'react-native'
2021-02-20 08:38:08 +00:00
import { fullName } from '../utils/peopleHelpers'
2021-10-05 15:44:14 +00:00
import { translate } from '../utils/translation'
2021-02-20 08:38:08 +00:00
import {
CallIcon,
EmailIcon,
MapIcon,
MoreIcon,
SMSIcon,
} from './icon.component'
2020-12-16 15:39:22 +00:00
interface ContactMenuProps {
contact: Classmate
selected: boolean
setSelected: (value?: number | null) => void
}
export const ContactMenu = ({
contact,
selected,
setSelected,
}: ContactMenuProps) => {
const [visible, setVisible] = React.useState(selected)
2020-12-16 15:39:22 +00:00
const renderToggleButton = () => (
<Button
testID="ShowContactInfoButton"
accessibilityHint={translate(
'contact.a11y_show_contact_info_button_hint',
{
defaultValue: 'Visar kontaktinformation',
}
)}
accessibilityLabel={translate(
'contact.a11y_show_contact_info_button_label',
{
defaultValue: 'Visa kontaktinformation',
}
)}
onPress={() => setVisible(true)}
appearance="ghost"
2021-02-20 08:38:08 +00:00
accessoryLeft={MoreIcon}
/>
)
2020-12-16 15:39:22 +00:00
const handleBackdropPress = () => {
setVisible(false)
setSelected(null)
}
const shouldDisplay = (option?: string) => (option ? 'flex' : 'none')
2020-12-16 15:39:22 +00:00
return (
<OverflowMenu
visible={visible}
anchor={renderToggleButton}
backdropStyle={styles.backdrop}
onBackdropPress={handleBackdropPress}
>
2021-02-20 08:38:08 +00:00
{contact.guardians.map((guardian) => {
const { address, email, mobile } = guardian
return (
<MenuGroup
key={fullName(guardian)}
title={fullName(guardian)}
style={styles.group}
>
<MenuItem
testID="CallMenuItem"
accessibilityLabel={translate('contact.call')}
2021-02-20 08:38:08 +00:00
accessoryLeft={CallIcon}
style={{ display: shouldDisplay(mobile) }}
title={translate('contact.call')}
2021-02-20 08:38:08 +00:00
onPress={() => Linking.openURL(`tel:${mobile}`)}
/>
<MenuItem
testID="SMSMenuItem"
accessibilityLabel={translate('contact.sms')}
2021-02-20 08:38:08 +00:00
accessoryLeft={SMSIcon}
style={{ display: shouldDisplay(mobile) }}
title={translate('contact.sms')}
2021-02-20 08:38:08 +00:00
onPress={() => Linking.openURL(`sms:${mobile}`)}
/>
<MenuItem
testID="SendEmailMenuItem"
accessibilityLabel={translate('contact.email')}
2021-02-20 08:38:08 +00:00
accessoryLeft={EmailIcon}
style={{ display: shouldDisplay(email) }}
title={translate('contact.email')}
2021-02-20 08:38:08 +00:00
onPress={() => Linking.openURL(`mailto:${email}`)}
/>
<MenuItem
testID="ShowHomeMenuItem"
accessibilityLabel={translate('contact.home')}
2021-02-20 08:38:08 +00:00
accessoryLeft={MapIcon}
style={{ display: shouldDisplay(address) }}
title={translate('contact.home')}
2021-02-20 08:38:08 +00:00
onPress={() =>
Linking.openURL(`http://maps.apple.com/?daddr=${address}`)
}
/>
</MenuGroup>
)
})}
2020-12-16 15:39:22 +00:00
</OverflowMenu>
)
}
const styles = StyleSheet.create({
backdrop: {
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
2021-02-20 08:38:08 +00:00
group: {
position: 'relative',
zIndex: 10,
},
})