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

109 lines
3.1 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'
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, { RefObject, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Alert, StyleSheet } from 'react-native'
import FlashMessage from 'react-native-flash-message'
import { ScrollView } from 'react-native-gesture-handler'
2021-08-29 15:25:38 +02:00
const TabMeProfileName: React.FC<TabMeProfileStackScreenProps<
2021-05-09 21:59:03 +02:00
'Tab-Me-Profile-Name'
> & { messageRef: RefObject<FlashMessage> }> = ({
messageRef,
route: {
params: { display_name }
},
navigation
}) => {
const { mode } = useTheme()
const { t, i18n } = useTranslation('screenTabs')
const { mutateAsync, status } = useProfileMutation()
const [displayName, setDisplayName] = useState(display_name)
2021-05-10 17:41:32 +02:00
2021-05-09 21:59:03 +02:00
const [dirty, setDirty] = useState(false)
useEffect(() => {
setDirty(display_name !== displayName)
}, [displayName])
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 () => {
2021-05-26 23:30:15 +02:00
mutateAsync({
mode,
messageRef,
message: {
text: 'me.profile.root.name.title',
succeed: true,
failed: true
},
type: 'display_name',
data: displayName
}).then(() => {
navigation.navigate('Tab-Me-Profile-Root')
})
2021-05-09 21:59:03 +02:00
}}
/>
)
})
}, [mode, i18n.language, dirty, status, displayName])
return (
2021-05-11 21:38:48 +02:00
<ScrollView style={styles.base} keyboardShouldPersistTaps='always'>
2021-05-10 23:41:48 +02:00
<Input
value={displayName}
setValue={setDisplayName}
emoji
options={{
maxLength: 30,
autoCapitalize: 'none',
autoCompleteType: 'username',
autoCorrect: false
}}
/>
2021-05-09 21:59:03 +02:00
</ScrollView>
)
}
const styles = StyleSheet.create({
base: {
2021-05-10 23:41:48 +02:00
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding
2021-05-09 21:59:03 +02:00
}
})
2021-05-11 21:38:48 +02:00
export default TabMeProfileName