-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to extract the logic to render the content of the dialog and the error handing into a separate component.
- Loading branch information
1 parent
6c91efd
commit c4413e7
Showing
4 changed files
with
64 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class RemotePartial extends HTMLElement { | ||
constructor() { | ||
super() | ||
this.addEventListener("ajax:success", this) | ||
this.addEventListener("ajax:error", this) | ||
} | ||
|
||
handleEvent(event) { | ||
switch (event.type) { | ||
case "ajax:success": | ||
this.#updateContent(event.detail[2]) | ||
break | ||
case "ajax:error": | ||
break | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
this.innerHTML = `<alchemy-spinner size="medium"></alchemy-spinner>` | ||
this.#loadContent().then((content) => (this.innerHTML = content)) | ||
} | ||
|
||
/** | ||
* load content of the given url | ||
* @returns {Promise<string>} | ||
*/ | ||
async #loadContent() { | ||
const response = await fetch(this.url, { | ||
headers: { "X-Requested-With": "XMLHttpRequest" } | ||
}) | ||
return await response.text() | ||
} | ||
|
||
#updateContent(xhr) { | ||
const isTextResponse = xhr.getResponseHeader("Content-Type").match(/html/) | ||
|
||
if (isTextResponse) { | ||
this.innerHTML = xhr.responseText | ||
} | ||
} | ||
|
||
get url() { | ||
return this.getAttribute("url") | ||
} | ||
} | ||
|
||
customElements.define("alchemy-remote-partial", RemotePartial) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters