github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/assets/scripts/share-by-link.js (about)

     1  ;(function (w, d) {
     2    if (!w.fetch || !w.Headers) return
     3  
     4    const form = d.getElementById('share-by-link-password-form')
     5    const field = d.getElementById('password-field')
     6    const submit = d.getElementById('password-submit')
     7    const input = d.getElementById('password')
     8    const permID = d.getElementById('perm-id')
     9  
    10    const onSubmit = function (event) {
    11      event.preventDefault()
    12      input.setAttribute('disabled', true)
    13      submit.setAttribute('disabled', true)
    14  
    15      const data = new URLSearchParams()
    16      data.append('password', input.value)
    17      data.append('perm_id', permID.value)
    18  
    19      const headers = new Headers()
    20      headers.append('Content-Type', 'application/x-www-form-urlencoded')
    21  
    22      return fetch(form.action, {
    23        method: 'POST',
    24        headers: headers,
    25        body: data,
    26        credentials: 'include',
    27      })
    28        .then((response) => {
    29          if (response.status < 400) {
    30            const tooltip = field.querySelector('.invalid-tooltip')
    31            if (tooltip) {
    32              tooltip.classList.add('d-none')
    33            }
    34            submit.innerHTML = '<span class="icon icon-check"></span>'
    35            submit.classList.add('btn-done')
    36            w.location.reload()
    37          } else {
    38            return response.json().then(function (body) {
    39              w.showError(field, body.error)
    40              input.removeAttribute('disabled')
    41              submit.removeAttribute('disabled')
    42            })
    43          }
    44        })
    45        .catch((err) => w.showError(field, err))
    46    }
    47  
    48    form.addEventListener('submit', onSubmit)
    49  })(window, document)