-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
📈 [#2965] Add 'Mijn aanvragen' Siteimprove tracking (Dynamic errors) #1563
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/open_inwoner/js/components/siteimprove/error-tracking.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* Note: users can upload multiple faulty files with their respective errors, and can delete them one by one, | ||
* Tracking should only happen on newly added errors, not the entire occurring set. | ||
*/ | ||
// if (typeof _sz === 'undefined') { | ||
// /** Mock SiteImprove `_sz` object for testing - only used during development */ | ||
// var _sz = { | ||
// push: function (data) { | ||
// try { | ||
// console.log('Event pushed to _sz:', data) | ||
// } catch (error) { | ||
// console.error('Error occurred while pushing event data:', error) | ||
// } | ||
// }, | ||
// } | ||
// } | ||
|
||
/** | ||
* Class that handles the transaction between file errors and SiteImprove. | ||
* Used in `/mijn-aanvragen/{number}/{uuid}/status` | ||
*/ | ||
class DynamicFileInputErrors { | ||
constructor() { | ||
this.initialized = false | ||
this.previousErrorState = new Map() | ||
this.bindEvents() | ||
} | ||
|
||
/** | ||
* Binds events to callbacks. | ||
* Use this to define EventListeners, MutationObservers etc. | ||
*/ | ||
bindEvents() { | ||
if (this.#formElement && !this.initialized) { | ||
this.initialized = true | ||
this.#formElement.addEventListener( | ||
'change', | ||
this.handleChanges.bind(this) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Gets called when this.getForm() changes. | ||
* @param {MouseEvent} event | ||
*/ | ||
handleChanges(event) { | ||
if (!this.#fileInputElement || event.target !== this.#fileInputElement) | ||
return | ||
|
||
const currentErrors = Object.entries(this.#occurringErrors).reduce( | ||
(acc, [key, nodes]) => { | ||
nodes.forEach((node) => { | ||
const fileName = | ||
node.querySelector('.file__name').textContent ?? | ||
[...this.#fileListElement.children].indexOf(node) | ||
swrichards marked this conversation as resolved.
Show resolved
Hide resolved
|
||
acc.set(fileName, this.#ERROR_MAP[key]) | ||
}) | ||
return acc | ||
}, | ||
new Map() | ||
) | ||
|
||
currentErrors.forEach((message, id) => { | ||
if (!this.previousErrorState.has(id) && typeof _sz !== 'undefined') | ||
swrichards marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_sz.push(['event', 'Mijn Aanvragen', 'Error', message]) | ||
}) | ||
|
||
// Update previous error state only with persistent errors | ||
this.previousErrorState = currentErrors | ||
} | ||
|
||
/** | ||
* The predefined (Dutch) error messages | ||
* @private | ||
*/ | ||
get #ERROR_MAP() { | ||
return { | ||
type: 'Error verkeerd bestand type.', | ||
size: 'Error bestand te groot.', | ||
typeSize: 'Error bestand te groot en van verkeerde type.', | ||
} | ||
} | ||
|
||
/** | ||
* Get the form element | ||
* @returns {HTMLElement} | ||
* @private | ||
*/ | ||
get #formElement() { | ||
return document.querySelector('#document-upload') | ||
} | ||
|
||
/** | ||
* Get the file input element | ||
* @returns {HTMLElement} | ||
* @private | ||
*/ | ||
get #fileInputElement() { | ||
return document.querySelector('#document-upload .file-input__input') | ||
} | ||
|
||
/** | ||
* Returns a specific file list based on a child. | ||
* @returns {HTMLElement} | ||
* @private | ||
*/ | ||
get #fileListElement() { | ||
return document.querySelector('#document-upload .file-list__list') | ||
} | ||
|
||
/** | ||
* Returns the name of a file or the index of the file in the list. | ||
* @param {HTMLElement} node | ||
* @returns {{type: NodeListOf<Element>, size: NodeListOf<Element>, typeSize: NodeListOf<Element>}} | ||
* @private | ||
*/ | ||
get #occurringErrors() { | ||
return { | ||
type: document.querySelectorAll('.file:has(.error > .file-error__type)'), | ||
size: document.querySelectorAll('.file:has(.error > .file-error__size)'), | ||
typeSize: document.querySelectorAll( | ||
'.file:has(.error > .file-error__type-size)' | ||
), | ||
} | ||
} | ||
} | ||
|
||
// HTMX event listener to start tracking when content updates. - Start! | ||
document.body.addEventListener( | ||
'htmx:afterSwap', | ||
() => new DynamicFileInputErrors() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly unrelated, but: I assume that's a typo in the (pre-existing) filename?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I wanted to keep the filename so we can see the delta/changes - but the developer who made this may have set this name to avoid conflicts with other JS files? But prob a typo. I don't know...