Skip to content
Closed
Changes from 1 commit
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
67 changes: 54 additions & 13 deletions blocks/edit/da-assets/da-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ const { loadStyle } = await import(`${getNx()}/scripts/nexter.js`);
const { loadIms, handleSignIn } = await import(`${getNx()}/utils/ims.js`);
const loadScript = (await import(`${getNx()}/utils/script.js`)).default;

const ASSET_SELECTOR_URL = 'https://experience.adobe.com/solutions/CQ-assets-selectors/assets/resources/assets-selectors.js';
const ASSET_SELECTOR_URL =
'https://experience.adobe.com/solutions/CQ-assets-selectors/assets/resources/assets-selectors.js';

const CONFS = {};

async function getMergedConf(owner, repo) {
return mergedConf;
}

async function fetchConf(path) {
if (CONFS[path]) return CONFS[path];
const resp = await daFetch(`${DA_ORIGIN}/config${path}`);
Expand All @@ -27,7 +32,29 @@ async function fetchConf(path) {
async function fetchValue(path, key) {
if (CONFS[path]?.[key]) return CONFS[path][key];

const data = await fetchConf(path);
const [repoConf, orgConf] = await Promise.all([
fetchConf(`/${owner}/${repo}/`),
fetchConf(`/${owner}/`),
]);

const data = null;

// Start with organization-level config
if (orgConf) {
data = {};
orgConf.forEach((item) => {
data[item.key] = item.value;
});
}

// Override with repository-level config
if (repoConf) {
if (data === null) data = {};
repoConf.forEach((item) => {
data[item.key] = item.value;
});
}

if (!data) return null;

const confKey = data.find((conf) => conf.key === key);
Copy link
Contributor

Choose a reason for hiding this comment

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

data was an array previously, and is now an object which means that the data.find will fail on line 60. Now that it's an object, you can just do a lookup

Suggested change
const data = null;
// Start with organization-level config
if (orgConf) {
data = {};
orgConf.forEach((item) => {
data[item.key] = item.value;
});
}
// Override with repository-level config
if (repoConf) {
if (data === null) data = {};
repoConf.forEach((item) => {
data[item.key] = item.value;
});
}
if (!data) return null;
const confKey = data.find((conf) => conf.key === key);
const data = {};
// Start with organization-level config
orgConf?.forEach((item) => {
data[item.key] = item.value;
});
// Override with repository-level config
repoConf?.forEach((item) => {
data[item.key] = item.value;
});
return data[key];

Copy link
Author

Choose a reason for hiding this comment

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

I will be updating the PR to treat data as an array to retain the expected data type of data variable. Pushing objects to an array and updating values inside them if the keys match afterwards, is my approach.

Expand All @@ -39,23 +66,29 @@ async function fetchValue(path, key) {
export async function getConfKey(owner, repo, key) {
if (!(repo || owner)) return null;
let value = await fetchValue(`/${owner}/${repo}/`, key);
if (!value) value = await fetchValue(`/${owner}/`, key);

// Turned off check for organization level value update as new fetchValue does that internally.
// if (!value) value = await fetchValue(`/${owner}/`, key);

return value;
}

export async function openAssets() {
const details = await loadIms();
if (details.anonymous) handleSignIn();
if (!(details.accessToken)) return;
if (!details.accessToken) return;

const { owner, repo } = getPathDetails();
const repoId = await getConfKey(owner, repo, 'aem.repositoryId');

// Determine publicly available asset origin
const prodOrigin = await getConfKey(owner, repo, 'aem.assets.prod.origin') || `${repoId.replace('author', 'publish')}`;
const prodOrigin =
(await getConfKey(owner, repo, 'aem.assets.prod.origin')) ||
`${repoId.replace('author', 'publish')}`;

// Determine if images should be links
const injectLink = (await getConfKey(owner, repo, 'aem.assets.image.type')) === 'link';
const injectLink =
(await getConfKey(owner, repo, 'aem.assets.image.type')) === 'link';

let dialog = document.querySelector('.da-dialog-asset');
if (!dialog) {
Expand All @@ -79,7 +112,9 @@ export async function openAssets() {
imsToken: details.accessToken.token,
repositoryId: repoId,
aemTierType,
onClose: () => { dialog.close(); },
onClose: () => {
dialog.close();
},
handleSelection: (assets) => {
const [asset] = assets;
if (!asset) return;
Expand All @@ -91,12 +126,18 @@ export async function openAssets() {
dialog.close();

// eslint-disable-next-line no-underscore-dangle
const alt = asset?._embedded?.['http://ns.adobe.com/adobecloud/rel/metadata/asset']?.['dc:description'];

const src = aemTierType === 'author'
? `https://${prodOrigin}${path}`
// eslint-disable-next-line no-underscore-dangle
: asset._links['http://ns.adobe.com/adobecloud/rel/rendition'][0].href.split('?')[0];
const alt =
asset?._embedded?.[
'http://ns.adobe.com/adobecloud/rel/metadata/asset'
]?.['dc:description'];

const src =
aemTierType === 'author'
? `https://${prodOrigin}${path}`
: // eslint-disable-next-line no-underscore-dangle
asset._links[
'http://ns.adobe.com/adobecloud/rel/rendition'
][0].href.split('?')[0];

const imgObj = { src, style: 'width: 180px' };
if (alt) imgObj.alt = alt;
Expand Down