thuiop / magium-dev

MIT License
13 stars 4 forks source link

Stats page not initializing properly during initial load #38

Closed himanshunaidu closed 2 months ago

himanshunaidu commented 2 months ago

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.

Available_Nan

himanshunaidu commented 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?

thuiop commented 2 months ago

Does this need to be executed before the request? If so, the htmx:beforerequest event will do the trick.

himanshunaidu commented 2 months ago

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'

himanshunaidu commented 2 months ago

Got it! Thanks

himanshunaidu commented 2 months ago

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.

himanshunaidu commented 2 months ago

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

himanshunaidu commented 2 months ago

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.

himanshunaidu commented 2 months ago

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.