aboutsummaryrefslogtreecommitdiff
path: root/src/screens/badge/BadgeSelection.tsx
blob: f1cd000cd64a64098c45d3dc78903ab2f7e131c4 (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
import AsyncStorage from '@react-native-community/async-storage';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {useEffect, useState} from 'react';
import {Alert, SafeAreaView, StatusBar, StyleSheet, View} from 'react-native';
import {Text} from 'react-native-animatable';
import {TouchableOpacity} from 'react-native-gesture-handler';
import LinearGradient from 'react-native-linear-gradient';
import {useDispatch} from 'react-redux';
import {ADD_USER_BADGES, BACKGROUND_GRADIENT_MAP} from '../../constants';
import {BADGE_DATA} from '../../constants/badges';
import {
  ERROR_BADGES_EXCEED_LIMIT,
  ERROR_UPLOAD_BADGES,
} from '../../constants/strings';
import {suggestedPeopleBadgesFinished} from '../../store/actions';
import {BackgroundGradientType} from '../../types';
import {SCREEN_HEIGHT, StatusBarHeight} from '../../utils';
import BadgeList from './BadgeList';
import BadgeScreenHeader from './BadgeScreenHeader';

/**
 * Home Screen for displaying Tagg Badge Selections
 **/

type BadgeSelectionParamList = {
  BadgeList: any[];
};

type BadgeSelectionScreenNavigationProp = StackNavigationProp<
  BadgeSelectionParamList,
  'BadgeList'
>;

type BadgeSelectionProps = {
  navigation: BadgeSelectionScreenNavigationProp;
};

const BadgeSelection: React.FC<BadgeSelectionProps> = ({navigation}) => {
  const [canSubmit, setCanSubmit] = useState(false);
  navigation.setOptions({
    headerRight: () => (
      <TouchableOpacity
        style={styles.rightButtonContainer}
        onPress={() => {
          if (canSubmit) {
            uploadUserSelection();
          } else {
            dispatch(suggestedPeopleBadgesFinished());
          }
        }}>
        <Text style={styles.rightButton}>{canSubmit ? 'Done' : 'Skip'}</Text>
      </TouchableOpacity>
    ),
  });

  const [selectedBadges, setSelectedBadges] = useState<string[]>([]);
  const selectKey = (key: string) => {
    if (selectedBadges.includes(key)) {
      const selectedBadgesArray = [...selectedBadges];
      const itemIndex = selectedBadgesArray.indexOf(key);
      if (itemIndex > -1) {
        selectedBadgesArray.splice(itemIndex, 1);
      }
      setSelectedBadges(selectedBadgesArray);
    } else {
      const selectedBadgesArray = [...selectedBadges, key];
      setSelectedBadges(selectedBadgesArray);
    }
  };
  const dispatch = useDispatch();
  useEffect(() => {
    setCanSubmit(selectedBadges.length !== 0);
  }, [selectedBadges]);

  const uploadUserSelection = async () => {
    try {
      const token = await AsyncStorage.getItem('token');
      const form = new FormData();
      form.append('badges', JSON.stringify(selectedBadges));
      const response = await fetch(ADD_USER_BADGES, {
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/form-data',
          Authorization: 'Token ' + token,
        },
        body: form,
      });
      if (response.status === 400) {
        Alert.alert(ERROR_BADGES_EXCEED_LIMIT);
        return;
      }
      dispatch(suggestedPeopleBadgesFinished());
    } catch (error) {
      console.log(error);
      Alert.alert(ERROR_UPLOAD_BADGES);
    }
  };

  return (
    <LinearGradient
      colors={BACKGROUND_GRADIENT_MAP[BackgroundGradientType.Dark]}
      style={styles.container}>
      <StatusBar barStyle={'light-content'} />
      <SafeAreaView>
        <View style={styles.listContainer}>
          <BadgeScreenHeader />
          {/* filter not working, comment out for now */}
          {/* <SearchBar
            style={styles.searchBarStyle}
            onCancel={() => {}}
            top={Animated.useValue(0)}
          /> */}
          <BadgeList
            data={BADGE_DATA}
            selectedBadges={selectedBadges}
            selectKey={selectKey}
          />
        </View>
      </SafeAreaView>
    </LinearGradient>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  searchBarStyle: {
    width: '95%',
    alignSelf: 'center',
    marginTop: SCREEN_HEIGHT * 0.05,
  },
  viewContainer: {marginTop: StatusBarHeight},
  listContainer: {marginTop: SCREEN_HEIGHT * 0.05},
  rightButtonContainer: {marginRight: 24},
  rightButton: {
    color: '#FFFFFF',
    fontWeight: 'bold',
    fontSize: 15,
    lineHeight: 18,
  },
  leftButtonContainer: {marginLeft: 24},
  leftButton: {
    color: '#FFFFFF',
    fontWeight: '500',
    fontSize: 15,
    lineHeight: 18,
  },
});

export default BadgeSelection;