tooot/src/screens/Tabs/Me/Profile/Fields.tsx

167 lines
5.0 KiB
TypeScript
Raw Normal View History

import { ComponentEmojis } from '@components/Emojis'
import { EmojisState } from '@components/Emojis/helpers/EmojisContext'
2021-05-09 21:59:03 +02:00
import { HeaderLeft, HeaderRight } from '@components/Header'
import ComponentInput from '@components/Input'
import CustomText from '@components/Text'
2021-08-29 15:25:38 +02:00
import { TabMeProfileStackScreenProps } from '@utils/navigation/navigators'
2021-05-09 21:59:03 +02:00
import { useProfileMutation } from '@utils/queryHooks/profile'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import React, { Dispatch, RefObject, SetStateAction, useEffect, useRef, useState } from 'react'
2021-05-09 21:59:03 +02:00
import { useTranslation } from 'react-i18next'
2022-09-23 00:21:41 +02:00
import { Alert, ScrollView, TextInput } from 'react-native'
2021-05-09 21:59:03 +02:00
import FlashMessage from 'react-native-flash-message'
const Field: React.FC<{
allProps: EmojisState['inputProps']
setDirty: Dispatch<SetStateAction<boolean>>
index: number
field?: Mastodon.Field
}> = ({ allProps, setDirty, index, field }) => {
const { colors } = useTheme()
const { t } = useTranslation('screenTabs')
2022-09-23 00:21:41 +02:00
const nameRef = useRef<TextInput>(null)
const valueRef = useRef<TextInput>(null)
const [name, setName] = useState(field?.name || '')
const [value, setValue] = useState(field?.value || '')
allProps[index * 2] = {
2022-09-18 23:28:14 +02:00
value: [name, setName],
selection: useState({ start: name.length }),
isFocused: useRef<boolean>(false),
2022-09-23 00:21:41 +02:00
maxLength: 255,
ref: nameRef
}
allProps[index * 2 + 1] = {
2022-09-18 23:28:14 +02:00
value: [value, setValue],
selection: useState({ start: value.length }),
isFocused: useRef<boolean>(false),
2022-09-23 00:21:41 +02:00
maxLength: 255,
ref: valueRef
}
useEffect(() => {
setDirty(dirty =>
2022-09-18 23:28:14 +02:00
dirty ? dirty : (field?.name || '') !== name || (field?.value || '') !== value
)
}, [name, value])
return (
<>
<CustomText
fontStyle='S'
style={{
marginBottom: StyleConstants.Spacing.XS,
color: colors.primaryDefault
}}
>
{t('me.profile.fields.group', { index: index + 1 })}
</CustomText>
<ComponentInput title={t('me.profile.fields.label')} {...allProps[index * 2]} />
<ComponentInput
title={t('me.profile.fields.content')}
{...allProps[index * 2 + 1]}
multiline
/>
</>
)
2021-05-09 21:59:03 +02:00
}
2022-02-12 14:51:01 +01:00
const TabMeProfileFields: React.FC<
TabMeProfileStackScreenProps<'Tab-Me-Profile-Fields'> & {
messageRef: RefObject<FlashMessage>
}
> = ({
2021-05-09 21:59:03 +02:00
messageRef,
route: {
params: { fields }
},
navigation
}) => {
const { theme } = useTheme()
2021-05-09 21:59:03 +02:00
const { t, i18n } = useTranslation('screenTabs')
const { mutateAsync, status } = useProfileMutation()
const allProps: EmojisState['inputProps'] = []
2021-05-09 21:59:03 +02:00
const [dirty, setDirty] = useState(false)
useEffect(() => {
navigation.setOptions({
headerLeft: () => (
<HeaderLeft
onPress={() => {
if (dirty) {
Alert.alert(
t('me.profile.cancellation.title'),
t('me.profile.cancellation.message'),
[
{
text: t('me.profile.cancellation.buttons.cancel'),
style: 'default'
},
{
text: t('me.profile.cancellation.buttons.discard'),
style: 'destructive',
onPress: () => navigation.navigate('Tab-Me-Profile-Root')
}
]
)
} else {
navigation.navigate('Tab-Me-Profile-Root')
}
}}
/>
),
headerRight: () => (
<HeaderRight
disabled={!dirty}
loading={status === 'loading'}
content='Save'
onPress={async () => {
mutateAsync({
2022-02-12 14:51:01 +01:00
theme,
2021-05-26 23:30:15 +02:00
messageRef,
message: {
text: 'me.profile.root.note.title',
succeed: true,
failed: true
},
2021-05-09 21:59:03 +02:00
type: 'fields_attributes',
data: Array.from(Array(4).keys())
.filter(
index =>
2022-09-18 23:28:14 +02:00
allProps[index * 2]?.value[0].length || allProps[index * 2 + 1]?.value[0].length
)
.map(index => ({
2022-09-18 23:28:14 +02:00
name: allProps[index * 2].value[0],
value: allProps[index * 2 + 1].value[0]
}))
2021-05-26 23:30:15 +02:00
}).then(() => {
navigation.navigate('Tab-Me-Profile-Root')
2021-05-09 21:59:03 +02:00
})
}}
/>
)
})
}, [theme, i18n.language, dirty, status, allProps.map(p => p.value)])
2021-05-09 21:59:03 +02:00
return (
<ComponentEmojis inputProps={allProps}>
2022-09-18 23:28:14 +02:00
<ScrollView style={{ paddingHorizontal: StyleConstants.Spacing.Global.PagePadding }}>
2021-05-10 23:41:48 +02:00
{Array.from(Array(4).keys()).map(index => (
<Field
key={index}
allProps={allProps}
setDirty={setDirty}
index={index}
field={fields?.[index]}
/>
2021-05-10 23:41:48 +02:00
))}
2022-09-18 23:28:14 +02:00
</ScrollView>
</ComponentEmojis>
2021-05-09 21:59:03 +02:00
)
}
2021-05-11 21:38:48 +02:00
export default TabMeProfileFields