Skip to content

Commit

Permalink
feat: fetch asset-metadata on select
Browse files Browse the repository at this point in the history
  • Loading branch information
luqven committed Jun 13, 2024
1 parent 45e9bf1 commit 9414af4
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/components/Gallery/ImageGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GridImage, GalleryPlaceholder } from './';

import { ActionBar } from '../ActionBar';
import './ImageGallery.css';
import { stringifyJsonFields } from '../../helpers/utils';

interface GalleryProps {
selectedSource: Partial<SourceProps>;
Expand All @@ -29,11 +30,32 @@ 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();
};

handleClick = (selectedAsset: AssetProps) => this.setState({ selectedAsset });

handleSubmit = () => {
handleSubmit = async () => {
if (!this.state.selectedAsset?.src) {
return;
}

// add metadata to selectedAsset attributes
const metadata = await this.getAssetMetadata(this.state.selectedAsset.src);
const selectedAsset = { ...this.state.selectedAsset };
const assetAttributes = stringifyJsonFields(
{ ...selectedAsset.attributes, ...metadata },
['custom_fields', 'tags', 'colors.dominant_colors'],
);
selectedAsset.attributes = assetAttributes;

this.props.sdk.close({
...this.state.selectedAsset,
...selectedAsset,
selectedSource: this.props.selectedSource,
});
};
Expand Down

0 comments on commit 9414af4

Please sign in to comment.