docbrown/app/assets/javascripts/initializers/initializeReadingListIcons.js
Ridhwana 9dee48dbc2
Add tracking for the registration flow - "Clicked on Create Account" (#18459)
* refactor: move trackEvents to a utilities folder so that it can be re-used

* feat: add a function called trackCommentClicks and use it on the articlePage

* feat: update the auth widget to take the correct properties

* feat: add create account tracking method for hamburger

* feat: add create account tracking method for sidebar left

* feat: whoops, sidebar tracking info added

* feat: add the feed card create account tracking

* feat: whoops add the id to the dom

* feat: add top nav create account tracking

* feat: create account tracking for the registration modal

* fix: callback instead of invoking + rename class

* fix: js-prepend

* feat: some showLogin event tracking

* use the tracking propert

* x - feed.jsx

* fix: oops I removed hidden

* track from the search login

* feat: add tracking for follow button

* fix: main_stories is only being called in view/articles and both are including the FollowButtons pack

* comments track

* feat: remove punc (.)

* chore: cahnge working

* chore: remove tracking-id from modal

* feat: remove ecmascript features

* refactor: rename some events

* feat: referrer page

* feat: add a secondary source for the follow buttons

* feat: remove file that is not used

* chore: rename key

* feat: use @utilities

* feat: add spec for create account clicks

* feat: add key referrer

* fix: tests

* fix: oops

* chore: add commented test

* test for tracking

* feat: update the change by 1

* feat: aggregate_failures

* feat: add data-no-instant back

* feat: try a different way of testing

* refactor: change secondary_source to referring_souce

* feat: change ahoy event test

* feat: add a version number to ahoy tracking

* feat: change the href to pathname

* feat: reply comment tracking

* fix: to be present syntax

* feat: use href instead of pathname

* chore: do not track tags and podcasts follow and remove referrer

* feat: add readinglist

* fix: remove referrer from test

* feat: fail gracefully in instances where there is no tracking data
2022-09-22 16:03:49 +02:00

166 lines
4.5 KiB
JavaScript

/* eslint-disable no-use-before-define */
/* eslint-disable no-undef */
/* eslint-disable func-names */
/* eslint-disable consistent-return */
/* eslint-disable no-unused-vars */
function initializeReadingListIcons() {
setReadingListButtonsState();
addReadingListCountToHomePage();
}
// set SAVE or SAVED articles buttons
function setReadingListButtonsState() {
var readingListButtons = document.querySelectorAll(
'.bookmark-button:not([data-initial-feed])',
);
Array.from(readingListButtons).forEach(highlightButton);
}
// private
function highlightButton(button) {
var user = userData();
var buttonIdInt = parseInt(button.dataset.reactableId, 10);
if (user && user.reading_list_ids.indexOf(buttonIdInt) > -1) {
button.classList.add('selected');
} else {
button.classList.remove('selected');
}
button.addEventListener('click', reactToReadingListButtonClick);
button.dataset.saveInitialized = true;
}
function addReadingListCountToHomePage() {
const user = userData();
const readingListContainers = document.querySelectorAll(
'.js-reading-list-count',
);
if (user && readingListContainers) {
readingListContainers.forEach(function (e) {
const readingListCount =
user.reading_list_ids.length > 0 ? user.reading_list_ids.length : '';
e.innerHTML = readingListCount;
e.dataset.count = user.reading_list_ids.length;
});
}
}
function reactToReadingListButtonClick(event) {
var button;
var userStatus;
event.preventDefault();
sendHapticMessage('medium');
userStatus = document.body.getAttribute('data-user-status');
if (userStatus === 'logged-out') {
showLoginModal({
referring_source: 'post_index_toolbar',
trigger: 'reading_list',
});
return;
}
button = properButtonFromEvent(event);
renderOptimisticResult(button);
getCsrfToken()
.then(sendFetch('reaction-creation', buttonFormData(button)))
.then(function (response) {
if (response.status === 200) {
return response.json().then(function (json) {
renderButtonState(button, json);
renderNewSidebarCount(button, json);
});
} // else {
// there's currently no errorCb.
// }
})
.catch(function (error) {
// there's currently no error handling.
});
}
function renderButtonState(button, json) {
if (json.result === 'create') {
button.classList.add('selected');
} else {
button.classList.remove('selected');
}
}
function renderNewSidebarCount(button, json) {
const readingListContainers = document.querySelectorAll(
'.js-reading-list-count',
);
if (readingListContainers) {
readingListContainers.forEach(function (e) {
const count = parseInt(e.dataset.count, 10);
let newCount;
if (json.result === 'create') {
newCount = count + 1;
} else if (count !== 0) {
newCount = count - 1;
}
e.dataset.count = newCount;
e.innerHTML = newCount > 0 ? newCount : '';
});
}
}
function buttonFormData(button) {
var formData = new FormData();
formData.append('reactable_type', 'Article');
formData.append('reactable_id', button.dataset.reactableId);
formData.append('category', 'readinglist');
return formData;
}
function renderOptimisticResult(button) {
renderButtonState(button, { result: 'create' }); // optimistic create only for now
}
function properButtonFromEvent(event) {
var properElement;
if (event.target.tagName === 'BUTTON') {
properElement = event.target;
} else {
properElement = event.target.parentElement;
}
return properElement;
}
/*
Determines if the element is the target of the reading list button hover.
*/
function isReadingListButtonHoverTarget(element) {
var classList = element.classList;
return (
(element.tagName === 'BUTTON' &&
classList.contains('bookmark-button') &&
classList.contains('selected')) ||
(element.tagName === 'SPAN' && classList.contains('bm-success'))
);
}
function readingListButtonMouseHandler(event) {
var target = event.target;
if (isReadingListButtonHoverTarget(target)) {
event.preventDefault();
var textReplacement = this; // `this` is the text to be replaced
var textSpan;
if (target.tagName === 'BUTTON') {
textSpan = target.getElementsByClassName('bm-success')[0];
} else {
textSpan = target;
}
textSpan.innerHTML = textReplacement;
}
}
/* eslint-enable no-use-before-define */
/* eslint-enable no-undef */
/* eslint-enable func-names */
/* eslint-enable consistent-return */
/* eslint-enable no-unused-vars */