/
Profile.tsx
167 lines (155 loc) · 5.49 KB
/
Profile.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { NativeStackScreenProps } from '@react-navigation/native-stack'
import React, { useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { StyleSheet, Text, View } from 'react-native'
import * as RNFS from 'react-native-fs'
import { SafeAreaView } from 'react-native-safe-area-context'
import { saveNameAndPicture } from 'src/account/actions'
import { nameSelector, pictureSelector } from 'src/account/selectors'
import { showError, showMessage } from 'src/alert/actions'
import { SettingsEvents } from 'src/analytics/Events'
import ValoraAnalytics from 'src/analytics/ValoraAnalytics'
import { ErrorMessages } from 'src/app/ErrorMessages'
import Button from 'src/components/Button'
import CancelButton from 'src/components/CancelButton'
import InLineNotification, { Severity } from 'src/components/InLineNotification'
import KeyboardAwareScrollView from 'src/components/KeyboardAwareScrollView'
import KeyboardSpacer from 'src/components/KeyboardSpacer'
import TextInput from 'src/components/TextInput'
import Touchable from 'src/components/Touchable'
import i18n from 'src/i18n'
import { generateRandomUsername } from 'src/nameGenerator'
import { emptyHeader } from 'src/navigator/Headers'
import { Screens } from 'src/navigator/Screens'
import { StackParamList } from 'src/navigator/types'
import PictureInput from 'src/onboarding/registration/PictureInput'
import { useDispatch, useSelector } from 'src/redux/hooks'
import colors from 'src/styles/colors'
import fontStyles from 'src/styles/fonts'
import { Spacing } from 'src/styles/styles'
import Logger from 'src/utils/Logger'
import { saveProfilePicture } from 'src/utils/image'
type Props = NativeStackScreenProps<StackParamList, Screens.Profile>
function Profile({ navigation, route }: Props) {
const { t } = useTranslation()
const [newName, setNewName] = useState(useSelector(nameSelector) ?? '')
const picturePath = useSelector(pictureSelector)
const [newPictureUri, setNewPictureUri] = useState(picturePath || null)
const exampleName = useMemo(() => generateRandomUsername(), [])
const dispatch = useDispatch()
const onSave = () => {
if (newName.length === 0) {
dispatch(showError(ErrorMessages.MISSING_FULL_NAME))
return
}
ValoraAnalytics.track(SettingsEvents.profile_save)
dispatch(saveNameAndPicture(newName, newPictureUri))
dispatch(showMessage(t('namePictureSaved')))
navigation.goBack()
// Delete old profile pictures if necessary.
if (picturePath && picturePath !== newPictureUri) {
RNFS.unlink(picturePath).catch((e) => {
Logger.error('Profile', 'Error deleting old profile picture:', e)
})
}
}
const onPictureChosen = async (pictureDataUrl: string | null) => {
if (!pictureDataUrl) {
setNewPictureUri(null)
ValoraAnalytics.track(SettingsEvents.profile_photo_removed)
} else {
try {
const newPicturePath = await saveProfilePicture(pictureDataUrl)
setNewPictureUri(newPicturePath)
ValoraAnalytics.track(SettingsEvents.profile_photo_chosen)
} catch (error) {
dispatch(showError(ErrorMessages.PICTURE_LOAD_FAILED))
}
}
}
const updateName = (updatedName: string) => {
setNewName(updatedName)
}
const generateName = () => {
ValoraAnalytics.track(SettingsEvents.profile_generate_name)
updateName(generateRandomUsername())
}
return (
<SafeAreaView style={styles.container} edges={['bottom']}>
<KeyboardAwareScrollView testID="ProfileScrollView">
<View style={styles.accountProfile}>
<PictureInput picture={newPictureUri} onPhotoChosen={onPictureChosen} />
</View>
<View style={styles.inputContainer}>
<TextInput
placeholder={t('profileScreen.namePlaceholder', { exampleName }) ?? undefined}
testID="ProfileEditName"
onChangeText={updateName}
value={newName ?? t('unknown')}
/>
</View>
<View style={styles.ctaContainer}>
<Button
style={styles.generateButton}
text={t('profileScreen.generateName')}
onPress={generateName}
testID="GenerateNameButton"
/>
<Touchable testID="SaveButton" onPress={onSave}>
<Text style={styles.saveButton}>{t('save')}</Text>
</Touchable>
</View>
<View style={styles.disclaimerContainer}>
<InLineNotification
severity={Severity.Informational}
description={t('profileScreen.profilePictureDisclaimer')}
/>
</View>
</KeyboardAwareScrollView>
<KeyboardSpacer />
</SafeAreaView>
)
}
Profile.navigationOptions = ({ navigation }: Props) => {
const onCancel = () => {
ValoraAnalytics.track(SettingsEvents.profile_cancel)
navigation.goBack()
}
return {
...emptyHeader,
headerTitle: i18n.t('editProfile'),
headerLeft: () => <CancelButton onCancel={onCancel} />,
}
}
export default Profile
const styles = StyleSheet.create({
container: {
flex: 1,
},
accountProfile: {
paddingLeft: 10,
paddingTop: 24,
paddingRight: 15,
paddingBottom: 37,
flexDirection: 'column',
alignItems: 'center',
},
inputContainer: {
borderBottomWidth: 1,
borderBottomColor: colors.gray2,
marginHorizontal: 24,
},
ctaContainer: {
alignItems: 'center',
},
generateButton: {
marginVertical: 24,
},
saveButton: {
...fontStyles.regular,
color: colors.primary,
},
disclaimerContainer: {
margin: Spacing.Thick24,
},
})