Sha256: 683cdc324d8ad1a42e3ceff28e2c8fc87685bf4ab865f29252e95abbe13330ce

Contents?: true

Size: 1.27 KB

Versions: 9

Compression:

Stored size: 1.27 KB

Contents

import { Controller } from '@hotwired/stimulus'
import { post } from '@rails/request.js'
import { Rollbar } from 'rollbar'

// Connects to data-controller="notifications"
export default class extends Controller {
  timeoutId = null

  connect () {
    this.element.addEventListener('shown.bs.collapse', event => {
      this.timeoutId = setTimeout(() => {
        this.markAsSeen()
      }, 2000)
      document.addEventListener('turbo:load', () => { this.cancelTimeout() })
    })
    this.element.addEventListener('hide.bs.collapse', event => {
      clearTimeout(this.timeoutId)
    })
  }

  async markAsSeen () {
    const ids = []
    document.querySelectorAll('.notification')
      .forEach((e) => { ids.push(e.dataset.id) })
    const response = await post('/u/notifications/mark_as_seen',
      { query: { ids }, responseKind: 'turbo-stream' })

    if (response.ok) {
      document.querySelectorAll('.notification.unseen').forEach(
        (notif) => {
          notif.classList.remove('unseen')
        }
      )
      document.querySelector('.notifications-unseen-mark').remove()
    } else {
      const text = await response.text
      Rollbar.error('Error marking as seen: ', text)
    }
  }

  cancelTimeout () {
    if (this.timeoutId) {
      clearTimeout(this.timeoutId)
    }
  }
}

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pg_rails-7.0.8.pre.alpha.103 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.0.8.pre.alpha.102 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.0.8.pre.alpha.101 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.0.8.pre.alpha.100 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.0.8.pre.alpha.99 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.0.8.pre.alpha.98 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.0.8.pre.alpha.97 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.0.8.pre.alpha.96 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.0.8.pre.alpha.95 pg_layout/app/javascript/controllers/notifications_controller.js