2022-09-18 01:02:25 +02:00
|
|
|
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'
|
2022-09-18 01:02:25 +02:00
|
|
|
import ComponentInput from '@components/Input'
|
2022-05-07 00:52:32 +02:00
|
|
|
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'
|
2022-09-18 01:02:25 +02:00
|
|
|
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'
|
|
|
|
|
2022-09-18 01:02:25 +02:00
|
|
|
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)
|
2022-09-18 01:02:25 +02:00
|
|
|
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
|
2022-09-18 01:02:25 +02:00
|
|
|
}
|
|
|
|
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
|
2022-09-18 01:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setDirty(dirty =>
|
2022-09-18 23:28:14 +02:00
|
|
|
dirty ? dirty : (field?.name || '') !== name || (field?.value || '') !== value
|
2022-09-18 01:02:25 +02:00
|
|
|
)
|
|
|
|
}, [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
|
|
|
|
}) => {
|
2022-09-18 01:02:25 +02:00
|
|
|
const { theme } = useTheme()
|
2021-05-09 21:59:03 +02:00
|
|
|
const { t, i18n } = useTranslation('screenTabs')
|
|
|
|
const { mutateAsync, status } = useProfileMutation()
|
|
|
|
|
2022-09-18 01:02:25 +02:00
|
|
|
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',
|
2022-09-18 01:02:25 +02:00
|
|
|
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
|
2022-09-18 01:02:25 +02:00
|
|
|
)
|
|
|
|
.map(index => ({
|
2022-09-18 23:28:14 +02:00
|
|
|
name: allProps[index * 2].value[0],
|
|
|
|
value: allProps[index * 2 + 1].value[0]
|
2022-09-18 01:02:25 +02:00
|
|
|
}))
|
2021-05-26 23:30:15 +02:00
|
|
|
}).then(() => {
|
|
|
|
navigation.navigate('Tab-Me-Profile-Root')
|
2021-05-09 21:59:03 +02:00
|
|
|
})
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})
|
2022-09-18 01:02:25 +02:00
|
|
|
}, [theme, i18n.language, dirty, status, allProps.map(p => p.value)])
|
2021-05-09 21:59:03 +02:00
|
|
|
|
|
|
|
return (
|
2022-09-18 01:02:25 +02:00
|
|
|
<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 => (
|
2022-09-18 01:02:25 +02:00
|
|
|
<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>
|
2022-09-18 01:02:25 +02:00
|
|
|
</ComponentEmojis>
|
2021-05-09 21:59:03 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-11 21:38:48 +02:00
|
|
|
export default TabMeProfileFields
|