Sha256: 77c9c1ada1fcc89f9009c64817529b4426f0680ddc575f4f77127dff14660ab3

Contents?: true

Size: 1.07 KB

Versions: 9

Compression:

Stored size: 1.07 KB

Contents

import { Controller } from "stimulus"
import axios from "axios"

export default class extends Controller {
  async more(event) {
    event.preventDefault()
    const button = event.currentTarget
    const path = event.currentTarget.href
    button.classList.add('is-loading')

    try {
      const response = await axios.get(path)
      const listContents = document.createElement('template')
      listContents.innerHTML = response.data
      const listId = this.element.id.split('-')[1]
      const list = document.getElementById('list-' + listId)
      console.log(list, this.element, listContents)
      list.insertBefore(listContents.content.cloneNode(true), this.element)
      
      let nextPage = path.match(/page=(\d+)/)[1]
      nextPage = parseInt(nextPage, 10) + 1
      button.href = button.href.replace(/page=(\d+)/, "page=" + nextPage)
      button.classList.remove('is-loading')

      if (response.data.includes("data-list-done=\"true\"")) {
        button.remove()
      }
    } catch (error) {
      button.classList.remove('is-loading')
      console.error(error)
    }
  }
}

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
yarii-editor-0.4.8 app/javascript/controllers/list_loading_controller.js
yarii-editor-0.4.7 app/javascript/controllers/list_loading_controller.js
yarii-editor-0.4.6 app/javascript/controllers/list_loading_controller.js
yarii-editor-0.4.5 app/javascript/controllers/list_loading_controller.js
yarii-editor-0.4.4 app/javascript/controllers/list_loading_controller.js
yarii-editor-0.4.3 app/javascript/controllers/list_loading_controller.js
yarii-editor-0.4.2 app/javascript/controllers/list_loading_controller.js
yarii-editor-0.4.1 app/javascript/controllers/list_loading_controller.js
yarii-editor-0.4.0 app/javascript/controllers/list_loading_controller.js