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

159 lines
4.8 KiB
TypeScript
Raw Normal View History

2021-05-09 21:59:03 +02:00
import { HeaderLeft, HeaderRight } from '@components/Header'
import Input 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 { isEqual } from 'lodash'
import React, { RefObject, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Alert, View } from 'react-native'
2021-05-09 21:59:03 +02:00
import FlashMessage from 'react-native-flash-message'
import { ScrollView } from 'react-native-gesture-handler'
const prepareFields = (
fields: Mastodon.Field[] | undefined
): Mastodon.Field[] => {
return Array.from(Array(4).keys()).map(index => {
if (fields && fields[index]) {
return fields[index]
} else {
return { name: '', value: '', verified_at: null }
}
})
}
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-02-12 14:51:01 +01:00
const { colors, theme } = useTheme()
2021-05-09 21:59:03 +02:00
const { t, i18n } = useTranslation('screenTabs')
const { mutateAsync, status } = useProfileMutation()
const [newFields, setNewFields] = useState(prepareFields(fields))
const [dirty, setDirty] = useState(false)
useEffect(() => {
setDirty(!isEqual(prepareFields(fields), newFields))
}, [newFields])
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: newFields
.filter(field => field.name.length && field.value.length)
.map(field => ({ name: field.name, value: field.value }))
2021-05-26 23:30:15 +02:00
}).then(() => {
navigation.navigate('Tab-Me-Profile-Root')
2021-05-09 21:59:03 +02:00
})
}}
/>
)
})
2022-02-12 14:51:01 +01:00
}, [theme, i18n.language, dirty, status, newFields])
2021-05-09 21:59:03 +02:00
return (
<ScrollView
style={{
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding,
marginBottom: StyleConstants.Spacing.L
}}
keyboardShouldPersistTaps='always'
>
2021-05-10 23:41:48 +02:00
<View style={{ marginBottom: StyleConstants.Spacing.L * 2 }}>
{Array.from(Array(4).keys()).map(index => (
<View key={index} style={{ marginBottom: StyleConstants.Spacing.M }}>
<CustomText
fontStyle='S'
style={{
marginBottom: StyleConstants.Spacing.XS,
color: colors.primaryDefault
}}
>
2021-05-10 23:41:48 +02:00
{t('me.profile.fields.group', { index: index + 1 })}
</CustomText>
2021-05-10 23:41:48 +02:00
<Input
title={t('me.profile.fields.label')}
autoFocus={false}
options={{ maxLength: 255 }}
value={newFields[index].name}
setValue={(v: any) =>
setNewFields(
newFields.map((field, i) =>
i === index ? { ...field, name: v } : field
)
2021-05-09 21:59:03 +02:00
)
2021-05-10 23:41:48 +02:00
}
emoji
/>
<Input
title={t('me.profile.fields.content')}
autoFocus={false}
options={{ maxLength: 255 }}
value={newFields[index].value}
setValue={(v: any) =>
setNewFields(
newFields.map((field, i) =>
i === index ? { ...field, value: v } : field
)
2021-05-09 21:59:03 +02:00
)
2021-05-10 23:41:48 +02:00
}
emoji
/>
</View>
))}
</View>
2021-05-09 21:59:03 +02:00
</ScrollView>
)
}
2021-05-11 21:38:48 +02:00
export default TabMeProfileFields