aboutsummaryrefslogtreecommitdiff
path: root/src/screens/profile/CaptionScreen.tsx
blob: 146ad86cbcc9d7e763e69a454ada22200c979641 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {RouteProp} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {Fragment, useEffect, useState} from 'react';
import {
  Alert,
  Image,
  Keyboard,
  KeyboardAvoidingView,
  Platform,
  StyleSheet,
  Text,
  TouchableOpacity,
  TouchableWithoutFeedback,
  View,
} from 'react-native';
import {MentionInput} from 'react-native-controlled-mentions';
import {Button, normalize} from 'react-native-elements';
import {useDispatch, useSelector} from 'react-redux';
import {ProfilePreview, SearchBackground} from '../../components';
import {CaptionScreenHeader} from '../../components/';
import TaggLoadingIndicator from '../../components/common/TaggLoadingIndicator';
import {TAGG_LIGHT_BLUE_2} from '../../constants';
import {ERROR_UPLOAD, SUCCESS_PIC_UPLOAD} from '../../constants/strings';
import {MainStackParams} from '../../routes';
import {postMoment} from '../../services';
import {
  loadUserMoments,
  updateProfileCompletionStage,
} from '../../store/actions';
import {RootState} from '../../store/rootReducer';
import {ProfilePreviewType, ScreenType} from '../../types';
import {SCREEN_WIDTH, StatusBarHeight} from '../../utils';
import {mentionPartTypes} from '../../utils/comments';

/**
 * Upload Screen to allow users to upload posts to Tagg
 */
type CaptionScreenRouteProp = RouteProp<MainStackParams, 'CaptionScreen'>;
type CaptionScreenNavigationProp = StackNavigationProp<
  MainStackParams,
  'CaptionScreen'
>;
interface CaptionScreenProps {
  route: CaptionScreenRouteProp;
  navigation: CaptionScreenNavigationProp;
}

const CaptionScreen: React.FC<CaptionScreenProps> = ({route, navigation}) => {
  const {title, image, screenType, selectedUsers} = route.params;
  const {
    user: {userId},
  } = useSelector((state: RootState) => state.user);
  const dispatch = useDispatch();
  const [caption, setCaption] = useState('');
  const [loading, setLoading] = useState(false);
  const [taggedUsers, setTaggedUsers] = useState<ProfilePreviewType[]>([]);

  useEffect(() => {
    setTaggedUsers(selectedUsers ? selectedUsers : []);
  }, [route.params]);

  const navigateToProfile = () => {
    //Since the logged In User is navigating to own profile, useXId is not required
    navigation.navigate('Profile', {
      screenType,
      userXId: undefined,
    });
  };

  const handleShare = async () => {
    setLoading(true);
    if (!image.filename) {
      return;
    }
    postMoment(image.filename, image.path, caption, title, userId).then(
      (data) => {
        setLoading(false);
        if (data) {
          dispatch(loadUserMoments(userId));
          dispatch(updateProfileCompletionStage(data));
          navigateToProfile();
          setTimeout(() => {
            Alert.alert(SUCCESS_PIC_UPLOAD);
          }, 500);
        } else {
          setTimeout(() => {
            Alert.alert(ERROR_UPLOAD);
          }, 500);
        }
      },
    );
  };

  return (
    <SearchBackground>
      {loading ? <TaggLoadingIndicator fullscreen /> : <Fragment />}
      <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
        <KeyboardAvoidingView
          behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
          style={styles.flex}>
          <View style={styles.contentContainer}>
            <View style={styles.buttonsContainer}>
              <Button
                title="Cancel"
                buttonStyle={styles.button}
                onPress={() => navigateToProfile()}
              />
              <Button
                title="Share"
                titleStyle={styles.shareButtonTitle}
                buttonStyle={styles.button}
                onPress={handleShare}
              />
            </View>
            <CaptionScreenHeader style={styles.header} {...{title: title}} />
            <Image
              style={styles.image}
              source={{uri: image.path}}
              resizeMode={'cover'}
            />
            <MentionInput
              containerStyle={styles.text}
              placeholder="Write something....."
              placeholderTextColor="gray"
              value={caption}
              onChange={setCaption}
              partTypes={mentionPartTypes('blue')}
            />
            <View style={{marginHorizontal: '5%', marginTop: '3%'}}>
              <TouchableOpacity
                style={{width: SCREEN_WIDTH}}
                onPress={() =>
                  navigation.navigate('TagSelectionScreen', {
                    selectedUsers: taggedUsers,
                  })
                }>
                <Text style={styles.tagFriendsTitle}>Tag Friends</Text>
              </TouchableOpacity>
              <View style={styles.tagFriendsContainer}>
                {taggedUsers.map((user) => (
                  <View>
                    {/* TODO: Add Icon for Tag Friends */}
                    <ProfilePreview
                      profilePreview={user}
                      previewType={'Tag Selection'}
                      screenType={ScreenType.Profile}
                    />
                  </View>
                ))}
              </View>
            </View>
          </View>
        </KeyboardAvoidingView>
      </TouchableWithoutFeedback>
    </SearchBackground>
  );
};
const styles = StyleSheet.create({
  contentContainer: {
    paddingTop: StatusBarHeight,
    justifyContent: 'flex-end',
  },
  buttonsContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    marginLeft: '5%',
    marginRight: '5%',
  },
  button: {
    backgroundColor: 'transparent',
  },
  shareButtonTitle: {
    fontWeight: 'bold',
    color: TAGG_LIGHT_BLUE_2,
  },
  header: {
    marginVertical: 20,
  },
  image: {
    position: 'relative',
    width: SCREEN_WIDTH,
    aspectRatio: 1,
    marginBottom: '3%',
  },
  text: {
    position: 'relative',
    backgroundColor: 'white',
    width: '100%',
    paddingHorizontal: '2%',
    paddingVertical: '1%',
    height: 60,
  },
  flex: {
    flex: 1,
  },
  tagFriendsTitle: {
    color: 'white',
    fontSize: normalize(12),
    lineHeight: normalize(16.71),
    letterSpacing: normalize(0.3),
    fontWeight: '600',
  },
  tagFriendsContainer: {flexDirection: 'row', marginTop: '3%'},
});

export default CaptionScreen;