<commit_before>/**
 * Copyright (c) 2017 <NAME> <<EMAIL>>
 *
 * This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the GNU Affero General Public License,
 * version 3, as published by the Free Software Foundation.
 */

import { debounce } from '../dom/time'

export class AutosaveForm extends HTMLElement {
  constructor () {
    super()

    this._onInput = debounce(this._onInput.bind(this))
  }

  connectedCallback () {
    this.addEventListener('input', this._onInput)
    this.addEventListener('change', this._onInput)
  }

  disconnectedCallback () {
    this.removeEventListener('input', this._onInput)
    this.removeEventListener('change', this._onInput)
  }

  /**
   *
   * @param {Event} event
   * @private
   */
  _onInput (event) {
    console.log(event.target)
  }
}
<commit_msg>Implement saving of inputs<commit_after>/**
 * Copyright (c) 2017 <NAME> <<EMAIL>>
 *
 * This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the GNU Affero General Public License,
 * version 3, as published by the Free Software Foundation.
 */
import { debounce } from '../dom/time'
import { getCsrfToken } from '../dom/environment'
import { rejectHttpErrors } from '../dom/fetch'
import { formData } from '../dom/form'
import { handleAjaxError, handleAjaxResponse } from '../app/ajax'

export class AutosaveForm extends HTMLElement {
  constructor () {
    super()

    this._onInput = debounce(this._onInput.bind(this))
  }

  connectedCallback () {
    this.addEventListener('input', this._onInput)
    this.addEventListener('change', this._onInput)
  }

  disconnectedCallback () {
    this.removeEventListener('input', this._onInput)
    this.removeEventListener('change', this._onInput)
  }

  /**
   *
   * @param {Event} event
   * @private
   */
  _onInput (event) {
    const target = event.target

    const uri = target.getAttribute('save-uri')
    const data = formData({ [target.name]: target.value, token: getCsrfToken() })

    fetch(uri, { method: 'post', data })
      .then(rejectHttpErrors)
      .then((resp) => resp.json())
      .then((data) => handleAjaxResponse(data))
      .catch(() => handleAjaxError())
  }
}

{'fraction_non_alphanumeric': 0.1306363253265908, 'fraction_numerical': 0.004214075010535188, 'mean_word_length': 3.496212121212121, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, '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': '6425521', 'n_tokens_mistral': 710, 'n_tokens_neox': 691, 'n_words': 201}