Skip to content

Commit

Permalink
MWPW-155065 PartnerCards fallback for invalid thumbnail images
Browse files Browse the repository at this point in the history
- checks news card background image on first updated
- replace with a hardcoded default image path
- changes the background color of the card header to prevent FOUC
  • Loading branch information
richardhand committed Jul 25, 2024
1 parent 78e8592 commit 410ca6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 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,26 @@ class NewsCard extends LitElement {
return newUrl;
}

checkBackgroundImage(element) {
const style = window.getComputedStyle(element);
const url = style.backgroundImage.slice(5, -2);
const img = new Image();

const isProd = prodHosts.includes(window.location.host);
const defaultBackgroundImageOrigin = `https://solutionpartners.${isProd ? '' : 'stage2.'}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
2 changes: 1 addition & 1 deletion edsdme/components/PartnerCardsStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ export const newsCardStyles = css`
.news-card .card-header {
min-height: 192px;
max-height: 192px;
background-color: #323232;
background-color: #f5f5F5;
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: cover;
Expand Down

0 comments on commit 410ca6c

Please sign in to comment.