tooot/src/screens/Compose/Root/Footer/Poll.tsx

226 lines
6.7 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
2020-12-26 23:05:17 +01:00
import Button from '@components/Button'
import Icon from '@components/Icon'
2020-12-26 23:05:17 +01:00
import { MenuRow } from '@components/Menu'
2021-01-13 01:03:46 +01:00
import { useActionSheet } from '@expo/react-native-action-sheet'
2021-01-01 17:52:14 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import React, { useContext, useEffect, useState } from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
2021-01-13 01:03:46 +01:00
import { StyleSheet, TextInput, View } from 'react-native'
2021-01-19 01:13:45 +01:00
import ComposeContext from '../../utils/createContext'
2020-12-03 22:03:06 +01:00
const ComposePoll: React.FC = () => {
2021-01-13 01:03:46 +01:00
const { showActionSheetWithOptions } = useActionSheet()
const {
composeState: {
poll: { total, options, multiple, expire }
},
composeDispatch
} = useContext(ComposeContext)
2021-01-19 01:13:45 +01:00
const { t } = useTranslation('sharedCompose')
const { mode, theme } = useTheme()
2020-12-03 22:03:06 +01:00
const [firstRender, setFirstRender] = useState(true)
useEffect(() => {
setFirstRender(false)
}, [])
return (
<View style={[styles.base, { borderColor: theme.border }]}>
<View style={styles.options}>
2020-12-10 19:19:56 +01:00
{[...Array(total)].map((e, i) => {
const restOptions = Object.keys(options).filter(
o => parseInt(o) !== i && parseInt(o) < total
2020-12-06 22:32:36 +01:00
)
let hasConflict = false
restOptions.forEach(o => {
// @ts-ignore
2020-12-10 19:19:56 +01:00
if (options[o] === options[i]) {
2020-12-06 22:32:36 +01:00
hasConflict = true
}
})
return (
<View key={i} style={styles.option}>
<Icon
name={multiple ? 'Square' : 'Circle'}
2020-12-06 22:32:36 +01:00
size={StyleConstants.Font.Size.L}
color={theme.secondary}
/>
<TextInput
keyboardAppearance={mode}
2020-12-06 22:32:36 +01:00
{...(i === 0 && firstRender && { autoFocus: true })}
style={[
styles.textInput,
{
borderColor: theme.border,
color: hasConflict ? theme.red : theme.primaryDefault
2020-12-03 22:03:06 +01:00
}
2020-12-06 22:32:36 +01:00
]}
2021-01-19 01:13:45 +01:00
placeholder={
multiple
? t('content.root.footer.poll.option.placeholder.multiple')
: t('content.root.footer.poll.option.placeholder.single')
}
2020-12-06 22:32:36 +01:00
placeholderTextColor={theme.secondary}
maxLength={50}
// @ts-ignore
2020-12-10 19:19:56 +01:00
value={options[i]}
2020-12-06 22:32:36 +01:00
onChangeText={e =>
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-06 22:32:36 +01:00
type: 'poll',
2020-12-10 19:19:56 +01:00
payload: { options: { ...options, [i]: e } }
2020-12-06 22:32:36 +01:00
})
}
/>
</View>
)
})}
2020-12-03 22:03:06 +01:00
</View>
<View style={styles.controlAmount}>
<View style={styles.firstButton}>
2020-12-26 23:05:17 +01:00
<Button
onPress={() => {
2021-01-24 02:25:43 +01:00
analytics('compose_poll_reduce_press')
total > 2 &&
composeDispatch({
type: 'poll',
payload: { total: total - 1 }
})
}}
2020-12-26 23:05:17 +01:00
type='icon'
content='Minus'
2020-12-26 23:05:17 +01:00
round
2020-12-10 19:19:56 +01:00
disabled={!(total > 2)}
2020-12-03 22:03:06 +01:00
/>
</View>
2020-12-26 23:05:17 +01:00
<Button
onPress={() => {
2021-01-24 02:25:43 +01:00
analytics('compose_poll_increase_press')
total < 4 &&
composeDispatch({
type: 'poll',
payload: { total: total + 1 }
})
}}
2020-12-26 23:05:17 +01:00
type='icon'
content='Plus'
2020-12-26 23:05:17 +01:00
round
2020-12-10 19:19:56 +01:00
disabled={!(total < 4)}
2020-12-03 22:03:06 +01:00
/>
</View>
2020-12-26 23:05:17 +01:00
<MenuRow
2021-01-19 01:13:45 +01:00
title={t('content.root.footer.poll.multiple.heading')}
content={
multiple
? t('content.root.footer.poll.multiple.options.multiple')
: t('content.root.footer.poll.multiple.options.single')
}
2020-12-26 23:05:17 +01:00
onPress={() =>
2021-01-13 01:03:46 +01:00
showActionSheetWithOptions(
2020-12-26 23:05:17 +01:00
{
2021-01-19 01:13:45 +01:00
options: [
t('content.root.footer.poll.multiple.options.single'),
t('content.root.footer.poll.multiple.options.multiple'),
t('content.root.footer.poll.multiple.options.cancel')
],
2020-12-26 23:05:17 +01:00
cancelButtonIndex: 2
},
2021-01-24 02:25:43 +01:00
index => {
if (index < 2) {
analytics('compose_poll_expiration_press', {
current: multiple,
new: index === 1
})
composeDispatch({
type: 'poll',
payload: { multiple: index === 1 }
})
}
}
2020-12-26 23:05:17 +01:00
)
}
iconBack='ChevronRight'
2020-12-26 23:05:17 +01:00
/>
<MenuRow
2021-01-19 01:13:45 +01:00
title={t('content.root.footer.poll.expiration.heading')}
content={t(`content.root.footer.poll.expiration.options.${expire}`)}
onPress={() => {
const expirations: [
'300',
'1800',
'3600',
'21600',
'86400',
'259200',
'604800'
] = ['300', '1800', '3600', '21600', '86400', '259200', '604800']
2021-01-13 01:03:46 +01:00
showActionSheetWithOptions(
2020-12-26 23:05:17 +01:00
{
2021-01-19 01:13:45 +01:00
options: [
...expirations.map(e =>
t(`content.root.footer.poll.expiration.options.${e}`)
),
t('content.root.footer.poll.expiration.options.cancel')
],
2020-12-26 23:05:17 +01:00
cancelButtonIndex: 7
},
2021-01-24 02:25:43 +01:00
index => {
if (index < 7) {
analytics('compose_poll_expiration_press', {
current: expire,
new: expirations[index]
})
composeDispatch({
type: 'poll',
payload: { expire: expirations[index] }
})
}
}
2020-12-26 23:05:17 +01:00
)
2021-01-19 01:13:45 +01:00
}}
iconBack='ChevronRight'
2020-12-26 23:05:17 +01:00
/>
2020-12-03 22:03:06 +01:00
</View>
)
}
const styles = StyleSheet.create({
base: {
flex: 1,
borderWidth: StyleSheet.hairlineWidth,
2020-12-30 00:56:25 +01:00
borderRadius: 6,
margin: StyleConstants.Spacing.Global.PagePadding
2020-12-03 22:03:06 +01:00
},
options: {
marginTop: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S
},
option: {
marginLeft: StyleConstants.Spacing.M,
marginRight: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S,
flexDirection: 'row',
alignItems: 'center'
},
textInput: {
flex: 1,
padding: StyleConstants.Spacing.S,
borderWidth: StyleSheet.hairlineWidth,
borderRadius: 6,
...StyleConstants.FontStyle.M,
2020-12-03 22:03:06 +01:00
marginLeft: StyleConstants.Spacing.S
},
controlAmount: {
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
2020-12-26 23:05:17 +01:00
marginRight: StyleConstants.Spacing.M
2020-12-03 22:03:06 +01:00
},
firstButton: {
marginRight: StyleConstants.Spacing.S
}
})
export default ComposePoll