Skip to content

Conversation

@iOvergaard
Copy link
Contributor

@iOvergaard iOvergaard commented Nov 6, 2025

Prerequisites

  • I have added steps to test this contribution in the description below

Fixes: #20262

Description

This PR fixes a bug where the "Discard changes?" dialog appears after creating and saving a template with a master template, even though no changes were made.

Root Cause

When a template is created with a master template and saved:

  1. The workspace auto-redirects from /create/... to /edit/{id}
  2. The load() method sets both persisted and current data from the server
  3. setMasterTemplate(unique, false) is called to update the UI
  4. Bug: The _data.updateCurrent() call was executing even when updateLayoutBlock=false, causing the current data to diverge from persisted data
  5. This triggered the spurious "Discard changes?" dialog

The Fix

Moves the _data.updateCurrent({ masterTemplate: unique ? { unique } : null }) call inside the if (updateLayoutBlock) block in the setMasterTemplate() method.

This completes the fix started in PR #20529, which added the updateLayoutBlock parameter to prevent overwriting layout settings from disk, but inadvertently left the data model update outside the conditional.

Before:

if (updateLayoutBlock) {
    this.#updateMasterTemplateLayoutBlock();
}
this._data.updateCurrent({ masterTemplate: unique ? { unique } : null });

After:

if (updateLayoutBlock) {
    this.#updateMasterTemplateLayoutBlock();
    this._data.updateCurrent({ masterTemplate: unique ? { unique } : null });
}

Why This Works

  • When updateLayoutBlock=false (during load): We only update the #masterTemplate observable for UI display, without modifying the data model or content
  • When updateLayoutBlock=true (during user actions): We update both the layout block in the Razor content AND the data model, properly marking the workspace as having changes

Testing

Test 1: Creating a Template with Master Template (Issue #20262)

  1. Create a master template (e.g., "MasterLayout")
  2. Create a new template and select "MasterLayout" as the master template
  3. Save the template
  4. Navigate away from the template
  5. Expected: No "Discard changes?" dialog should appear
  6. Before fix: "Discard changes?" dialog appeared

Test 2: Creating a Sub-Template Under a Parent

  1. Create a master template (e.g., "ParentTemplate")
  2. Right-click on "ParentTemplate" in the tree and select "Create"
  3. Name the new template (e.g., "ChildTemplate") and save
  4. Verify the content shows Layout = "ParentTemplate.cshtml";
  5. Navigate away from the template
  6. Expected: No "Discard changes?" dialog should appear

Test 3: Changing Master Template via UI Picker

  1. Create or open an existing template
  2. Click the "Master template" button and select a different master template
  3. Verify the layout block in the content is updated (e.g., Layout = "NewMaster.cshtml";)
  4. Try to navigate away
  5. Expected: "Discard changes?" dialog SHOULD appear (this is a real unsaved change)
  6. Save the template
  7. Navigate away
  8. Expected: No "Discard changes?" dialog should appear

Test 4: Removing Master Template via UI

  1. Open a template that has a master template set
  2. Click the delete icon next to the master template button to remove it
  3. Verify the layout block is updated to Layout = null;
  4. Try to navigate away
  5. Expected: "Discard changes?" dialog SHOULD appear (this is a real unsaved change)

Test 5: Preserving Layout from Disk (PR #20529 regression check)

  1. Create two templates at the root (e.g., "LayoutBase" and "ContentPage")
  2. Manually edit the "ContentPage.cshtml" file on disk to set Layout = "LayoutBase.cshtml";
  3. Load "ContentPage" in the backoffice
  4. Expected: The backoffice shows Layout = "LayoutBase.cshtml"; (not overwritten to Layout = null;)
  5. Navigate away
  6. Expected: No "Discard changes?" dialog should appear

… master template (fixes #20262)

Moves the _data.updateCurrent() call inside the updateLayoutBlock conditional
in setMasterTemplate(). This prevents spurious change detection when loading
templates from the server, while maintaining proper change tracking when users
actually modify the master template via the UI.

This completes the fix started in PR #20529 which added the updateLayoutBlock
parameter but inadvertently left the data model update outside the conditional.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Copilot AI review requested due to automatic review settings November 6, 2025 09:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in the template workspace context where the master template data was being updated unconditionally, even when the updateLayoutBlock parameter was false. The change moves the _data.updateCurrent() call inside the conditional block to only update when layout blocks should be updated.

  • Moved the master template data update to only execute when updateLayoutBlock is true

Copy link
Member

@leekelleher leekelleher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested out, works as described! 🚀

@leekelleher leekelleher merged commit 89989d6 into main Nov 10, 2025
35 checks passed
@leekelleher leekelleher deleted the v17/bugfix/new-template-discard-changes branch November 10, 2025 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

V16 Discard Changes dialog shown after creating a template using a master template

3 participants