BoilerLiftUp / src / components / community / CreateCommunity.jsx
CreateCommunity.jsx
Raw
import React, { useState } from "react";
import { StyleSheet, SafeAreaView, Text, TextInput, View } from "react-native";
import { Button} from 'native-base';
import { useTheme } from '@react-navigation/native';

function CreateCommunity({navigation, route}) {
    const { colors } = useTheme();
    const [comName, setComName] = useState('');
    const [comDescription, setComDescription] = useState('');
    const { user } = route.params;

    function postComm(){
        fetch(localhost+'/community', {
            method: "POST",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                "ComName": comName, 
                "owner": user,
                "admins": [user],
                "members" : [user],
                "description" : comDescription
            })
        })
        .then(
            function(response) {
                if (response.status === 200 || response.status === 201) {
                    // Successful POST
                    console.log('good postComm');
                    alert("Successfully created!")
                } else {
                    // Examine the text in the response
                    console.log('issue in postComm. Status code: '+ response.status);
                    alert("Unsuccessful")
                }
            }
        )
        .catch(function(err) {
            console.log('Fetch Error :-S', err);
        });
    }

    function handleSubmit(){
        postComm();
        navigation.navigate("Communities")
    }
    function handleCancel(){
        navigation.navigate("Communities")
    }
    return (
        <SafeAreaView style={ [styles.screen, {flexDirection:"column"}] }>
            <Text style={ [styles.title, {color: colors.text}] }>Create a Community:</Text>
            <Text style={ [styles.subtitle, {color: colors.text}] }>Name:</Text>
            <TextInput style={ [styles.input, {color: colors.text}] } onChangeText={(comName) => setComName(comName)} />
            <Text style={ [styles.subtitle, {color: colors.text}] }>Description:</Text>
            <TextInput multiline = {true} numberOfLines = {5} style={ [styles.input, {color: colors.text}] } onChangeText={(comDescription) => setComDescription(comDescription)} />
            <View style={{flexDirection: "row"}}>
                <Button style={ styles.nextButton } onPress={handleSubmit}>
                    <Text style={ styles.nextText } >Submit</Text>
                </Button>
            </View>
            <View style={{flexDirection: "row"}}>
                <Button style={ styles.nextButton } onPress={handleCancel}>
                    <Text style={ styles.nextText } >Cancel</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:"center"
    },
    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 CreateCommunity;