<commit_before>import React from 'react'
import { connect } from 'react-redux'
import { any, get, omit, filter, startsWith, contains } from 'lodash'
import {
  CREATE_NETWORK,
  createNetwork,
  navigate,
  resetNetworkValidation,
  updateNetworkEditor,
  validateNetworkAttribute
} from '../../actions'
import { uploadImage } from '../../actions/uploadImage'
import { avatarUploadSettings, bannerUploadSettings } from '../../models/network'
import { scrollToBottom } from '../../util/scrolling'
import CommunityTagInput from '../../components/CommunityTagInput'
const { bool, func, object } = React.PropTypes

const defaultBanner = 'https://d3ngex8q79bk55.cloudfront.net/misc/default_community_banner.jpg'
const defaultAvatar = 'https://d3ngex8q79bk55.cloudfront.net/misc/default_community_avatar.png'

@connect(({people, networkEditor, networkValidation, pending}) => {
  let validating = any(networkValidation.pending)
  let { network, errors } = networkEditor
  let saving = pending[CREATE_NETWORK]

  if (!errors) errors = {}
  errors.nameUsed = get(networkValidation, 'name.unique') === false
  errors.slugUsed = get(networkValidation, 'slug.unique') === false
  errors.codeUsed = get(networkValidation, 'beta_access_code.unique') === false

  if (!network) network = {}
  if (!network.avatar_url) network.avatar_url = defaultAvatar
  if (!network.banner_url) network.banner_url = defaultBanner

  return {network, errors, validating, saving, currentUser: people.current}
})
export default class NetworkEditor extends React.Component {
  static propTypes = {
    saving: bool,
    validating: bool,
    errors: object,
    dispatch: func,
    network: object,
    currentUser: object
  }

  constructor (props) {
    super(props)
    this.state = {communityChoiceTerm: ''}
  }

  setValue = (key, value) => {
    return this.props.dispatch(updateNetworkEditor('network', {[key]: value}))
  }

  setError = obj =>
    this.props.dispatch(updateNetworkEditor('errors', obj))

  resetValidation = key =>
    this.props.dispatch(resetNetworkValidation(key))

  checkUnique = (key, value) =>
    this.props.dispatch(validateNetworkAttribute(key, value, 'unique'))

  setName = event => {
    let { value } = event.target
    this.setValue('name', value)
    this.validateName(value)
  }

  validateName (value) {
    this.setError({nameBlank: !value})

    if (!value) {
      this.resetValidation('name')
    } else {
      return this.checkUnique('name', value)
    }
  }

  setDescription = event => {
    let { value } = event.target
    this.setValue('description', value)
    this.validateDescription(value)
  }

  validateDescription (value) {
    this.setError({descriptionBlank: !value})
  }

  setSlug = event => {
    let { value } = event.target
    this.setValue('slug', value)
    this.validateSlug(value)
  }

  validateSlug (value) {
    let error = {slugBlank: false, slugInvalid: false}

    if (!value) {
      error.slugBlank = true
    } else if (!value.match(/^[a-z0-9-+]+$/)) {
      error.slugInvalid = true
    }

    this.setError(error)

    if (any(error)) {
      this.resetValidation('slug')
    } else {
      return this.checkUnique('slug', value)
    }
  }

  setCode = event => {
    let { value } = event.target
    this.setValue('beta_access_code', value)
    this.validateCode(value)
  }

  validateCode (value) {
    this.setError({codeBlank: !value})

    if (!value) {
      this.resetValidation('beta_access_code')
    } else {
      return this.checkUnique('beta_access_code', value)
    }
  }

  setCategory = event => {
    let { value } = event.target
    this.setValue('category', value)
    this.validateCategory(value)
  }

  validateCategory (value) {
    this.setError({categoryBlank: !value})
  }

  setLocation = event => {
    this.setValue('location', event.target.value)
  }

  addCommunity = community => {
    let { communities } = this.props.network
    this.setValue('communities', (communities || []).concat(community.id))
  }

  removeCommunity = community => {
    let { communities } = this.props.network
    this.setValue('communities', filter(communities, cid => cid !== community.id))
  }

  updateCommunityChoiceTerm = term => {
    this.setState({communityChoiceTerm: term})
  }

  getCommunityChoices = term => {
    if (!term) {
      return []
    }

    let { currentUser, network: { communities } } = this.props
    var match = c =>
      startsWith(c.name.toLowerCase(), term.toLowerCase()) &&
      !contains(communities, c.id)

    return filter(currentUser.memberships.map(m => m.community), match)<commit_msg>only autocomplete communities you are moderator of<commit_after>import React from 'react'
import { connect } from 'react-redux'
import { any, get, omit, filter, startsWith, contains } from 'lodash'
import {
  CREATE_NETWORK,
  createNetwork,
  navigate,
  resetNetworkValidation,
  updateNetworkEditor,
  validateNetworkAttribute
} from '../../actions'
import { uploadImage } from '../../actions/uploadImage'
import { avatarUploadSettings, bannerUploadSettings } from '../../models/network'
import { scrollToBottom } from '../../util/scrolling'
import CommunityTagInput from '../../components/CommunityTagInput'
const { bool, func, object } = React.PropTypes

const defaultBanner = 'https://d3ngex8q79bk55.cloudfront.net/misc/default_community_banner.jpg'
const defaultAvatar = 'https://d3ngex8q79bk55.cloudfront.net/misc/default_community_avatar.png'

@connect(({people, networkEditor, networkValidation, pending}) => {
  let validating = any(networkValidation.pending)
  let { network, errors } = networkEditor
  let saving = pending[CREATE_NETWORK]

  if (!errors) errors = {}
  errors.nameUsed = get(networkValidation, 'name.unique') === false
  errors.slugUsed = get(networkValidation, 'slug.unique') === false
  errors.codeUsed = get(networkValidation, 'beta_access_code.unique') === false

  if (!network) network = {}
  if (!network.avatar_url) network.avatar_url = defaultAvatar
  if (!network.banner_url) network.banner_url = defaultBanner

  return {network, errors, validating, saving, currentUser: people.current}
})
export default class NetworkEditor extends React.Component {
  static propTypes = {
    saving: bool,
    validating: bool,
    errors: object,
    dispatch: func,
    network: object,
    currentUser: object
  }

  constructor (props) {
    super(props)
    this.state = {communityChoiceTerm: ''}
  }

  setValue = (key, value) => {
    return this.props.dispatch(updateNetworkEditor('network', {[key]: value}))
  }

  setError = obj =>
    this.props.dispatch(updateNetworkEditor('errors', obj))

  resetValidation = key =>
    this.props.dispatch(resetNetworkValidation(key))

  checkUnique = (key, value) =>
    this.props.dispatch(validateNetworkAttribute(key, value, 'unique'))

  setName = event => {
    let { value } = event.target
    this.setValue('name', value)
    this.validateName(value)
  }

  validateName (value) {
    this.setError({nameBlank: !value})

    if (!value) {
      this.resetValidation('name')
    } else {
      return this.checkUnique('name', value)
    }
  }

  setDescription = event => {
    let { value } = event.target
    this.setValue('description', value)
    this.validateDescription(value)
  }

  validateDescription (value) {
    this.setError({descriptionBlank: !value})
  }

  setSlug = event => {
    let { value } = event.target
    this.setValue('slug', value)
    this.validateSlug(value)
  }

  validateSlug (value) {
    let error = {slugBlank: false, slugInvalid: false}

    if (!value) {
      error.slugBlank = true
    } else if (!value.match(/^[a-z0-9-+]+$/)) {
      error.slugInvalid = true
    }

    this.setError(error)

    if (any(error)) {
      this.resetValidation('slug')
    } else {
      return this.checkUnique('slug', value)
    }
  }

  setCode = event => {
    let { value } = event.target
    this.setValue('beta_access_code', value)
    this.validateCode(value)
  }

  validateCode (value) {
    this.setError({codeBlank: !value})

    if (!value) {
      this.resetValidation('beta_access_code')
    } else {
      return this.checkUnique('beta_access_code', value)
    }
  }

  setCategory = event => {
    let { value } = event.target
    this.setValue('category', value)
    this.validateCategory(value)
  }

  validateCategory (value) {
    this.setError({categoryBlank: !value})
  }

  setLocation = event => {
    this.setValue('location', event.target.value)
  }

  addCommunity = community => {
    let { communities } = this.props.network
    this.setValue('communities', (communities || []).concat(community.id))
  }

  removeCommunity = community => {
    let { communities } = this.props.network
    this.setValue('communities', filter(communities, cid => cid !== community.id))
  }

  updateCommunityChoiceTerm = term => {
    this.setState({communityChoiceTerm: term})
  }

  getCommunityChoices = term => {
    if (!term) {
      return []
    }

    let { currentUser, network: { communities } } = this.props
    var match = c =>
      startsWith(c.name.toLowerCase(), term.toLowerCase()) &&
      !contains(communities, c.id)

    return filter(filter(currentUser.memberships, m => m.role === 1).map(m => m.community), match)
{'fraction_non_alphanumeric': 0.13885281385281387, 'fraction_numerical': 0.0031385281385281384, 'mean_word_length': 3.5679683638161146, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, 'https://': 4, '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': '7306667', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2822, 'n_words': 584}