Skip to content

Commit

Permalink
ASSETS-34548 : Update the Collection API Endpoints (#39)
Browse files Browse the repository at this point in the history
* Check in working code with hardcoded collectionId urn:cid:aem:8f67c491-347a-4c50-a55b-aad555671b22
which is a collection that was created with the new api to create a collection
https://adobe-aem-assets-delivery-experimental.redoc.ly/#operation/createCollection

Once the List All Collections API is working can this code be updated to be completed

* In collection.js added new function export async function searchListCollection(limit = undefined, page = 0)

Pagination in files
/adobe-gmo/blocks/adp-infinite-results-collections/CollectionsDatasource.js

Needs to be fixed as not all the new Collections are showing up

* Fixed pagination logic for this.lastPage

* /blocks/adp-infinite-results-collection/CollectionDatasource.js => Pagination for the Collection Cards needs to be fixed
/scripts/collections.js :  Tested that the getCollection method only works with the new Collections created with the Create and Update Collection API calls  /adobe/assets/collections/{collectionId}/

* Updated deleteCollection function, added etag parameter as it required in the header in the new Assets Delete API

* Updated function patchCollection API URL from   ${getBaseAssetsCollectionsUrl()}/${collectionId} to ${getBaseAssetsCollectionsUrl()}/${collectionId}/items

* /scripts/collections.js ==> Updated function searchListCollection(limit = undefined, page = 0), added logic when the parameter page = null
to not add the page parameter in the data / body parameters

/blocks/adp-add-to-collection-modal/adp-add-to-collection-modal.js : Removed hard coded getCollection('urn:cid:aem:8f67c491-347a-4c50-a55b-aad555671b22') call to call the selected collection for add to an existing collection.

* Check in latest collection modal code

* Deleted/removed Caching for JSON data for when the app is run on localhost

* /scripts/scripts.js  Restored the caching of collection data for localhost
/scripts/collections.js : Removed deprecated listCollection function
/blocks/adp-add-to-collection-modal/adp-add-to-collection-modal.js : Removed all the commented out old code that called listCollections
/blocks/adp-add-to-collection-modal/adp-add-to-collection-modal.js : Removed all the commented out old code that called listCollections

* Added code to get the Search Collection Index from the  configuration file  admin-config.json
which is stored in a new property searchCollectionIndex e.g.
        {
            "ID": "searchCollectionIndex",
            "Value": "108396-1046543_collections"
        }

* Updated function loadMoreData to get all the collections to populate them in the Collection dropdown list.
Deleted the  dropdownSelect.addEventListener('scroll' .... as the <select> tag does not support the scroll event

* Deleted  event listener  dropdownSelect.addEventListener('scroll' .. as it not used.

* Fixed lint errors

* Pull Request code changes.

* PR review code change to remove redundant code

* Removed redundant if statement

* Removed      'x-ch-Request': 'search' from header of Search Collections Endpoint

* Replaced with 'x-api-key': getBackendApiKey(),

* Added      'x-adp-request': 'search' to the header for the Search Collections call
  • Loading branch information
TyroneAEM authored Mar 14, 2024
1 parent 3e13598 commit 94b1229
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 71 deletions.
37 changes: 9 additions & 28 deletions blocks/adp-add-to-collection-modal/adp-add-to-collection-modal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { decorateIcons } from '../../scripts/lib-franklin.js';
import {
listCollection, createCollection, patchCollection, getCollection,
searchListCollection, createCollection, patchCollection, getCollection,
} from '../../scripts/collections.js';
import { getSelectedAssetsFromInfiniteResultsBlock, populateAssetViewLeftDialog } from '../../scripts/scripts.js';
import createMultiSelectedAssetsTable from '../../scripts/multi-selected-assets-table.js';
Expand Down Expand Up @@ -43,38 +43,18 @@ async function createDropdown(addToExistingRadioDropboxContainer) {
dropdownSelect.classList.add('add-to-existing-dropdown'); // Add the new class

// Function to load more data when reaching the end of the dropdown
const loadMoreData = async (cursor) => {
const collectionData = await listCollection({ cursor, limit: 10 }); // Adjust the limit as needed
const loadMoreData = async (page) => {
// Call to get the nbHits for the total number of Collections
const collectionMax = await searchListCollection(0, 0);
// Get all the collections
const collectionData = await searchListCollection(collectionMax.nbHits, page);
return collectionData;
};

let cursor = null;

// Event listener to detect scroll and load more data when at the bottom
dropdownSelect.addEventListener('scroll', async () => {
if (dropdownSelect.scrollTop + dropdownSelect.clientHeight >= dropdownSelect.scrollHeight) {
const moreData = await loadMoreData(cursor);
if (moreData.items.length > 0) {
// Update the cursor for the next load
cursor = moreData.cursor;

// Populate the options in the dropdown from the additional data
moreData.items.forEach((collection) => {
const option = document.createElement('option');
option.value = collection.id;
option.textContent = collection.title;
dropdownSelect.appendChild(option);
});
}
}
});

const page = 0;
// Initial data loading
const initialData = await loadMoreData(cursor);
const initialData = await loadMoreData(page);
if (initialData.items.length > 0) {
// Update the cursor for the next load
cursor = initialData.cursor;

// Populate the options in the dropdown with the initial data
initialData.items.forEach((collection) => {
const option = document.createElement('option');
Expand Down Expand Up @@ -160,6 +140,7 @@ export async function openModal(items) {
const { etag } = collection;
patchCollection(collectionId, etag, payload);
});

resetDialogState();
}

Expand Down
2 changes: 1 addition & 1 deletion blocks/adp-collection-header/adp-collection-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function createCollectionInfoHeader(collectionInfoHeader, collection) {
'Delete collection',
`Are you sure you want to delete the collection "${collection.title}"?`,
async () => {
await deleteCollection(collectionId, collection.title);
await deleteCollection(collectionId, collection.title, collection.etag);
navigateTo(createLinkHref('/collections'));
},
'Proceed',
Expand Down
23 changes: 15 additions & 8 deletions blocks/adp-infinite-results-collections/CollectionsDatasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ import { createLinkHref, navigateTo } from '../../scripts/scripts.js';
import {
getCollectionID,
getCollectionTitle,
listCollection,
searchListCollection,
} from '../../scripts/collections.js';
import { createCollectionCardElement } from '../../scripts/card-html-builder.js';

export default class CollectionsDatasource {
cursor;

infiniteResultsContainer = null;

container = null;

pageNumber = 0;

lastPage = false;

async showMore() {
const list = await listCollection(5, this.cursor);
this.cursor = list.cursor;

const list = await searchListCollection(5, this.pageNumber);

this.pageNumber += 1;

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

this.infiniteResultsContainer.resultsCallback(
this.container,
list.items,
Expand All @@ -31,14 +37,15 @@ export default class CollectionsDatasource {
}

isLastPage() {
return !this.cursor;
return this.lastPage;
}

async registerResultsCallback(container, infiniteResultsContainer) {
this.infiniteResultsContainer = infiniteResultsContainer;
this.container = container;
const list = await listCollection(5, this.cursor);
this.cursor = list.cursor;

const list = await searchListCollection(5, this.pageNumber);

infiniteResultsContainer.resultsCallback(
container,
list.items,
Expand Down
Loading

0 comments on commit 94b1229

Please sign in to comment.