fix: preserve recipe progress view state

Refs #1, #5
This commit is contained in:
2026-08-14 21:02:21 -05:00
parent 22daf703bb
commit a8fbcf4104
4 changed files with 59 additions and 8 deletions
+35 -3
View File
@@ -28,9 +28,17 @@ Module.register("MMM-NextcloudCookbook", {
socketNotificationReceived(notification, payload) {
if (notification !== "STATE") return;
const previous = this.state;
this.state = payload;
this.setRecipeMode(Boolean(payload.active));
this.updateDom(this.config.animationSpeed);
const requiresDomUpdate = previous.active !== payload.active
|| previous.recipeId !== payload.recipeId
|| previous.portions !== payload.portions
|| previous.theme !== payload.theme
|| previous.error !== payload.error
|| previous.recipe?.recipeYield !== payload.recipe?.recipeYield;
if (requiresDomUpdate) this.updateDom(this.config.animationSpeed);
else this.updateProgressDom();
},
notificationReceived(notification) {
@@ -86,6 +94,24 @@ Module.register("MMM-NextcloudCookbook", {
}
},
updateProgressDom() {
const root = document.getElementById(this.identifier)?.querySelector(".ncc-active");
if (!root) return;
root.querySelectorAll("[data-ncc-progress-kind]").forEach((item) => {
const kind = item.dataset.nccProgressKind;
const index = Number(item.dataset.nccProgressIndex);
const completed = kind === "ingredient"
? this.state.progress?.completedIngredients?.includes(index)
: this.state.progress?.completedSteps?.includes(index);
item.classList.toggle("ncc-completed", completed);
const checkbox = item.querySelector("input[type=checkbox]");
if (checkbox) {
checkbox.checked = completed;
checkbox.disabled = false;
}
});
},
getDom() {
const root = document.createElement("section");
root.className = `ncc-root ${this.state.active ? "ncc-active" : "ncc-idle"} ncc-theme-${this.state.theme || this.config.theme}`;
@@ -143,9 +169,11 @@ Module.register("MMM-NextcloudCookbook", {
title.textContent = recipe.name || "Untitled recipe";
header.appendChild(title);
const yieldValue = recipe.originalRecipeYield && recipe.originalRecipeYield !== recipe.recipeYield
? `${recipe.recipeYield} (originally ${recipe.originalRecipeYield})`
: recipe.recipeYield;
const metadata = [
["Yield", recipe.recipeYield],
["Original yield", recipe.originalRecipeYield !== recipe.recipeYield ? recipe.originalRecipeYield : null],
["Yield", yieldValue],
["Prep", NCCFormatDuration(recipe.prepTime)],
["Cook", NCCFormatDuration(recipe.cookTime)],
["Total", NCCFormatDuration(recipe.totalTime)]
@@ -178,6 +206,8 @@ Module.register("MMM-NextcloudCookbook", {
const ingredientList = document.createElement("ul");
(recipe.recipeIngredient || []).forEach((ingredient, index) => {
const item = document.createElement("li");
item.dataset.nccProgressKind = "ingredient";
item.dataset.nccProgressIndex = index;
const status = recipe.recipeIngredientStatus?.[index];
if (status === "unquantified") {
item.classList.add("ncc-quantity-warning");
@@ -215,6 +245,8 @@ Module.register("MMM-NextcloudCookbook", {
const instructionList = document.createElement("ol");
this.flattenInstructions(recipe.recipeInstructions || []).forEach((step, index) => {
const item = document.createElement("li");
item.dataset.nccProgressKind = "step";
item.dataset.nccProgressIndex = index;
const completed = progress.completedSteps?.includes(index);
if (completed) item.classList.add("ncc-completed");
const label = document.createElement("label");