<commit_before><template>
  <div class="edit-guild" v-if="guild != null">
    <header>
      <guild-icon :guild="guild"/>
      <h2>{{ guild.name }}</h2>
    </header>
    <div class="flash" v-if="flash">{{ flash }}</div>

    <!-- guild information -->
    <ul>
      <li>Members: {{ guild.members }}</li>
      <li>Owner: <strong>{{ guild.owner.tag }}</strong> (<code>{{ guild.owner.id }}</code>)</li>
    </ul>

    <!-- guild config -->
    <h3>Configuration{{ dirty ? '*' : '' }}</h3>
    <div class="error" v-if="error">{{ error }}</div>
    <!-- buttons above textarea -->
    <div class="toolbar">
      <button type="button" :disabled="error" @click="save"
        title="You can also press CTRL+S (or CMD+S on Macs).">Save Changes</button>
      <spinner v-if="saving"/>
    </div>
    <ace-editor v-if="config != null"
      @change="validateConfig"
      @save="save"
      :content="config"
      lang="yaml"
      theme="chrome"/>
    <spinner v-else/>
  </div>
</template>

<script>
import API from '@/api'
import GuildIcon from '@/components/GuildIcon'
import Spinner from '@/components/Spinner'

import AceEditor from '@/components/AceEditor'
import 'brace/mode/yaml'
import 'brace/ext/searchbox'
import 'brace/theme/chrome'

import yaml from 'js-yaml'
import { validate } from '@/schema'

function check (raw) {
  let doc = yaml.safeLoad(raw)
  validate(doc)
}

export default {
  name: 'edit-guild',
  data () {
    return {
      guild: null,
      dirty: false,
      flash: null,
      flashing: false,
      error: null,
      saving: false,
      config: null
    }
  },
  components: { GuildIcon, AceEditor, Spinner },
  methods: {
    async save () {
      if (this.error) return
      if (this.config == null) return

      this.saving = true
      console.log('Saving...')
      await API.patch(`/api/guild/${this.guildId}/config`, this.config, {
        headers: {
          'content-type': 'text/yaml'
        }
      })
      this.showFlash('Saved.')
      this.saving = false
      this.dirty = false<commit_msg>Handle any save errors gracefully<commit_after><template>
  <div class="edit-guild" v-if="guild != null">
    <header>
      <guild-icon :guild="guild"/>
      <h2>{{ guild.name }}</h2>
    </header>
    <div class="flash" v-if="flash">{{ flash }}</div>

    <!-- guild information -->
    <ul>
      <li>Members: {{ guild.members }}</li>
      <li>Owner: <strong>{{ guild.owner.tag }}</strong> (<code>{{ guild.owner.id }}</code>)</li>
    </ul>

    <!-- guild config -->
    <h3>Configuration{{ dirty ? '*' : '' }}</h3>
    <div class="error" v-if="error">{{ error }}</div>
    <!-- buttons above textarea -->
    <div class="toolbar">
      <button type="button" :disabled="error" @click="save"
        title="You can also press CTRL+S (or CMD+S on Macs).">Save Changes</button>
      <spinner v-if="saving"/>
    </div>
    <ace-editor v-if="config != null"
      @change="validateConfig"
      @save="save"
      :content="config"
      lang="yaml"
      theme="chrome"/>
    <spinner v-else/>
  </div>
</template>

<script>
import API from '@/api'
import GuildIcon from '@/components/GuildIcon'
import Spinner from '@/components/Spinner'

import AceEditor from '@/components/AceEditor'
import 'brace/mode/yaml'
import 'brace/ext/searchbox'
import 'brace/theme/chrome'

import yaml from 'js-yaml'
import { validate } from '@/schema'

function check (raw) {
  let doc = yaml.safeLoad(raw)
  validate(doc)
}

export default {
  name: 'edit-guild',
  data () {
    return {
      guild: null,
      dirty: false,
      flash: null,
      flashing: false,
      error: null,
      saving: false,
      config: null
    }
  },
  components: { GuildIcon, AceEditor, Spinner },
  methods: {
    async save () {
      if (this.error) return
      if (this.config == null) return

      this.saving = true

      const resp = await API.patch(`/api/guild/${this.guildId}/config`, this.config, {
        headers: {
          'content-type': 'text/yaml'
        }
      })

      if (resp.error) {
        alert(`Failed to save: ${resp.message} (${resp.code})`)
        this.saving = false
        return
      }

      this.showFlash('Saved.')
      this.saving = false
      this.dirty = false
{'fraction_non_alphanumeric': 0.18171077504725897, 'fraction_numerical': 0.001890359168241966, 'mean_word_length': 2.548197820620285, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 75, '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': '5496814', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1399, 'n_words': 336}