Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASSETS-88896 : (Backend) Campaign Details: Collections #58

Open
wants to merge 5 commits into
base: assets-12024
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { createLinkHref, navigateTo } from '../../scripts/shared.js';
import {
getCollectionID,
getCollectionTitle,
listCampaignCollections,
} from '../../scripts/collections.js';

import { createCollectionCardElement } from '../../scripts/card-html-builder.js';

export default class CollectionsDatasource {
infiniteResultsContainer = null;

container = null;

pageNumber = 0;

lastPage = false;

async showMore() {

this.pageNumber += 1;

//Todo Pass in the campaignName
const list = await listCampaignCollections('gmo-campaignName:Everyone Can',pageNumber);

if (this.pageNumber >= list.nbPages){
this.lastPage = true;
}

this.infiniteResultsContainer.resultsCallback(
this.container,
list.items,
() => this.showMore(),
this.pageNumber,
false,
false,
() => this.isLastPage(),
);
}

isLastPage() {
return this.lastPage;
}

async registerResultsCallback(container, infiniteResultsContainer) {
this.infiniteResultsContainer = infiniteResultsContainer;
this.container = container;

//Todo Pass in the campaignName
const list = await listCampaignCollections('gmo-campaignName:Everyone Can',this.pageNumber);

infiniteResultsContainer.resultsCallback(
container,
list.items,
() => this.showMore(),
0,
true,
true,
() => this.isLastPage(),
);
}

getItemId(resultItem) {
return getCollectionID(resultItem);
}

getItemName(resultItem) {
return getCollectionTitle(resultItem);
}

createItemElement(item, infiniteResultsContainer) {
const id = getCollectionID(item);
const card = createCollectionCardElement(
item,
{
selectItemHandler: () => {
infiniteResultsContainer.toggleSelection(id);
},
deselectItemHandler: () => {
infiniteResultsContainer.deselectItem(id);
},
},
);
return card;
}

onItemSelected(itemElement, itemId) {
navigateTo(createLinkHref(`collection/${itemId}`));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.adp-infinite-results-collections .metadata-fields .metadata-row {
grid-column: span 2;
}

.adp-infinite-results-collections .metadata-fields .label {
color: var(--metadata-label-color);
font: var(--metadata-label-font);
margin-bottom: 4px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import InfiniteResultsContainer from '../../scripts/infinite-results/InfiniteResultsContainer.js';
// eslint-disable-next-line import/no-unresolved
import CollectionsDatasource from './CollectionsDatasource.js';

export default async function decorate(block) {
const instantSearchDatasource = new CollectionsDatasource();
const infiniteResultsContainer = new InfiniteResultsContainer(block, instantSearchDatasource);
infiniteResultsContainer.render();
}
1 change: 1 addition & 0 deletions contenthub/hydration/hydration-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export function getMetadataSchema(facetOptions){
{ name: 'Digital Editions', id: 'digital-editions' },
{ name: 'Dreamweaver', id: 'dreamweaver' },
{ name: 'Fill Sign', id: 'fill-sign' },
{ name: 'Firefly', id: 'firefly' },
{ name: 'Frame.io', id: 'frame-io' },
{ name: 'Fresco', id: 'fresco' },
{ name: 'Http Dynamic Streaming', id: 'http-dynamic-streaming' },
Expand Down
98 changes: 96 additions & 2 deletions scripts/collections.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getBearerToken } from './security.js';
import {
getSearchIndex,
getAssetHandlerApiKey,
getDeliveryEnvironment,
getBackendApiKey,
Expand Down Expand Up @@ -98,8 +99,6 @@ export function getSearchCollectionsUrl() {
return `${getDeliveryEnvironment()}/adobe/assets/search`;
}



/**
* Constructs and returns the base URL for assets collections. (New)
*
Expand Down Expand Up @@ -284,6 +283,101 @@ export async function searchListCollection(limit = undefined, page = 0) {
}
}


/**
* List Campaign Collections with required campaignName, indexName and page parameters.
*
* @param {String} - Campaign Name
* @param {String} - The Index Name
* @param {Number} - Page Number
* @returns {Promise<object>} A promise that resolves with a list of collections.
* @throws {Error} If an HTTP error or network error occurs.
*/
export async function listCampaignCollections(campaignName, page = 0) {

// Construct the query parameters
const queryParams = new URLSearchParams();

const indexName = getSearchIndex();

if (campaignName) {
queryParams.append('campaignName', campaignName);
}

if (indexName) {
queryParams.append('indexName', indexName);
}

if (page) {
queryParams.append('page', page);
}

const data = {
requests: [
{
indexName: indexName,
params: {
facetFilters: [
campaignName
],
highlightPostTag: '__/ais-highlight__',
highlightPreTag: '__ais-highlight__',
hitsPerPage: 40,
maxValuesPerFacet: 20,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TyroneAEM can you remove maxValuesPerFacet attribute. This is not needed

page: page,
query: '',
tagFilters: ''
}
}
]
};

const options = {
method: 'POST',
headers: await getRequestHeadersSearchCollections(),
body: JSON.stringify(data),
};

// Include the query parameters in the URL
const queryString = queryParams.toString();
//Uses Search Collections URL
const url = `${getSearchCollectionsUrl()}${queryString ? `?${queryString}` : ''}`;

try {
const response = await fetch(url, options);
// Handle response codes
if (response.status === 200) {
// Collection retrieved successfully
const responseBody = await response.json();
const hits = responseBody.results[0].hits;
const uniqueCollectionIds = Array.from(new Set(hits.reduce((acc, {collectionIds}) => [...acc, ...collectionIds], [])));

let collections = [];
for (const collectionId of uniqueCollectionIds) {
//collectionId is missing urn:cid:aem:
const fixedCollectionId = `urn:cid:aem:${collectionId}`;
const collectionBody = await getCollection(fixedCollectionId);
collections.push({id:collectionBody.self[0].id, title:collectionBody.self[0].collectionMetadata.title, description:collectionBody.self[0].collectionMetadata.description});
}

const transformedData = {
page: responseBody.results[0].page,
nbHits: uniqueCollectionIds.length,
nbPages: 1,
items: collections
};

return transformedData;
}
// Handle other response codes
throw new Error(`Failed to search campaign collection: ${response.status} ${response.statusText}`);
} catch (error) {
logError('listCampaignCollection', error);
throw error;
}
}


/**
* Updates a collection using JSON patch operations.
*
Expand Down
Loading