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

101 lines
3.0 KiB
TypeScript
Raw Normal View History

import { ComponentEmojis } from '@components/Emojis'
import { EmojisState } from '@components/Emojis/Context'
2021-05-09 21:59:03 +02:00
import { HeaderLeft, HeaderRight } from '@components/Header'
import ComponentInput 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, useRef, useState } from 'react'
2021-05-09 21:59:03 +02:00
import { useTranslation } from 'react-i18next'
2022-09-18 23:28:14 +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-02-12 14:51:01 +01:00
const TabMeProfileNote: React.FC<
TabMeProfileStackScreenProps<'Tab-Me-Profile-Note'> & {
messageRef: RefObject<FlashMessage>
}
> = ({
2021-05-09 21:59:03 +02:00
messageRef,
route: {
params: { note }
},
navigation
}) => {
2022-02-12 14:51:01 +01:00
const { theme } = useTheme()
2022-12-23 15:53:40 +01:00
const { t } = useTranslation(['common'])
2021-05-09 21:59:03 +02:00
const { mutateAsync, status } = useProfileMutation()
const [notes, setNotes] = useState(note)
2022-09-18 23:28:14 +02:00
const notesProps: NonNullable<EmojisState['inputProps'][0]> = {
value: [notes, setNotes],
selection: useState({ start: notes.length }),
isFocused: useRef<boolean>(false),
ref: useRef<TextInput>(null),
maxLength: 500
}
2021-05-10 17:41:32 +02:00
2021-05-09 21:59:03 +02:00
const [dirty, setDirty] = useState(false)
useEffect(() => {
setDirty(note !== notes)
}, [notes])
2021-05-09 21:59:03 +02:00
useEffect(() => {
navigation.setOptions({
headerLeft: () => (
<HeaderLeft
content='chevron-left'
2021-05-09 21:59:03 +02:00
onPress={() => {
if (dirty) {
2022-12-23 15:53:40 +01:00
Alert.alert(t('common:discard.title'), t('common:discard.message'), [
{
text: t('common:buttons.discard'),
style: 'destructive',
onPress: () => navigation.navigate('Tab-Me-Profile-Root')
},
{
text: t('common:buttons.cancel'),
style: 'default'
}
])
2021-05-09 21:59:03 +02:00
} else {
navigation.navigate('Tab-Me-Profile-Root')
}
}}
/>
),
headerRight: () => (
<HeaderRight
disabled={!dirty}
loading={status === 'loading'}
content='save'
2021-05-09 21:59:03 +02:00
onPress={async () => {
2021-05-26 23:30:15 +02:00
mutateAsync({
messageRef,
message: {
text: 'me.profile.root.note.title',
succeed: true,
failed: true
},
type: 'note',
data: notes
2021-05-26 23:30:15 +02:00
}).then(() => {
navigation.navigate('Tab-Me-Profile-Root')
})
2021-05-09 21:59:03 +02:00
}}
/>
)
})
2022-12-14 23:37:41 +01:00
}, [theme, dirty, status, notes])
2021-05-09 21:59:03 +02:00
return (
2022-09-23 00:21:41 +02:00
<ComponentEmojis inputProps={[notesProps]}>
2022-09-18 23:28:14 +02:00
<ScrollView style={{ paddingHorizontal: StyleConstants.Spacing.Global.PagePadding }}>
<ComponentInput {...notesProps} multiline />
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 TabMeProfileNote