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

    const arr = []

    useEffect(() => {
        setLevels(['Beginner', 'Intermediate', 'Advanced' ]);
    }, []);

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

    const postLevel=() => {
        if (selections.length == 0) {
            alert("Please select a level.")
        } else {
            for(i=0; i < selections.length; i++){
                arr.push(selections[i].value)
            }
            console.log(arr);
            fetch(localhost+'/signup/level', {
                method: "PUT",
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    "username": route.params.username,
                    "level": arr
                })
            })
            .then(
                function(response) {
                    if (response.status === 200 || response.status === 201) {
                        // Successful POST
                        console.log('good postLevel');
                        alert("Successfully added!")
                        navigation.navigate("Interests", {
                            username: route.params.username
                        });
                    } else {
                        // Examine the text in the response
                        console.log('issue in postLevel. 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}] }>Enter your level:</Text>
            <View>
                <SelectMultiple
                    items={levels}
                    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={postLevel}>
                    <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 Level;