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
43 stars 11 forks source link

Is it possible to use Sanity client in asynchronous function for loading language list? #6

Closed alexanderkalachev closed 1 year ago

alexanderkalachev commented 1 year ago

If yes, what would be the function? I'm trying something like:

async () => {
    client.fetch(query, params)
        .then(data => {
            const result = data[0].languages;
            return result;
        });
}

But it return a promise as a language list instead of an array.

SimeonGriggs commented 1 year ago

It is, in the section about loading languages there is an async example. The function here actually has the client as the first param so it could be:

languages: async (client) => {
  const response = await client.fetch(`*[_type == "language"]{ id, title }`)
  return response
}
alexanderkalachev commented 1 year ago

I tried this:

plugins: [
      deskTool(),
      visionTool(),
      internationalizedArray({
          languages: async (client) => {
              const response = await client.fetch(`*[_type == "language"]{ id, title }`);
              return response;
          },
          fieldTypes: ['string']
      })
  ],

and still having the same error:

Unhandled Runtime Error
TypeError: k.filter is not a function

Call Stack
U
node_modules/sanity-plugin-internationalized-array/lib/index.esm.js (1:3047)
renderWithHooks
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (18082:0)
mountIndeterminateComponent
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (23209:0)
beginWork
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (24786:0)
HTMLUnknownElement.callCallback
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (4246:0)
Object.invokeGuardedCallbackDev
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (4295:0)
invokeGuardedCallback
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (4359:0)
beginWork$1
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (32207:0)
performUnitOfWork
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (31082:0)
workLoopSync
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (30891:0)
renderRootSync
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (30856:0)
performSyncWorkOnRoot
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (30368:0)
flushSyncCallbacks
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (14254:0)
eval
node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (29896:0)
SimeonGriggs commented 1 year ago

I have updated the readme example in #11, re-open this if you still cannot get this feature to work.