• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / blurts-server / #13322

pending completion
#13322

push

circleci

web-flow
Merge pull request #3001 from mozilla/main

Merge `main` into `localization`

282 of 1768 branches covered (15.95%)

Branch coverage included in aggregate %.

451 of 451 new or added lines in 56 files covered. (100.0%)

959 of 4670 relevant lines covered (20.54%)

1.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/client/js/partials/unsubscribe.js
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4

5
const unsubscribePartial = document.querySelector('[data-partial="unsubscribe"]')
×
6

7
function init () {
8
  unsubscribePartial.querySelector('.js-unsubscribe-button').addEventListener('click', handleEvent)
×
9
}
10

11
async function handleEvent (event) {
12
  // TODO: Use more specific and localised messages when we reinstate
13
  // unsubscribing from all emails.
14
  const errorMessage = 'Unsubscribing failed.'
×
15
  const successMessage = 'Unsubscribed successfully.'
×
16

17
  try {
×
18
    const target = event.target
×
19
    const csrfToken = target.getAttribute('data-csrf-token')
×
20
    const unsubscribeParameters = getUnsubscribeParameters()
×
21

22
    if (unsubscribeParameters === null) {
×
23
      const missingParametersToast = document.createElement('toast-alert')
×
24
      missingParametersToast.textContent = errorMessage
×
25
      document.body.append(missingParametersToast)
×
26
      return
×
27
    }
28

29
    const response = await fetch('/user/unsubscribe', {
×
30
      headers: {
31
        'Content-Type': 'application/json',
32
        'x-csrf-token': csrfToken
33
      },
34
      mode: 'same-origin',
35
      method: 'POST',
36
      body: JSON.stringify(unsubscribeParameters)
37
    })
38

39
    if (response?.redirected) {
×
40
      throw response.error
×
41
    }
42

43
    let toast
44
    if (!response.ok) {
×
45
      toast = document.createElement('toast-alert')
×
46
      toast.textContent = errorMessage
×
47
    } else {
48
      toast = document.createElement('toast-alert')
×
49
      toast.setAttribute('type', 'success')
×
50
      toast.textContent = successMessage
×
51
    }
52

53
    document.body.append(toast)
×
54
  } catch (error) {
55
    throw new Error(errorMessage)
×
56
  }
57

58
  window.gtag('event', 'unsubscribed', { action: 'click', page_location: location.href })
×
59
}
60

61
/**
62
 * @returns { null | { hash: string; token: string; } }
63
 */
64
function getUnsubscribeParameters () {
65
  const queryParams = new URLSearchParams(document.location.search)
×
66
  const token = queryParams.get('token')
×
67
  const hash = queryParams.get('hash')
×
68
  if (typeof token === 'string' && typeof hash === 'string') {
×
69
    return { hash, token }
×
70
  }
71
  return null
×
72
}
73

74
if (unsubscribePartial) init()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc