-
-
Notifications
You must be signed in to change notification settings - Fork 55
Refactor js classes #96
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
base: 2.x
Are you sure you want to change the base?
Conversation
a187605
to
fe7d44c
Compare
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.
Good work, this will be a new major update since it comes with some breaking changes.
resources/js/Modal.js
Outdated
return; | ||
} | ||
|
||
this.getValues(); |
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.
Please rename to this.getUIElements()
instead of this.getValues()
. Ideally, all DOM elements are stored in the same this.elements
object, such as:
this.elements = this.getUIElements(document.querySelector('#cookies-policy'));
if(! this.elements) {
return;
}
This elements
object can then be further used like this: this.elements.root
, this.elements.reset
, ...
The current this.text
attribute should be renamed to this.translations
and parsed after this.elements
has been set, such as:
this.translations = this.getUITranslations();
getUITranslations() {
let value = this.elements.root.getAttribute('data-text');
this.elements.root.removeAttribute('data-text');
return JSON.parse(value);
}
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.
Update: maybe getUITranslations
will not be necessary, take a look at this comment below.
resources/views/cookies.blade.php
Outdated
@if(!$isReset) | ||
<script data-cookie-consent> | ||
let element = document.querySelector('#cookies-policy'); | ||
if (element) { | ||
element.classList.add('cookies--pre-init'); | ||
element.classList.add('cookies--closing'); | ||
} | ||
</script> | ||
@endif |
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.
Don't like this at all. Should be handled inside the modal.js
init method. In all cases, the #cookies-policy
element should be hidden when JS is enabled and the modal's script has not been initiated.
src/CookiesManager.php
Outdated
return view('cookie-consent::cookies', [ | ||
'cookies' => $this->registrar, | ||
'policy' => $policy, | ||
'isReset' => $isReset, |
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.
Instead of providing a isReset
attribute, it would be better to provide a RenderingContext
(Enum) value to the view. This way developers can hook in all the possible modal rendering contexts:
enum CookieConsentState
{
// User has yet to configure consent
case Pending;
// User has defined a consent configuration
case Configured;
// User has configured consent but wants to show the modal in order to eventually make changes
case Updating;
// User had configured consent but chose to reset the settings (similar to "pending")
case Reset;
}
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.
After reviewing other open PRs, this would be more related to PR #99. Please add this enum there.
In this PR's contect, I don't think isReset
is still needed here because of this comment.
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.
public function __invoke(Request $request) | ||
{ | ||
|
||
} |
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.
Since both routes leading to this controller have named methods, the __invoke
method can be removed.
public function getModalScript() | ||
{ | ||
$content = file_get_contents(LCC_ROOT . '/dist/modal.js'); | ||
return response($content)->header('Content-Type', 'application/javascript'); | ||
} |
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.
Maybe use this opportunity to pass the translation strings to the modal's constructor, like we did for the API routes configuration in getCookiesScript()
?
…t the reset button is always clearly identifiable.
…nly for the modal handling
…latency and only pass the cookie script inside the head tag
Cover #26, #28, and PR #89 which solve #47 and #63. Transitions on the cookie modal don’t work without JavaScript.
Refactor script.js file by creating a modal object. Each logic is now in a dedicated file and we make sure document is fully loaded before doing everything.