Closed himanshunaidu closed 2 months ago
Moving the code of the stats.js file back to stats.ejs fixes this issue, which is of course, not something I'd want to do. But it implies, that we may need to rethink about the event being used. Or is there something I am missing here?
Does this need to be executed before the request? If so, the htmx:beforerequest event will do the trick.
Yes, as you can see, the very first time the app is run, this 'NaN' is bound to happen (at least this has been consistently happening in my experiments'
Got it! Thanks
So I tried multiple events to get this to run, ranging from beforerequest, right up to afterSettle. By any chance, could it be that, because we are loading the Javascript as an src tag? There seem to be some not-so-straightforward issues with the stats.ejs file, in relation to loading the script externally.
E.g. Let's see I run the following code in the stats.ejs file:
<script src="/scripts/stats.js"></script>
<script>
console.log(getCookies);
var locals = getCookies() // From the utils.js file
var stats_variables = getDefaultStatsVariables()
"DOMContentLoaded htmx:load".split(" ").forEach(function(e){
console.log('Adding event listener to', e);
document.addEventListener(e, function() {
//...Code
})
})
</script>
This gives the error: Uncaught ReferenceError: getDefaultStatsVariables is not defined
Despite the fact that getDefaultStatsVariables is defined in the stats.js file
On testing the very first commit when this htmx event was added https://github.com/thuiop/magium-dev/commit/ec5935353014346cc6f2c14d80d461f4dc9672e1#diff-1cd37efa893161a9f03c7679ccdc81e7b47092ceb0179a81a46821dde2334760
This worked. And in this one, the js code was still inside the stats.ejs file.
So there is most definitely a problem with adding scripts inline.
Spent quite a long time trying to figure out the fix without solution :(
I guess I am going to leave it open for now, and focus on the saves page. Feel free to look into it when you can. Afaik, it is not really that serious of a bug.
The stats page does not load properly when we open it for the first time. The event: htmx:afterSwap is not being emitted on time, in the stats.js file.
"DOMContentLoaded htmx:afterSwap".split(" ").forEach(function(e){ document.addEventListener(e, function() { var stats_missing = stats_variables.some(function(stat) { var value = locals[stat]; return value === null || value === undefined; });
Due to this, when we open the 'Invest Points Now' for the first time, we get NaN in the 'Available Points' section.
Perhaps, we need a different htmx event.