Skip to content

Commit

Permalink
fix: dont stringify values that are already strings
Browse files Browse the repository at this point in the history
  • Loading branch information
luqven committed Aug 26, 2024
1 parent 4e2d0b3 commit 10e8758
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/components/Gallery/ImageGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ export class Gallery extends Component<GalleryProps, GalleryState> {
/**
* Fetches an Object of asset metadata, like width and height attributes.
*/
getAssetMetadata = async (assetURL: string) => {
const response = await fetch(`${assetURL.split('?')[0]}?fm=json`);
return await response.json();
getAssetMetadata = async (assetURL: string): Promise<Record<string, any>> => {
try {
const response = await fetch(`${assetURL.split('?')[0]}?fm=json`);
return await response.json();
} catch (error) {
return {};
}
};

handleClick = (selectedAsset: AssetProps) => this.setState({ selectedAsset });
Expand All @@ -50,19 +54,23 @@ export class Gallery extends Component<GalleryProps, GalleryState> {
const selectedAsset = { ...this.state.selectedAsset };

if (!selectedAsset.attributes.media_width) {
selectedAsset.attributes.media_width = metadata.PixelWidth;
selectedAsset.attributes.media_width = metadata?.PixelWidth || '';
}

if (!selectedAsset.attributes.media_height) {
selectedAsset.attributes.media_height = metadata.PixelHeight;
selectedAsset.attributes.media_height = metadata?.PixelHeight || '';
}

this.props.sdk.close({
const stringifiedAsset = {
...stringifyJsonFields(selectedAsset, [
'attributes.custom_fields',
'attributes.tags',
'attributes.colors.dominant_colors',
]),
};

this.props.sdk.close({
...stringifiedAsset,
selectedSource: this.props.selectedSource,
});
};
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ export const stringifyJsonFields = (

for (const field of fields) {
const value = _.get(object, field);
const newValue = JSON.stringify(value, replaceNullWithEmptyString);
_.set(modifiedObject, field, newValue);
if (typeof value !== 'string') {
const newValue = JSON.stringify(value, replaceNullWithEmptyString);
_.set(modifiedObject, field, newValue);
}
}

return modifiedObject;
Expand Down

0 comments on commit 10e8758

Please sign in to comment.