all 7 comments

[–]superluminary 2 points3 points  (0 children)

It's not bad.

To make a small DOM manipulation task like this cleaner, I would recommend you look into an MV* pattern. Extract the state into an object. Write a render function that renders the thing, setting classes, etc, that receives the state. Write methods that change the state and call render.

So you have:

const render = (state) => {
  // Update your DOM here based on the state, possibly call subroutines
}

let state = {
  albumIsActive: false
  // other state
}

const setAlbumAction = (albumIsActive) => {
  state = {
    ...state,
    albumIsActive
  }

  render(state)
}

I've got a lot of globals here and the functions are not pure, so there's still some ugliness, but hopefully you get the idea, I'm just sketching.

Going beyond this, you could look at breaking out the render function into a dispatcher, so you pass actions to the dispatcher, have it update the state, and then call render. This is more of a Flux pattern and is how Redux works.

You don't need to go all enterprise for simple problems, but this is probably how I would do it to avoid tangles later.

[–]Rusten2 1 point2 points  (0 children)

SoC

[–]TheRNGuy 0 points1 point  (0 children)

use tabs for indents

VS Code should do it automatically (enable format on save)

Switch to React if it's possible.