BoilerLiftUp / src / components / signup / Privacy.jsx
Privacy.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 Privacy({route, navigation}) {
    const { colors } = useTheme();
    const [settings, setSettings] = useState([]);
    const [selections, setSelections] = useState([]);

    const arr = []

    useEffect(() => {
        setSettings(['Can friends see your stats?', 'Can your records be used publicly?' ]);
    }, []);

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

    function postPrivacy() {
        for(i=0; i < selections.length; i++){
            arr.push(selections[i].value)
        }
        
        // Determine the privacy number to be stored by examining the 
        // arr generated based off their selections

        var privacyNumber = 0;

        if (arr.length == 2) { // They selected both
            privacyNumber = 3;
        } else if (arr.length == 0) { // They selected neither
            privacyNumber = 0;
        } else { // They selected only one
            if (arr[0] == "Can friends see your stats?") {
                privacyNumber = 1;
            } else if (arr[0] == "Can your records be used publicly?") {
                privacyNumber = 2;
            }
        }
        fetch(localhost+'/signup/privacy', {
            method: "PUT",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                "username": route.params.username,
                "privacy": privacyNumber
            })
        })
        .then(
            function(response) {
                if (response.status === 200 || response.status === 201) {
                    // Successful POST
                    console.log('good postPrivacy');
                    alert("Successfully added!")
                    navigation.navigate("UploadProfilePic", {
                        username: route.params.username
                    });
                } else {
                    // Examine the text in the response
                    console.log('issue in postPrivacy. Status code: '+ response.status);
                    alert("Unsuccessful")
                }
            }
        )
        .catch(function(err) {
            console.log('Fetch Error :-S', err);
        });
    }

    return (
        <SafeAreaView style={ [styles.screen, {flexDirection:"column"}] }>
            <Text style={ [styles.title, {color: colors.text}] }>Privacy:</Text>
            <View>
                <SelectMultiple
                    items={settings}
                    selectedItems={selections}
                    onSelectionsChange={onSelectionsChange}
                    labelStyle={{ color: colors.text}}
                    rowStyle={{ backgroundColor: 'white'}}
                    checkboxStyle={{ backgroundColor: "#f0b543", borderRadius: 10 }}
                />
            </View>
            <View style={{flexDirection: "row"}}>
                <Button style={ styles.nextButton } onPress={postPrivacy}>
                    <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'
    },
    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 Privacy;