sanity-io / sanity-plugin-internationalized-array

A plugin to register array fields with a custom input component to store field values in multiple languages, queryable by using the language ID as an array `_key`.
MIT License
41 stars 10 forks source link

`defaultLanguages` option causes weird behaviours (duplicate keys, unable to delete documents) #47

Closed vntw closed 4 months ago

vntw commented 9 months ago

Hey there,

thanks for the plugin, it worked nicely while not using the defaultLanguages config option. 😅

Issues

When using it, I encountered some problems:

Issue 1 - Duplicate keys

Creating a new document via the UI and with the option configured, this is the result for all translated fields: image

Looking at the data (*[_type == "project"] { translatedString1, translatedString2, translatedStringText } - same for every field):

"translatedString1": [
    {
        "_key": "en",
        "_type": "internationalizedArrayStringValue"
    },
    {
        "_type": "internationalizedArrayStringValue",
        "_key": "en"
    }
],

When creating a document, there are two POST requests

https://<id>.api.sanity.io/v2022-09-09/data/mutate/production?tag=sanity.studio.document.commit&returnIds=true&visibility=async&skipCrossDatasetReferenceValidation=true

containing sth. like the snippet below, so makes sense that keys are duplicated (I also observed the same field being patched twice in the same request, e.g. translatedString2[-1]):

{
    "patch": {
        "insert": {
            "after": "translatedString2[-1]",
            "items": [
                {
                    "_type": "internationalizedArrayStringValue",
                    "_key": "en"
                }
            ]
        },
        "id": "drafts.<same-id>"
    }
},
{
    "patch": {
        "insert": {
            "after": "translatedString2[-1]",
            "items": [
                {
                    "_type": "internationalizedArrayStringValue",
                    "_key": "en"
                }
            ]
        },
        "id": "drafts.<same-id>"
    }
},

I can generate unique keys and fix the "deduped" key to be of a valid language via the UI, but… for now I'll go without the config option and having to click the languages every time

Issue 2 - Document deletion not possible

Deleting a document in this state is not possible, the delete request gets aborted and a POST request with createIfNotExists for every translated field follows. Removing the defaultLanguages option will delete it successfully. I know there were issues before that covered this, but it's still happening for me (in this state)

Reproduction

I created a reproduction repo based on this template with a few added fields in the project document where this behaviour should be observable (it is for me every time, using Firefox). You should only have to enter valid API credentials.

Using the latest version of the plugin (1.10.3). If you have any further questions, let me know!

Thanks for your input!

mateusriff commented 8 months ago

I'm also facing this exact issue. It does seem to be a problem with the plugin.

kiurious commented 8 months ago

Hi! I am having the same problem. I've been having this for some time and ignoring, but now that is time to deliver the project, while bug hunting I found this thread.

This is the schema:

export default defineType({
  name: "pages",
  type: "document",
  title: "Pages",
  fields: [
    defineField({
      name: "title",
      type: "internationalizedArrayString",
      title: "Title",
    }),
    defineField({
      name: "slug",
      type: "slug",
      title: "Slug",
      description: "This is used to generate the URL for this category",
      validation: (Rule) => Rule.required(),
      options: {
        source: "name",
        maxLength: 44,
      },
    }),
],
  preview: {
    select: {
      title: "title",
    },
    prepare(selection) {
      const { title } = selection;
      return {
        ...selection,
        title: title[1].value || "Untitled",
      };
    },
  },
});

And this is my config:


internationalizedArray({
      languages: [
        { id: "el", title: "Greek" },
        { id: "en", title: "English" },
      ],
      defaultLanguages: ["en"],
      fieldTypes: ["string"],
    }), ```

I tried adding a _key to the schema fields but it was not recognized. Perhaps I was doing it wrong but I couldn't find a way to add it manually. 
Any help would be appreciated
bllfoad commented 6 months ago

any one found the solution for this?

paoyangt commented 4 months ago

Issue is still present to this day

jprinsen commented 4 months ago

any one found the solution for this?

Could this be somehow related to having two language-files in the setup, e.g. languages.js and languages.json?

edit; unfortunately not the solution; issue still present this day

kiurious commented 4 months ago

Didn't find a solution. I just ended up removing the defaultLanguages from the config and moved on with the project in other ways.

jprinsen commented 4 months ago

Didn't find a solution. I just ended up removing the defaultLanguages from the config and moved on with the project in other ways.

Yes, seems to be the only option left on the table. Thanks for letting us know.