<commit_before>import PropTypes from 'prop-types'
import React from 'react'
import Page, {mapDispatchToProps, propTypes as pagePropTypes} from 'components/Page'
import autobind from 'autobind-decorator'

import Fieldset from 'components/Fieldset'
import Breadcrumbs from 'components/Breadcrumbs'
import Form from 'components/Form'

import API from 'api'

import AppContext from 'components/AppContext'
import { connect } from 'react-redux'

class BaseAdminIndex extends Page {

	static propTypes = {
		...pagePropTypes,
		loadAppData: PropTypes.func,
	}

	constructor(props) {
		super(props)

		this.state = {
			settings: {},
		}
	}

	fetchData(props) {
		return API.query(/* GraphQL */`
			adminSettings { key, value }
		`).then(data => {
			let settings = {}
			data.adminSettings.forEach(setting => settings[setting.key] = setting.value)
			this.setState({settings})
		})
	}

	render() {
		let {settings} = this.state

		return (
			<div>
				<Breadcrumbs items={[['Admin settings', '/admin']]} />

				<Form formFor={settings} horizontal submitText="Save settings" onChange={this.onChange} onSubmit={this.onSubmit}>
					<Fieldset title="Site settings">
						{Object.map(settings, (key, value) =>
							<Form.Field id={key} key={key} />
						)}
					</Fieldset>
				</Form>
			</div>
		)
	}

	@autobind
	onChange(event) {
		let settings = this.state.settings
		this.setState({settings})
	}

	@autobind
	onSubmit(event) {
		event.stopPropagation()
		event.preventDefault()
		// settings as JSON
		let settings = Object.map(this.state.settings, (key, value) => ({key, value}))
		let graphql = 'saveAdminSettings(settings: $settings)'
		const variables = { settings: settings }
		const variableDef = '($settings: [AdminSettingInput]!)'
		API.mutation(graphql, variables, variableDef, {disableSubmits: true})
			.then(data => {
				this.props.loadAppData()
			}).catch(error => {
				this.setState({success: null, error: error})
				window.scrollTo(0, 0)
				console.error(error)
			})
	}

}

const AdminIndex = (props) => (
	<AppContext.Consumer>
		{context =>
			<BaseAdminIndex loadAppData={context.loadAppData} {...props} /><commit_msg>NCI-Agency/anet#822: Add success/error message to admin settings page<commit_after>import PropTypes from 'prop-types'
import React from 'react'
import Page, {mapDispatchToProps, propTypes as pagePropTypes} from 'components/Page'
import autobind from 'autobind-decorator'

import Fieldset from 'components/Fieldset'
import Breadcrumbs from 'components/Breadcrumbs'
import Form from 'components/Form'
import Messages from 'components/Messages'

import API from 'api'

import AppContext from 'components/AppContext'
import { connect } from 'react-redux'

class BaseAdminIndex extends Page {

	static propTypes = {
		...pagePropTypes,
		loadAppData: PropTypes.func,
	}

	constructor(props) {
		super(props)

		this.state = {
			success: null,
			error: null,
			settings: {},
		}
	}

	fetchData(props) {
		return API.query(/* GraphQL */`
			adminSettings { key, value }
		`).then(data => {
			let settings = {}
			data.adminSettings.forEach(setting => settings[setting.key] = setting.value)
			this.setState({settings})
		})
	}

	render() {
		let {settings} = this.state

		return (
			<div>
				<Breadcrumbs items={[['Admin settings', '/admin']]} />
				<Messages success={this.state.success} error={this.state.error} />

				<Form formFor={settings} horizontal submitText="Save settings" onChange={this.onChange} onSubmit={this.onSubmit}>
					<Fieldset title="Site settings">
						{Object.map(settings, (key, value) =>
							<Form.Field id={key} key={key} />
						)}
					</Fieldset>
				</Form>
			</div>
		)
	}

	@autobind
	onChange(event) {
		let settings = this.state.settings
		this.setState({settings})
	}

	@autobind
	onSubmit(event) {
		event.stopPropagation()
		event.preventDefault()
		// settings as JSON
		let settings = Object.map(this.state.settings, (key, value) => ({key, value}))
		let graphql = 'saveAdminSettings(settings: $settings)'
		const variables = { settings: settings }
		const variableDef = '($settings: [AdminSettingInput]!)'
		API.mutation(graphql, variables, variableDef, {disableSubmits: true})
			.then(data => {
				this.setState({success: 'Admin settings saved', error: null})
				window.scrollTo(0, 0)
				this.props.loadAppData()
			}).catch(error => {
				this.setState({success: null, error: error})
				window.scrollTo(0, 0)
				console.error(error)
			})
	}

}

const AdminIndex = (props) => (
	<AppContext.Consumer>
		{context =>
			<BaseAdminIndex loadAppData={context.loadAppData} {...props} />
{'fraction_non_alphanumeric': 0.14550610820244328, 'fraction_numerical': 0.001963350785340314, 'mean_word_length': 3.97828447339848, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 40, 'https://': 0, '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': '3198470', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1485, 'n_words': 336}