Sha256: b833e82c90f94846a9a247edb0f49cd9424ef13d5e98db93af0201f47640a2b8

Contents?: true

Size: 1.76 KB

Versions: 78

Compression:

Stored size: 1.76 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 markAsUnseen (e) {
    const notification = e.target.closest('.notification')
    notification.dataset.markedAsUnseen = true
    const id = notification.dataset.id
    const response = await post('/u/notifications/mark_as_unseen',
      { query: { id } })

    if (response.ok) {
      notification.classList.add('unseen')
    } else {
      const text = await response.text
      Rollbar.error('Error marking as unseen: ', text)
    }
  }

  async markAsSeen () {
    const ids = []
    const targets = Array.from(document.querySelectorAll('.notification')).filter((e) => {
      return !e.dataset.markedAsUnseen
    })
    targets.forEach((e) => { ids.push(e.dataset.id) })

    const response = await post('/u/notifications/mark_as_seen',
      { query: { ids }, responseKind: 'turbo-stream' })

    if (response.ok) {
      targets.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

78 entries across 78 versions & 1 rubygems

Version Path
pg_rails-7.6.21.pre.4 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.21.pre.3 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.21.pre.2 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.21.pre.1 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.20 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.19 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.18 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.17 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.16 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.15 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.14 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.13 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.12 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.11 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.10 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.9 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.8 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.7 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.6 pg_layout/app/javascript/controllers/notifications_controller.js
pg_rails-7.6.5 pg_layout/app/javascript/controllers/notifications_controller.js