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

272 lines
8.5 KiB
TypeScript
Raw Normal View History

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'
import CustomText from '@components/Text'
2021-01-13 01:03:46 +01:00
import { useActionSheet } from '@expo/react-native-action-sheet'
2021-11-15 22:34:43 +01:00
import { getInstanceConfigurationPoll } from '@utils/slices/instancesSlice'
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'
import { StyleSheet, TextInput, View } from 'react-native'
2021-11-15 22:34:43 +01:00
import { useSelector } from 'react-redux'
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-03-28 23:31:10 +02:00
const { t } = useTranslation('screenCompose')
2022-02-12 14:51:01 +01:00
const { colors, mode } = useTheme()
2020-12-03 22:03:06 +01:00
2021-11-15 22:34:43 +01:00
const instanceConfigurationPoll = useSelector(
getInstanceConfigurationPoll,
() => true
)
const MAX_OPTIONS = instanceConfigurationPoll.max_options
const MAX_CHARS_PER_OPTION =
instanceConfigurationPoll.max_characters_per_option
const MIN_EXPIRATION = instanceConfigurationPoll.min_expiration
const MAX_EXPIRATION = instanceConfigurationPoll.max_expiration
2020-12-03 22:03:06 +01:00
const [firstRender, setFirstRender] = useState(true)
useEffect(() => {
setFirstRender(false)
}, [])
return (
<View
style={{
flex: 1,
borderWidth: StyleSheet.hairlineWidth,
borderRadius: 6,
margin: StyleConstants.Spacing.Global.PagePadding,
borderColor: colors.border
}}
>
<View
style={{
marginTop: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S
}}
>
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}
2022-02-12 14:51:01 +01:00
color={colors.secondary}
2020-12-06 22:32:36 +01:00
/>
<TextInput
2021-04-09 21:43:12 +02:00
accessibilityLabel={t(
'content.root.footer.poll.option.placeholder.accessibilityLabel',
{ index: i + 1 }
)}
keyboardAppearance={mode}
2020-12-06 22:32:36 +01:00
{...(i === 0 && firstRender && { autoFocus: true })}
style={{
flex: 1,
padding: StyleConstants.Spacing.S,
borderWidth: StyleSheet.hairlineWidth,
borderRadius: 6,
...StyleConstants.FontStyle.M,
marginLeft: StyleConstants.Spacing.S,
borderColor: colors.border,
color: hasConflict ? colors.red : colors.primaryDefault
}}
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')
}
2022-02-12 14:51:01 +01:00
placeholderTextColor={colors.disabled}
2021-11-15 22:34:43 +01:00
maxLength={MAX_CHARS_PER_OPTION}
2020-12-06 22:32:36 +01:00
// @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={{
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-end',
marginRight: StyleConstants.Spacing.M
}}
>
2020-12-26 23:05:17 +01:00
<Button
2021-11-15 22:34:43 +01:00
{...(total > 2
? {
accessibilityLabel: t(
'content.root.footer.poll.quantity.reduce.accessibilityLabel',
{ amount: total - 1 }
)
}
: {
accessibilityHint: t(
'content.root.footer.poll.quantity.reduce.accessibilityHint',
{ amount: total }
)
})}
onPress={() => {
total > 2 &&
composeDispatch({
type: 'poll',
payload: { total: total - 1 }
})
}}
type='icon'
content='Minus'
round
disabled={!(total > 2)}
/>
<CustomText
style={{
marginHorizontal: StyleConstants.Spacing.S,
color: colors.secondary
}}
>
2021-11-15 22:34:43 +01:00
{total} / {MAX_OPTIONS}
</CustomText>
2021-11-15 22:34:43 +01:00
<Button
{...(total < MAX_OPTIONS
2021-04-09 21:43:12 +02:00
? {
accessibilityLabel: t(
'content.root.footer.poll.quantity.increase.accessibilityLabel',
{ amount: total + 1 }
)
}
: {
accessibilityHint: t(
'content.root.footer.poll.quantity.increase.accessibilityHint',
{ amount: total }
)
})}
onPress={() => {
2021-11-15 22:34:43 +01:00
total < MAX_OPTIONS &&
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
2021-11-15 22:34:43 +01:00
disabled={!(total < MAX_OPTIONS)}
2020-12-03 22:03:06 +01:00
/>
</View>
<View
style={{ paddingHorizontal: StyleConstants.Spacing.Global.PagePadding }}
>
2021-05-09 23:16:53 +02:00
<MenuRow
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')
}
onPress={() =>
showActionSheetWithOptions(
{
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')
],
2022-02-12 14:51:01 +01:00
cancelButtonIndex: 2,
userInterfaceStyle: mode
2021-05-09 23:16:53 +02:00
},
index => {
2021-11-15 22:34:43 +01:00
if (index && index < 2) {
2021-05-09 23:16:53 +02:00
composeDispatch({
type: 'poll',
payload: { multiple: index === 1 }
})
}
2021-01-24 02:25:43 +01:00
}
2021-05-09 23:16:53 +02:00
)
}
iconBack='ChevronRight'
/>
<MenuRow
title={t('content.root.footer.poll.expiration.heading')}
content={t(`content.root.footer.poll.expiration.options.${expire}`)}
onPress={() => {
2022-02-02 22:30:06 +01:00
const expirations = [
2021-11-15 22:34:43 +01:00
'300',
'1800',
'3600',
'21600',
'86400',
'259200',
'604800'
].filter(
expiration =>
parseInt(expiration) >= MIN_EXPIRATION &&
parseInt(expiration) <= MAX_EXPIRATION
)
2021-05-09 23:16:53 +02:00
showActionSheetWithOptions(
{
options: [
...expirations.map(e =>
t(`content.root.footer.poll.expiration.options.${e}`)
),
t('content.root.footer.poll.expiration.options.cancel')
],
2022-02-12 14:51:01 +01:00
cancelButtonIndex: expirations.length,
userInterfaceStyle: mode
2021-05-09 23:16:53 +02:00
},
index => {
2022-05-01 00:18:18 +02:00
if (index !== undefined && index < expirations.length) {
2021-05-09 23:16:53 +02:00
composeDispatch({
type: 'poll',
2022-02-02 22:30:06 +01:00
// @ts-ignore
2021-05-09 23:16:53 +02:00
payload: { expire: expirations[index] }
})
}
2021-01-24 02:25:43 +01:00
}
2021-05-09 23:16:53 +02:00
)
}}
iconBack='ChevronRight'
/>
</View>
2020-12-03 22:03:06 +01:00
</View>
)
}
const styles = StyleSheet.create({
option: {
marginLeft: StyleConstants.Spacing.M,
marginRight: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S,
flexDirection: 'row',
alignItems: 'center'
}
})
export default ComposePoll