<commit_before>//User has ability to create a new user petition from New Item Menu. GH15
//If user is on "All" feed and tries to create new item, user must choose which group the item will be posted to.
//If user is already looking at a specific group (e.g. USA group) in newsfeed tab (e.g. not "all"), app will assume new post is for that group.
//https://api-dev.powerli.ne/api-doc#post--api-v2.2-groups-{group}-user-petitions

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Actions } from 'react-native-router-flux';
import {
    Container,
    Content,
    Header,
    Left,
    Right,
    Label,
    Text,
    Button,
    Icon,
    Title,
    Body,
    Footer,
    Textarea,
    View,
    List,
    ListItem,
    Thumbnail,
    Input
} from 'native-base';
const PLColors = require('PLColors');
import styles from './styles';
import SuggestionBox from '../../../common/suggestionBox';
import {
    Dimensions,
    ScrollView,
    TextInput
} from 'react-native';
const { width, height } = Dimensions.get('window');
import { loadUserData, getGroups, getUsersByGroup, createPetition, getPetitionConfig } from 'PLActions';
import { showToast } from 'PLToast';

const PETITION_MAX_LENGTH = 7000;
import randomPlaceholder from '../../../utils/placeholder';

class NewPetition extends Component{
    constructor(props){
        super(props);

        this.state = {
            showCommunity: true,
            profile: {},
            grouplist: [],
            selectedGroupIndex: -1,
            title: "",
            content: "",
            petition_remaining: null,
            mentionEntry: null,
            suggestionSearch: '',
            groupUsers: []
        };

        this.placeholderTitle = randomPlaceholder('petition');
        this.onSelectionChange = this.onSelectionChange.bind(this);
    }

    componentDidMount(){
        var { token } = this.props;
        loadUserData(token).then(data => {
            this.setState({
                profile: data
            });
        })
        .catch(err => {

        });

        getGroups(token).then(ret => {
            this.setState({
                grouplist: ret.payload
            });
        })
        .catch(err => {

        });
    }

    toggleCommunity(){
        this.setState({
           showCommunity: ! this.state.showCommunity 
        });
    }

    selectGroupList(index){
        this.setState({
            selectedGroupIndex: index,
            showCommunity: false
        });

        var { token } = this.props;

        getPetitionConfig(token, this.state.grouplist[index].id)
        .then(data => {
            this.setState({
                petition_remaining: data.petitions_remaining
            });
        })
        .catch(err => {

        });
    }

    createPetition(){
        if(this.state.selectedGroupIndex == -1){
            alert('Where do you want to post this? Select a group');
            return;
        }else if(this.state.title == '' || this.state.title.trim() == ''){
            alert("Please create a title for your petition");
            return;
        }else if(this.state.content == '' || this.state.content.trim() == ''){
            alert("Whoops! Looks like you forgot to write your petition down!")
        }

        var  { token } = this.props;
        createPetition(token, this.state.grouplist[this.state.selectedGroupIndex].id, this.state.title, this.state.content)
        .then(data => {
            showToast('Petition Successful!');
            //alert('Petition successfully created!');
            Actions.itemDetail({ entityId: data.id, entityType: 'user-petition' });
        })
        .catch(err => {

        });
    }<commit_msg>add return<commit_after>//User has ability to create a new user petition from New Item Menu. GH15
//If user is on "All" feed and tries to create new item, user must choose which group the item will be posted to.
//If user is already looking at a specific group (e.g. USA group) in newsfeed tab (e.g. not "all"), app will assume new post is for that group.
//https://api-dev.powerli.ne/api-doc#post--api-v2.2-groups-{group}-user-petitions

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Actions } from 'react-native-router-flux';
import {
    Container,
    Content,
    Header,
    Left,
    Right,
    Label,
    Text,
    Button,
    Icon,
    Title,
    Body,
    Footer,
    Textarea,
    View,
    List,
    ListItem,
    Thumbnail,
    Input
} from 'native-base';
const PLColors = require('PLColors');
import styles from './styles';
import SuggestionBox from '../../../common/suggestionBox';
import {
    Dimensions,
    ScrollView,
    TextInput
} from 'react-native';
const { width, height } = Dimensions.get('window');
import { loadUserData, getGroups, getUsersByGroup, createPetition, getPetitionConfig } from 'PLActions';
import { showToast } from 'PLToast';

const PETITION_MAX_LENGTH = 7000;
import randomPlaceholder from '../../../utils/placeholder';

class NewPetition extends Component{
    constructor(props){
        super(props);

        this.state = {
            showCommunity: true,
            profile: {},
            grouplist: [],
            selectedGroupIndex: -1,
            title: "",
            content: "",
            petition_remaining: null,
            mentionEntry: null,
            suggestionSearch: '',
            groupUsers: []
        };

        this.placeholderTitle = randomPlaceholder('petition');
        this.onSelectionChange = this.onSelectionChange.bind(this);
    }

    componentDidMount(){
        var { token } = this.props;
        loadUserData(token).then(data => {
            this.setState({
                profile: data
            });
        })
        .catch(err => {

        });

        getGroups(token).then(ret => {
            this.setState({
                grouplist: ret.payload
            });
        })
        .catch(err => {

        });
    }

    toggleCommunity(){
        this.setState({
           showCommunity: ! this.state.showCommunity 
        });
    }

    selectGroupList(index){
        this.setState({
            selectedGroupIndex: index,
            showCommunity: false
        });

        var { token } = this.props;

        getPetitionConfig(token, this.state.grouplist[index].id)
        .then(data => {
            this.setState({
                petition_remaining: data.petitions_remaining
            });
        })
        .catch(err => {

        });
    }

    createPetition(){
        if(this.state.selectedGroupIndex == -1){
            alert('Where do you want to post this? Select a group');
            return;
        }else if(this.state.title == '' || this.state.title.trim() == ''){
            alert("Please create a title for your petition");
            return;
        }else if(this.state.content == '' || this.state.content.trim() == ''){
            alert("Whoops! Looks like you forgot to write your petition down!");
            return;
        }

        var  { token } = this.props;
        createPetition(token, this.state.grouplist[this.state.selectedGroupIndex].id, this.state.title, this.state.content)
        .then(data => {
            showToast('Petition Successful!');
            Actions.itemDetail({ entityId: data.id, entityType: 'user-petition' });
        })
        .catch(err => {
        });
    }
{'fraction_non_alphanumeric': 0.12810140237324702, 'fraction_numerical': 0.002696871628910464, 'mean_word_length': 1.9895203546956872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6259371', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2039, 'n_words': 509}