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 = ({navigation}) => { const [canSubmit, setCanSubmit] = useState(false); navigation.setOptions({ headerRight: () => ( { if (canSubmit) { uploadUserSelection(); } else { dispatch(suggestedPeopleBadgesFinished()); } }}> {canSubmit ? 'Done' : 'Skip'} ), }); const [selectedBadges, setSelectedBadges] = useState([]); 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 ( {/* filter not working, comment out for now */} {/* {}} top={Animated.useValue(0)} /> */} ); }; 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;