BoilerLiftUp / src / components / signup / Interests.jsx
Interests.jsx
Raw
import React, { useEffect, useState } from "react";
import { StyleSheet, SafeAreaView, Text, TextInput, View } from "react-native";
import { Button} from 'native-base';
import { useTheme } from '@react-navigation/native';
import SelectMultiple from 'react-native-select-multiple';

function Interests({route, navigation}) {
    const { colors } = useTheme();
    const [interests, setInterests] = useState([]);
    const [optionalInterest, setOptionalInterest] = useState('');
    const [selections, setSelections] = useState([]);
    useEffect(() => {
        setInterests(['Running', 'Biking', 'Yoga', 'Lifting', 'Swimming', 'Tennis', 'Basketball' ]);
    }, []);

    const onSelectionsChange = interestSelection => {
        setSelections(interestSelection)
    }

    const arr = []
    const postInterests=() => {
        if (selections.length != 0) {
            for(i=0; i < selections.length; i++){
                arr.push(selections[i].value)
            }
            arr.push(optionalInterest)
            fetch(localhost+'/signup/interest', {
                method: "PUT",
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    "username": route.params.username,
                    "interests": arr
                })
            })
            .then(
                function(response) {
                    if (response.status === 200 || response.status === 201) {
                        // Successful POST
                        console.log('good postInterests');
                        alert("Successfully added!")
                        navigation.navigate("Privacy", {
                            username: route.params.username
                        });
                    } else {
                        // Examine the text in the response
                        console.log('issue in postInterests. Status code: '+ response.status);
                        alert("Unsuccessful")
                    }
                }
            )
            .catch(function(err) {
                console.log('Fetch Error :-S', err);
            });
        }
        navigation.navigate("Privacy", {
            username: route.params.username
        });
    }

    return (
        <SafeAreaView style={ [styles.screen, {flexDirection:"column"}] }>
            <Text style={ [styles.title, {color: colors.text}] }>Enter your Interests:</Text>
            <View>
                <SelectMultiple
                    items={interests}
                    selectedItems={selections}
                    onSelectionsChange={onSelectionsChange}
                    labelStyle={{ color: colors.text}}
                    rowStyle={{ backgroundColor: 'white'}}
                    checkboxStyle={{ backgroundColor: "#f0b543", borderRadius: 10 }}
                />
            </View>
            <Text style={ [styles.subtitle, {color: colors.text}] }>Other interest (optional): </Text>
            <TextInput placeholder = "ex: Soccer" style={ [styles.input, {color: colors.text}] } onChangeText={(optionalInterest) => setOptionalInterest(optionalInterest)} />
            <View style={{flexDirection: "row"}}>
                <Button style={ styles.nextButton } onPress={postInterests}>
                    <Text style={ styles.nextText } >Next</Text>
                </Button>
            </View>
        </SafeAreaView>
    );
}

const styles = StyleSheet.create({
    screen:{
        marginTop: "20%",
        paddingLeft: "5%",
        paddingRight: "5%",
        paddingBottom: "30%",
        marginLeft: "10%",
        marginRight: "10%"
    },
    title: {
        fontSize: 25,
        fontWeight: "bold",
        marginTop: "10%",
        paddingBottom: "20%",
        textAlign:"left"
    },
    subtitle: {
        fontSize: 16,
        textAlign:"left",
        fontWeight: "bold",
        color: '#5c5a55',
        marginTop: 20
    },
    input: {
        width: "100%",
        height: 40,
        borderBottomWidth: 1
    },
    nextButton: {
        width: '100%',
        justifyContent: 'center',
        backgroundColor: "#d4911c",
        borderRadius: 10,
        alignItems: "center",
        marginBottom: 10,
        marginTop: 50
    },
    nextText: {
        fontSize: 16,
        fontWeight: "bold",
        color: "white"
    },
});

export default Interests;