aboutsummaryrefslogtreecommitdiff
path: root/src/components/profile/PublicProfile.tsx
blob: b89203512a77026e453d980683c1de65df149d2d (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import {useFocusEffect, useNavigation} from '@react-navigation/native';
import React, {useCallback, useEffect, useState} from 'react';
import {Alert, StyleSheet, Text, View} from 'react-native';
import {TouchableOpacity} from 'react-native-gesture-handler';
import {useDispatch, useSelector} from 'react-redux';
import GreyPlusLogo from '../../assets/icons/grey-plus-logo.svg';
import {TAGG_LIGHT_BLUE} from '../../constants';
import {
  UPLOAD_MOMENT_PROMPT_THREE_HEADER,
  UPLOAD_MOMENT_PROMPT_THREE_MESSAGE,
  UPLOAD_MOMENT_PROMPT_TWO_HEADER,
  UPLOAD_MOMENT_PROMPT_TWO_MESSAGE,
} from '../../constants/strings';
import {
  deleteUserMomentsForCategory,
  updateMomentCategories,
} from '../../store/actions';
import {EMPTY_MOMENTS_LIST, NO_PROFILE} from '../../store/initialStates';
import {RootState} from '../../store/rootreducer';
import {ContentProps, MomentType} from '../../types';
import {moveCategory, normalize, SCREEN_HEIGHT} from '../../utils';
import {TaggPrompt} from '../common';
import {Moment} from '../moments';

const PublicProfile: React.FC<ContentProps> = ({
  userXId,
  screenType,
  setScrollEnabled,
  profileBodyHeight,
  socialsBarHeight,
  scrollViewRef,
}) => {
  const dispatch = useDispatch();

  const {profile = NO_PROFILE} = useSelector((state: RootState) =>
    userXId ? state.userX[screenType][userXId] : state.user,
  );

  const {moments = EMPTY_MOMENTS_LIST} = useSelector((state: RootState) =>
    userXId ? state.userX[screenType][userXId] : state.moments,
  );

  const {momentCategories = []} = useSelector((state: RootState) =>
    userXId ? state.userX[screenType][userXId] : state.momentCategories,
  );

  const navigation = useNavigation();

  /**
   * States
   */
  const [imagesMap, setImagesMap] = useState<Map<string, MomentType[]>>(
    new Map(),
  );

  const [isStageTwoPromptClosed, setIsStageTwoPromptClosed] = useState(false);
  const [isStageOnePromptClosed, setIsStageOnePromptClosed] = useState(false);
  const [isStageThreePromptClosed, setIsStageThreePromptClosed] = useState(
    false,
  );

  const move = (direction: 'up' | 'down', title: string) => {
    let categories = [...momentCategories];
    categories = moveCategory(categories, title, direction === 'up');
    dispatch(updateMomentCategories(categories, false));
  };

  /**
   * Prompt user to perform an activity based on their profile completion stage
   * To fire 2 seconds after the screen comes in focus
   * 1 means STAGE_1:
   *    The user must upload a moment, so take them to a screen  guiding them to post a moment
   * 2 means STAGE_2:
   *    The user must create another category so show a prompt on top of the screen
   * 3 means STAGE_3:
   *    The user must upload a moment to the second category, so show a prompt on top of the screen
   * Else, profile is complete and no prompt needs to be shown
   */
  useFocusEffect(
    useCallback(() => {
      const navigateToMomentUploadPrompt = () => {
        switch (profile.profile_completion_stage) {
          case 1:
            if (
              momentCategories &&
              momentCategories[0] &&
              !isStageOnePromptClosed &&
              scrollViewRef.current
            ) {
              setScrollEnabled(false);
              scrollViewRef.current.scrollTo({y: 0});
              navigation.navigate('MomentUploadPrompt', {
                screenType,
                momentCategory: momentCategories[0],
                profileBodyHeight,
                socialsBarHeight,
              });
              setIsStageOnePromptClosed(true);
            }
            break;
          case 2:
            setIsStageTwoPromptClosed(false);
            break;
          case 3:
            setIsStageThreePromptClosed(false);
            break;
          default:
            break;
        }
      };
      if (!userXId) {
        setTimeout(() => {
          navigateToMomentUploadPrompt();
          setScrollEnabled(true);
        }, 2000);
      }
    }, [
      userXId,
      profile.profile_completion_stage,
      momentCategories,
      isStageOnePromptClosed,
      setScrollEnabled,
      navigation,
      screenType,
      profileBodyHeight,
      socialsBarHeight,
      scrollViewRef,
    ]),
  );

  /**
   * Handle deletion of a category
   * Confirm with user before deleting the category
   * @param category category to be deleted
   */
  const handleCategoryDeletion = (category: string) => {
    Alert.alert(
      'Category Deletion',
      `Are you sure that you want to delete the category ${category} ?`,
      [
        {
          text: 'Cancel',
          style: 'cancel',
        },
        {
          text: 'Yes',
          onPress: () => {
            dispatch(
              updateMomentCategories(
                momentCategories.filter((mc) => mc !== category),
                false,
              ),
            );
            dispatch(deleteUserMomentsForCategory(category));
          },
        },
      ],
      {cancelable: true},
    );
  };

  const createImagesMap = useCallback(() => {
    let map = new Map();
    moments.forEach(function (imageObject) {
      let moment_category = imageObject.moment_category;
      if (map.has(moment_category)) {
        map.get(moment_category).push(imageObject);
      } else {
        map.set(moment_category, [imageObject]);
      }
    });
    setImagesMap(map);
  }, [moments]);

  useEffect(() => {
    createImagesMap();
  }, [createImagesMap]);

  return (
    <View style={styles.momentsContainer}>
      {userXId && moments.length === 0 && (
        <View style={styles.plusIconContainer}>
          <GreyPlusLogo width={90} height={90} />
          <Text style={styles.noMomentsText}>{`Looks like ${
            profile.name.split(' ')[0]
          } has not posted any moments yet`}</Text>
        </View>
      )}
      {!userXId &&
        profile.profile_completion_stage === 2 &&
        !isStageTwoPromptClosed && (
          <TaggPrompt
            messageHeader={UPLOAD_MOMENT_PROMPT_TWO_HEADER}
            messageBody={UPLOAD_MOMENT_PROMPT_TWO_MESSAGE}
            logoType="tagg"
            onClose={() => {
              setIsStageTwoPromptClosed(true);
            }}
          />
        )}
      {!userXId &&
        profile.profile_completion_stage === 3 &&
        !isStageThreePromptClosed && (
          <TaggPrompt
            messageHeader={UPLOAD_MOMENT_PROMPT_THREE_HEADER}
            messageBody={UPLOAD_MOMENT_PROMPT_THREE_MESSAGE}
            logoType="tagg"
            onClose={() => {
              setIsStageThreePromptClosed(true);
            }}
          />
        )}
      {momentCategories.map(
        (title, index) =>
          (!userXId || imagesMap.get(title)) && (
            <Moment
              key={index}
              title={title}
              images={imagesMap.get(title)}
              userXId={userXId}
              screenType={screenType}
              handleMomentCategoryDelete={handleCategoryDeletion}
              shouldAllowDeletion={momentCategories.length > 1}
              showUpButton={index !== 0}
              showDownButton={index !== momentCategories.length - 1}
              move={move}
            />
          ),
      )}
      {!userXId && (
        <TouchableOpacity
          onPress={() =>
            navigation.navigate('CategorySelection', {
              newCustomCategory: undefined,
            })
          }
          style={styles.createCategoryButton}>
          <Text style={styles.createCategoryButtonLabel}>
            Create a new category
          </Text>
        </TouchableOpacity>
      )}
    </View>
  );
};

const styles = StyleSheet.create({
  momentsContainer: {
    backgroundColor: '#f2f2f2',
    paddingBottom: SCREEN_HEIGHT * 0.15,
    flex: 1,
    flexDirection: 'column',
  },
  createCategoryButton: {
    backgroundColor: TAGG_LIGHT_BLUE,
    justifyContent: 'center',
    alignItems: 'center',
    width: '70%',
    height: 30,
    marginTop: '15%',
    alignSelf: 'center',
  },
  createCategoryButtonLabel: {
    fontSize: normalize(16),
    fontWeight: '500',
    color: 'white',
  },
  plusIconContainer: {
    flexDirection: 'column',
    justifyContent: 'center',
    alignItems: 'center',
    marginVertical: '10%',
  },
  noMomentsText: {
    fontSize: normalize(14),
    fontWeight: 'bold',
    color: 'gray',
    marginVertical: '8%',
  },
});

export default PublicProfile;