Skip to content

Commit

Permalink
Refactored code; no results is displayed if there are no fetched card…
Browse files Browse the repository at this point in the history
…s; added portal and component specific collectionTags
  • Loading branch information
draganalilly021 committed Jun 12, 2024
1 parent 946c726 commit 0db833b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export default async function init(el) {
'localizedText': localizedText,
'tableData' : el.children,
'cardsPerPage': 9,
'ietf': config.locale.ietf
'ietf': config.locale.ietf,
'collectionTags': 'caas:adobe-partners/collections/knowledge-base'
}

const app = document.createElement('knowledge-base-overview');
Expand Down
3 changes: 2 additions & 1 deletion eds/blocks/partner-news/partner-news.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export default async function init(el) {
'tableData' : el.children,
'dateFilter': dateFilter,
'cardsPerPage': 12,
'ietf': config.locale.ietf
'ietf': config.locale.ietf,
'collectionTags': 'caas:adobe-partners/collections/news'
}

const app = document.createElement('partner-news');
Expand Down
79 changes: 45 additions & 34 deletions eds/components/PartnerCards.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export class PartnerCards extends LitElement {
this.selectedSortOrder = {};
this.selectedFilters = {};
this.urlSearchParams = {};
this.collectionTags = [];
this.hasResponseData = false;
this.mobileView = window.innerWidth <= 1200;
this.updateView = this.updateView.bind(this);
}
Expand All @@ -108,11 +110,12 @@ export class PartnerCards extends LitElement {
'default': {},
items: []
},
'collectionTags': '',
'language': '',
'country': ''
};

this.collectionTags = [ this.blockData.collectionTags ];

const blockDataActions = {
'title': (cols) => {
const [titleEl] = cols;
Expand Down Expand Up @@ -162,8 +165,7 @@ export class PartnerCards extends LitElement {
'collection-tags': (cols) => {
const [collectionTagsEl] = cols;
const collectionTags = Array.from(collectionTagsEl.querySelectorAll('li'), (li) => li.innerText.trim().toLowerCase());
const collectionTagsParamStr = collectionTags.filter(e => e.length).join(',');
this.blockData.collectionTags = collectionTagsParamStr;
this.collectionTags = [...this.collectionTags, ...collectionTags];
}
}

Expand All @@ -173,7 +175,7 @@ export class PartnerCards extends LitElement {
const rowTitle = cols[0].innerText.trim().toLowerCase().replaceAll(' ', '-');
const colsContent = cols.slice(1);
if (blockDataActions[rowTitle]) blockDataActions[rowTitle](colsContent);
})
});

const ietfArr = this.blockData.ietf.split('-');
this.blockData.language = ietfArr[0];
Expand All @@ -196,24 +198,8 @@ export class PartnerCards extends LitElement {
async fetchData() {
try {
const api = new URL('https://14257-chimera.adobeioruntime.net/api/v1/web/chimera-0.0.1/collection?originSelection=dx-partners&draft=false&debug=true&flatFile=false&expanded=true');

const { collectionTags, language, country } = this.blockData;

if(collectionTags) {
api.searchParams.set('collectionTags', collectionTags);
}

const partnerDataComplexQueryParam = this.getPartnerDataComplexQueryParam();
if(partnerDataComplexQueryParam) {
api.searchParams.set('complexQuery', partnerDataComplexQueryParam);
}

if (language && country) {
api.searchParams.set('language', language);
api.searchParams.set('country', country);
}

const response = await fetch(api.toString());
const apiWithParams = this.setApiParams(api);
const response = await fetch(apiWithParams);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
Expand All @@ -227,27 +213,52 @@ export class PartnerCards extends LitElement {
apiData.cards.forEach((card, index) => card.orderNum = index + 1);
this.allCards = this.cards = apiData.cards;
this.paginatedCards = this.cards.slice(0, this.cardsPerPage);
this.hasResponseData = true;
} else {
this.hasResponseData = false;
}
} catch (error) {
console.error('Error fetching data:', error);
}
}

getPartnerDataComplexQueryParam () {
setApiParams(api) {
const portal = this.getProgramType(window.location.pathname);

if (portal) {
const portalCollectionTag = `caas:adobe-partners/${portal}`;
if (!this.collectionTags.length || !this.collectionTags.includes(portalCollectionTag)) {
this.collectionTags = [...this.collectionTags, portalCollectionTag];
}

const partnerDataComplexQueryParam = this.getPartnerDataComplexQueryParam(portal);
if (partnerDataComplexQueryParam) api.searchParams.set('complexQuery', partnerDataComplexQueryParam);
}

if(this.collectionTags.length) {
const collectionTagsStr = this.collectionTags.filter(e => e.length).join(',');
api.searchParams.set('collectionTags', collectionTagsStr);
}

const { language, country } = this.blockData;
if (language && country) {
api.searchParams.set('language', language);
api.searchParams.set('country', country);
}

return api.toString();
}

getPartnerDataComplexQueryParam (portal) {
try {
const portal = this.getProgramType(window.location.pathname);
let cookies = document.cookie.split(';').map(cookie => cookie.trim());
let partnerDataCookie = cookies.find(cookie => cookie.startsWith('partner_data='));
const cookies = document.cookie.split(';').map(cookie => cookie.trim());
const partnerDataCookie = cookies.find(cookie => cookie.startsWith('partner_data='));
if (!partnerDataCookie) return `(("caas:adobe-partners/${portal}/partner-level/public"))`;

if (!partnerDataCookie) return `(("caas:adobe-partners/${portal}/partner-level/public")OR(""))`;
const {level} = JSON.parse(partnerDataCookie.substring(('partner_data=').length).toLowerCase());
if(!portal)
return '';

if (!level)
return `(("caas:adobe-partners/${portal}/partner-level/public")OR(""))`;
if (!level) return `(("caas:adobe-partners/${portal}/partner-level/public"))`;

return `(("caas:adobe-partners/${portal}/partner-level/${level}")OR("caas:adobe-partners/${portal}/partner-level/public"))`;
return `(("caas:adobe-partners/${portal}/partner-level/${level}")+OR+("caas:adobe-partners/${portal}/partner-level/public"))`;
} catch(error) {
console.error('Error parsing partner data object:', error);
return '';
Expand Down Expand Up @@ -721,7 +732,7 @@ export class PartnerCards extends LitElement {
</div>
</div>
<div class="partner-cards-collection">
${this.allCards.length
${this.hasResponseData
? this.partnerCards
: html`
<div class="progress-circle-wrapper">
Expand Down

0 comments on commit 0db833b

Please sign in to comment.