Skip to content

Commit

Permalink
feat: grab state from dom for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Apr 14, 2023
1 parent 83f6146 commit 1bfdbbc
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions client/src/server/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ export default class Connection {
uiData: Record<string, any>,
dom: typeof Dom
) {
const component: Element = uiData.element.closest('[ui-state]');
const componentData = component.getAttribute('ui-state') ?? '{}';
const pageState: Record<string, any> = {};
const component: HTMLElement = uiData.element.closest('[ui-state]');
const componentData = JSON.parse(component?.getAttribute('ui-state') ?? '{}');
const components = document.querySelectorAll('[ui-state]');

components.forEach((i) => {
const attr = JSON.parse(i.getAttribute('ui-state') ?? '{}');
pageState[attr.key] = attr;
});

const payload = {
type,
payload: {
params: [],
method: uiData.method,
methodArgs: uiData.methodArgs,
data: componentData,
component: componentData?.key,
data: pageState,
}
};

Expand Down Expand Up @@ -47,7 +55,12 @@ export default class Connection {
response.text().then(response => {
const data = JSON.parse(response);
window._leafUIConfig.data = data.state;
dom.diff(data.html, document.body!);
dom.diff(
data.html,
component.nodeName === 'HTML' || !component
? document.body!
: component
);
});
} else {
error(await response.text().then(res => res));
Expand Down

0 comments on commit 1bfdbbc

Please sign in to comment.