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

LAYOUTS-660 refresh component block after it is created in new window #3

Merged
merged 2 commits into from
Dec 8, 2022
Merged
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
50 changes: 50 additions & 0 deletions bundle/Resources/es6/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,53 @@ import 'bootstrap/js/src/modal';
import './location';

import '../sass/ibexa/admin/style.scss';

const handlePostRequest = async (id) => {
const data = localStorage.getItem(id);
if (!data) return;

const { blockId, contentId, locale } = JSON.parse(data);

const nglayoutsBasePath = document.querySelector('[name="nglayouts-base-path"]').getAttribute('content');
const url = `${nglayoutsBasePath}ibexa/admin/blocks/${blockId}/${locale}/connect-component-content/${contentId}`;
const bc = new BroadcastChannel('publish_content');

await fetch(url, { method: 'post' })
.then(() => {
bc.postMessage(JSON.parse(data));
})
.then(() => {
bc.close();
localStorage.removeItem(contentId);
});
};

const saveDataToLocalStorage = (pathname, hash) => {
if (!hash.includes('#ngl-component/')) return;

const params = hash.replace('#ngl-component/', '').split('/');
const blockId = params[0];
const locale = params[1];
const contentId = pathname.split('content/edit/draft/').splice(-1)[0].split('/')[0];

const data = { blockId, contentId, locale };
localStorage.setItem(contentId, JSON.stringify(data));
};

const connectBlockAndContent = async () => {
const urlPathname = window.location.pathname;
const urlHash = window.location.hash;

if (urlPathname.includes('view/content/') && urlPathname.includes('full')) {
const contentId = urlPathname.split('content/').splice(-1)[0].split('/')[0];

handlePostRequest(contentId);
} else {
saveDataToLocalStorage(urlPathname, urlHash);
}
};

// https://layouts-enterprise-bold-ms.dev9.netgen.biz/adminui/view/content/597/full/1/465#ngl-component/689ce6cf-5bb8-4574-9b11-8d3a790bd154
Copy link
Member

Choose a reason for hiding this comment

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

Please remove this commented URL :)

window.addEventListener('DOMContentLoaded', () => {
connectBlockAndContent();
});