Skip to content

Commit

Permalink
Merge pull request #28 from adobecom/mwpw-155065-invalid-thumbnail-fa…
Browse files Browse the repository at this point in the history
…llback

MWPW-155065 PartnerCards fallback for invalid thumbnail images
  • Loading branch information
richardhand-fortedigital authored Jul 29, 2024
2 parents 78e8592 + e75ab54 commit c1a8072
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion edsdme/components/NewsCard.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { newsCardStyles } from './PartnerCardsStyles.js';
import { formatDate, getLibs } from '../scripts/utils.js';
import { formatDate, getLibs, prodHosts } from '../scripts/utils.js';

const miloLibs = getLibs();
const { html, LitElement } = await import(`${miloLibs}/deps/lit-all.min.js`);

const DEFAULT_BACKGROUND_IMAGE_PATH = '/content/dam/solution/en/images/card-collection/sample_default.png';

class NewsCard extends LitElement {
static properties = { data: { type: Object } };

Expand All @@ -24,6 +26,25 @@ class NewsCard extends LitElement {
return newUrl;
}

checkBackgroundImage(element) {
const url = this.data.styles?.backgroundImage;
const img = new Image();

const isProd = prodHosts.includes(window.location.host);
const defaultBackgroundImageOrigin = `https://partners.${isProd ? '' : 'stage.'}adobe.com`;
const defaultBackgroundImageUrl = `${defaultBackgroundImageOrigin}${DEFAULT_BACKGROUND_IMAGE_PATH}`;

img.onerror = () => {
element.style.backgroundImage = `url(${defaultBackgroundImageUrl})`;
};

img.src = url;
}

firstUpdated() {
this.checkBackgroundImage(this.shadowRoot.querySelector('.card-header'));
}

render() {
return html`
<div class="news-card">
Expand Down

0 comments on commit c1a8072

Please sign in to comment.