diff --git a/src/modules/assets/index.ts b/src/modules/assets/index.ts index 45d1b32a..84f2c206 100644 --- a/src/modules/assets/index.ts +++ b/src/modules/assets/index.ts @@ -1,5 +1,5 @@ import {createSelector, createSlice, PayloadAction} from '@reduxjs/toolkit' -import type {ClientError, Patch, Transaction} from '@sanity/client' +import type {AttributeSet, ClientError, Patch, Transaction} from '@sanity/client' import { Asset, AssetItem, @@ -814,7 +814,10 @@ export const assetsUpdateImageReferencesEpic: MyEpic = (action$, state$, {client const clonedDocument = JSON.parse(JSON.stringify(document)) const assetsToReplace = findImageAssets(clonedDocument, asset, id) for (const assetToReplace of assetsToReplace) { - await client.patch(document._id).set(assetToReplace).commit() + await client + .patch(document._id) + .set(assetToReplace as AttributeSet) + .commit() } } return assetsActions.updateImageReferencesComplete({id}) diff --git a/src/utils/ReplaceImages.ts b/src/utils/ReplaceImages.ts index 06a89e22..20f91039 100644 --- a/src/utils/ReplaceImages.ts +++ b/src/utils/ReplaceImages.ts @@ -1,27 +1,29 @@ -export function findImageAssets( - document: Record, - newAsset: Record, +import {Asset} from '@types' + +export function findImageAssets( + document: T, + newAsset: T, assetToReplaceId: string -): any[] { - const foundEntries: any[] = [] - findNestedObjects(document, foundEntries, newAsset, assetToReplaceId, '') +): T[] { + const foundEntries: T[] = [] + findNestedObjects(document, foundEntries, newAsset, assetToReplaceId) return foundEntries } // Because of GROQ limitations we have to filter all image assets out the document manually -function findNestedObjects( - document: any, - foundEntries: any[], - newAsset: any, +function findNestedObjects( + document: T, + foundEntries: T[], + newAsset: T, assetToReplaceId: string, - currentPath: string + currentPath: string = '' ) { if (typeof document !== 'object' || document === null) { return } - if (document.hasOwnProperty('_type') && document._type === 'image') { - const assetProperty = document.asset + if (document.hasOwnProperty('_type') && document._type === 'image' && document.asset) { + const assetProperty = document.asset as Asset if (assetProperty.hasOwnProperty('_ref') && assetProperty._ref === assetToReplaceId) { const imageObject: any = {} document.asset._ref = newAsset._id @@ -36,7 +38,7 @@ function findNestedObjects( for (const key in document) { if (typeof document[key] === 'object') { const newPath = currentPath ? `${currentPath}` : key - findNestedObjects(document[key], foundEntries, newAsset, assetToReplaceId, newPath) + findNestedObjects(document[key] as T, foundEntries, newAsset, assetToReplaceId, newPath) } } }