decaporg / decap-cms

A Git-based CMS for Static Site Generators
https://decapcms.org
MIT License
17.96k stars 3.05k forks source link

i18n Relation Widget Causing "Unsaved Changes" Error in Posts Collection #7276

Open 181221 opened 2 months ago

181221 commented 2 months ago

Describe the bug I'm experiencing an issue with the i18n setup for relation widgets in Decap CMS. When using the relation widget with i18n across multiple locales, the CMS consistently shows a "You have unsaved changes" warning whenever I navigate away from a post, even if no changes were made.

To Reproduce

Steps to reproduce the behavior

  1. Set up Decap CMS with the provided config.yml using i18n with multiple locales.
  2. Create an author and a post, assigning the author using the relation widget.
  3. Try navigating away from the page without making changes. The CMS shows the "unsaved changes" warning, despite no changes being made.

Expected behavior The CMS should not display an 'unsaved changes' warning when no actual changes have been made.

Applicable Versions:

CMS configuration

# Quick example to reproduce
local_backend: true
backend:
  name: git-gateway
  branch: main
  repo: "username/repo"
media_folder: /public/media
public_folder: /media
i18n:
  structure: multiple_files
  locales: [no, en]
  default_locale: no
collections:
  - name: "authors"
    label: "Authors"
    folder: "content/authors"
    create: true
    slug: "{{fields.uuid}}"
    i18n: { structure: single_file, locales: [no, en], default_locale: no }
    fields:
      - label: "First Name"
        name: "first_name"
        widget: "string"
        i18n: true
      - label: "UUID"
        name: "uuid"
        widget: "hidden"
        i18n: "duplicate"

  - name: "posts"
    label: "Posts"
    folder: "content/posts"
    create: true
    slug: "{{fields.uuid}}"
    i18n: { structure: single_file, locales: [no, en], default_locale: no }
    fields:
      - label: "Title"
        name: "title"
        widget: "string"
        i18n: true
      - label: "Author"
        name: "author"
        widget: "relation"
        collection: "authors"
        search_fields: ["first_name"]
        value_field: "uuid"
        display_fields: ["first_name"]
        i18n: "duplicate"
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="robots" content="noindex" />
    <title>Content Manager</title>
  </head>
  <body>
    <!-- Include the script that builds the page and powers Decap CMS -->
    <script src="https://unpkg.com/decap-cms@^3.0.0/dist/decap-cms.js"></script>
    <script>
      CMS.registerEventListener({
        name: "preSave",
        handler: ({ entry }) => {
          const data = entry.toJS().data;
          let uuid = data.uuid || crypto.randomUUID();
          return entry.withMutations((m) => {
            // Update the default locale UUID
            m.setIn(["data", "uuid"], uuid);
            // Iterate over remaining locales and update the UUID in each nested map
            m.get("i18n").forEach((value, key) => {
              m.setIn(["i18n", key, "data", "uuid"], uuid);
            });
          });
        },
      });
      console.log("custom preSave-hook loaded");
    </script>
  </body>
</html>
LHSnow commented 2 months ago

This also affects gitlab, which creates empty merge requests if the user actually saves the non-changes. #7142