|
| 1 | +import { type NativeStackNavigationProp } from '@react-navigation/native-stack'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import { ScrollView } from 'react-native-gesture-handler'; |
| 4 | + |
| 5 | +import I18n from '../../i18n'; |
| 6 | +import SafeAreaView from '../../containers/SafeAreaView'; |
| 7 | +import * as List from '../../containers/List'; |
| 8 | +import { type ProfileStackParamList } from '../../stacks/types'; |
| 9 | +import { FormTextInput } from '../../containers/TextInput'; |
| 10 | +import Chip from '../../containers/Chip'; |
| 11 | +import { useUserPreferences } from '../../lib/methods/userPreferences'; |
| 12 | +import { WATCHOS_QUICKREPLIES } from '../../lib/constants/keys'; |
| 13 | + |
| 14 | +interface IUserPreferencesViewProps { |
| 15 | + navigation: NativeStackNavigationProp<ProfileStackParamList, 'UserPreferencesView'>; |
| 16 | +} |
| 17 | + |
| 18 | +const UserPreferencesView = ({ navigation }: IUserPreferencesViewProps): JSX.Element => { |
| 19 | + const [quickreplies, setQuickreplies] = useUserPreferences<string[]>(WATCHOS_QUICKREPLIES, []); |
| 20 | + const [input, setInput] = useState<string>(''); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + navigation.setOptions({ |
| 24 | + title: I18n.t('Preferences') |
| 25 | + }); |
| 26 | + }, [navigation]); |
| 27 | + |
| 28 | + const removeQuickReply = (reply: string) => { |
| 29 | + const newReplies = quickreplies?.filter(quickreply => quickreply !== reply); |
| 30 | + setQuickreplies(newReplies); |
| 31 | + }; |
| 32 | + |
| 33 | + const addQuickReply = () => { |
| 34 | + const value = input.trim(); |
| 35 | + if (!value) return; |
| 36 | + if (!quickreplies?.includes(input.trim())) setQuickreplies([...(quickreplies ?? []), value]); |
| 37 | + setInput(''); |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <SafeAreaView testID='preferences-view'> |
| 42 | + <List.Container> |
| 43 | + <List.Section title='WatchOS_Quick_Replies'> |
| 44 | + <> |
| 45 | + {quickreplies && quickreplies.length !== 0 && ( |
| 46 | + <ScrollView horizontal style={{ marginVertical: 8, paddingHorizontal: 4 }}> |
| 47 | + {quickreplies.map((reply, index) => ( |
| 48 | + <Chip key={index} text={reply} onPress={() => removeQuickReply(reply)} /> |
| 49 | + ))} |
| 50 | + </ScrollView> |
| 51 | + )} |
| 52 | + </> |
| 53 | + <List.Separator /> |
| 54 | + <FormTextInput |
| 55 | + value={input} |
| 56 | + onChangeText={text => setInput(text)} |
| 57 | + placeholder={I18n.t('Add_Quick_Reply')} |
| 58 | + onSubmitEditing={addQuickReply} |
| 59 | + /> |
| 60 | + <List.Separator /> |
| 61 | + <List.Info info='WatchOS_Quick_Replies_Description' /> |
| 62 | + </List.Section> |
| 63 | + </List.Container> |
| 64 | + </SafeAreaView> |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +export default UserPreferencesView; |
0 commit comments