<commit_before>import 'whatwg-fetch'
import * as Cookies from "js-cookie"
import {API_BASE, HOST_PORT, HOST_URL} from './api'
require('dom4') /* Activate polyfill for closest() */

export const toggleFavorite = function (e) {
  e.stopPropagation()
  e.preventDefault()

  var form = e.target.parentNode
  var method = 'PUT'

  const url = API_BASE + 'images/favorite/' + form.elements['identifier'].value
  const csrf = Cookies.get('csrftoken')

  // If they aren't logged in, tell them to do so. We can improve the UI here later.
  if (document.body.dataset.loggedIn != 'True') {
    alert("Please sign in to favorite this image.")
    return
  }

  if (form.dataset.isFavorite === 'True') {
    method = 'DELETE'
  }

  fetch(url, {
    method: method,
    body: JSON.stringify({}),
    credentials: "include",
    headers: {
      "X-CSRFToken": csrf,
      "Content-Type": "application/json"
    }
  })
  .then((response) => {
    if (response.status === 204) { // We removed a favorite
      removeAsFavorite(form)
    }
    else if (response.status === 201 || response.status === 200) {  // We added a favorite
      setAsFavorite(form)
    }
  })
}

export const setAsFavorite = (form) => {
  form.querySelector('button').classList.remove('secondary')
  form.querySelector('button').classList.add('success')
  form.dataset.isFavorite = 'True'
}
export const removeAsFavorite = (form) => {
  form.querySelector('button').classList.remove('success')
  form.querySelector('button').classList.add('secondary')
  form.dataset.isFavorite = 'False'

  // If it's actually in the favorites list, also animate removing it
  if (form.closest('.favorites-list')) {
    form.closest('.image-result').classList.add('animated')
    form.closest('.image-result').classList.add('zoomOut')
  }
}


document.addEventListener('DOMContentLoaded', () => {
  var results = document.querySelector('.image-results')
  let url = API_BASE + 'images/favorites'

  if (results && document.body.dataset.loggedIn === 'True') {<commit_msg>Changed function signature so we can pass an image id instead of the form id so we can re use this function in the new ui<commit_after>import 'whatwg-fetch'
import * as Cookies from "js-cookie"
import {API_BASE, HOST_PORT, HOST_URL} from './api'
require('dom4') /* Activate polyfill for closest() */

export const toggleFavorite = function (e) {
    e.stopPropagation()
    e.preventDefault()

    var form = e.target.parentNode
    toggleFavoriteWithForm (form);
}
export const toggleFavoriteWithForm = function (form, cb) {
    var method = 'PUT'

    const url = API_BASE + 'images/favorite/' + form.elements['identifier'].value
    const csrf = Cookies.get('csrftoken')

    // If they aren't logged in, tell them to do so. We can improve the UI here later.
    if (document.body.dataset.loggedIn != 'True') {
        alert("Please sign in to favorite this image.")
        if (cb) cb (false);
        return
    }

    if (form.dataset.isFavorite === 'True') {
        method = 'DELETE'
    }

    fetch(url, {
        method: method,
        body: JSON.stringify({}),
        credentials: "include",
        headers: {
            "X-CSRFToken": csrf,
            "Content-Type": "application/json"
        }
    })
        .then((response) => {
            var isFav = false;
            if (response.status === 204) { // We removed a favorite
                removeAsFavorite(form)
            }
            else if (response.status === 201 || response.status === 200) {  // We added a favorite
                setAsFavorite(form)
                isFav = true
            }
            if (cb) {
                cb (isFav);
            }
        })
}

export const setAsFavorite = (form) => {
  form.querySelector('button').classList.remove('secondary')
  form.querySelector('button').classList.add('success')
  form.dataset.isFavorite = 'True'
}
export const removeAsFavorite = (form) => {
  form.querySelector('button').classList.remove('success')
  form.querySelector('button').classList.add('secondary')
  form.dataset.isFavorite = 'False'

  // If it's actually in the favorites list, also animate removing it
  if (form.closest('.favorites-list')) {
    form.closest('.image-result').classList.add('animated')
    form.closest('.image-result').classList.add('zoomOut')
  }
}


document.addEventListener('DOMContentLoaded', () => {
  var results = document.querySelector('.image-results')
  let url = API_BASE + 'images/favorites'

  if (results && document.body.dataset.loggedIn === 'True') {
{'fraction_non_alphanumeric': 0.14554302145543022, 'fraction_numerical': 0.004423800044238001, 'mean_word_length': 3.0303030303030303, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 11, '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': '7422754', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1371, 'n_words': 340}