From f7bc0b862f32ecd1591f7b459dd72202ff247205 Mon Sep 17 00:00:00 2001 From: Code by Ben Date: Tue, 24 Sep 2024 11:54:56 +1000 Subject: [PATCH] fixed Something on the server is not working feedback with successful refresh. --- assets/js/editor-assets.js | 10 ++++++++-- handler/htmx/artifact.go | 6 ++---- public/js/editor-assets.min.js | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/assets/js/editor-assets.js b/assets/js/editor-assets.js index aaec2719..0ba73005 100644 --- a/assets/js/editor-assets.js +++ b/assets/js/editor-assets.js @@ -160,12 +160,18 @@ import { getElmById } from "./helper.mjs"; } const errClass = "text-danger"; const xhr = event.detail.xhr; - if (event.detail.successful) { + const statusFound = xhr.status === 200 || xhr.status === 302; + if ((event.detail && event.detail.successful) || statusFound) { feedback.innerText = `${xhr.responseText}`; feedback.classList.remove(errClass); return; } - if (event.detail.failed && event.detail.xhr) { + if ( + event.detail && + event.detail.failed !== undefined && + event.detail.failed && + event.detail.xhr + ) { feedback.classList.add(errClass); feedback.innerText = `Something on the server is not working, ` + diff --git a/handler/htmx/artifact.go b/handler/htmx/artifact.go index 259cbd3a..e084ecd3 100644 --- a/handler/htmx/artifact.go +++ b/handler/htmx/artifact.go @@ -37,10 +37,8 @@ var ( // // [HTMX header]: https://htmx.org/reference/#response_headers func pageRefresh(c echo.Context) echo.Context { - res := c.Response() - const htmxRefresh = "HX-Refresh" - res.Header().Set(htmxRefresh, "true") - res.WriteHeader(http.StatusOK) + c.Response().Header().Set("HX-Refresh", "true") + c.Response().WriteHeader(http.StatusFound) return c } diff --git a/public/js/editor-assets.min.js b/public/js/editor-assets.min.js index a9e2dedd..468c2640 100644 --- a/public/js/editor-assets.min.js +++ b/public/js/editor-assets.min.js @@ -1,2 +1,2 @@ /* editor-assets.min.js © Defacto2 2024 */ -(()=>{function u(t){let r=document.getElementById(t);if(r==null)throw new Error(`The ${t} for getElmById() element is null.`);return r}var v="is-invalid",f="d-none",R=100,x=1024*1024,L=100*x;async function Y(t){if(t==null)throw new Error("The file value of checkSHA is null.");try{let r=await z(t),o=await fetch(`/uploader/sha384/${r}`,{method:"PATCH",headers:{"Content-Type":"text/plain"},body:r});if(!o.ok)throw new Error(`Hashing is not possible, server response: ${o.status}`);return await o.text()=="true"}catch(r){console.log(`Hashing is not possible: ${r}`)}}async function z(t){if(t==null)throw new Error("The file value of sha384 is null.");try{let r=await t.arrayBuffer(),o=await crypto.subtle.digest("SHA-384",r);return Array.from(new Uint8Array(o)).map(c=>c.toString(16).padStart(2,"0")).join("")}catch(r){throw new Error(`Could not use arrayBuffer or crypto.subtle: ${r}`)}}function g(t,r){if(t==null)throw new Error("The formId value of progress is null.");if(r==null)throw new Error("The elementId value of progress is null.");htmx.on(`#${t}`,"htmx:xhr:progress",function(o){o.target.id==`${t}`&&htmx.find(`#${r}`).setAttribute("value",o.detail.loaded/o.detail.total*R)})}function $(t){if(t==null)throw new Error("The file value of checkSize is null.");return t.size>L?`The chosen file is too big at ${Math.round(t.size/x)}MB, maximum size is ${L/x}MB.`:""}function B(t,r,o,c){if(t==null)throw new Error("The errors value of checkErrors is null.");if(r==null)throw new Error("The alert value of checkErrors is null.");if(o==null)throw new Error("The fileInput value of checkErrors is null.");if(c==null)throw new Error("The results value of checkErrors is null.");t=t.filter(h=>h!=""),!(t.length<=0)&&(r.innerText=t.join(" "),r.classList.remove(f),o.innerText="",o.classList.add(v),c.classList.add(f))}async function M(t,r,o,c){if(t==null)throw new Error("The file value of checkDuplicate is null.");if(r==null)throw new Error("The alert value of checkDuplicate is null.");if(o==null)throw new Error("The fileInput value of checkDuplicate is null.");if(c==null)throw new Error("The results value of checkDuplicate is null.");await Y(t)!=!1&&(r.innerText=`The chosen file already exists in the database: ${t.name}`,r.classList.remove(f),o.innerText="",o.classList.add(v),c.classList.add(f))}function I(t,r,o){if(t==null)throw new Error("The fileInput value of resetInput is null.");if(r==null)throw new Error("The alert value of resetInput is null.");if(o==null)throw new Error("The results value of resetInput is null.");t.innerText="",t.classList.remove(v),r.innerText="",r.classList.add(f),o.innerText="",o.classList.add(f)}(()=>{"use strict";g("artifact-editor-dl-form","artifact-editor-dl-progress"),g("artifact-editor-preview-form","artifact-editor-preview-progress");let t=u("artifact-editor-preview-reset"),r=u("artifact-editor-replace-preview"),o=u("artifact-editor-dl-alert"),c=u("artifact-editor-dl-reset"),h=u("artifact-editor-last-modified"),p=u("artifact-editor-dl-results"),d=u("artifact-editor-dl-up");d.addEventListener("change",S);async function S(){I(d,o,p);let e=this.files[0],s=[$(e)];B(s,o,d,p),M(e,o,d,p);let n=e.lastModified,i=new Date().getTime(),a=60*60*1e3;i-n{location.reload()},500);return}if(e.detail.failed&&e.detail.xhr){i.classList.add(a),i.innerText=`Something on the server is not working, ${y.status} status: ${y.responseText}.`;return}i.classList.add(a),i.innerText="Something with the browser is not working, please try again or refresh the page."}function k(e,s,n,i){if(e.detail.elt===null||e.detail.elt.id!==`${s}`)return;let a=document.getElementById(n);if(a===null)throw new Error(`The htmx successful input element ${n} is null`);let l=document.getElementById(i);if(l===null)throw new Error(`The htmx successful feedback element ${i} is null`);if(e.detail.successful)return A(e,a,l);if(e.detail.failed&&e.detail.xhr)return j(e,a,l);F(a,l)}function A(e,s,n){let i=e.detail.xhr;n.innerText=`${i.responseText}`,n.classList.remove("invalid-feedback"),n.classList.add("valid-feedback"),s.classList.remove("is-invalid"),s.classList.add("is-valid"),setTimeout(()=>{location.reload()},500)}function j(e,s,n){let i=e.detail.xhr;n.innerText=`Something on the server is not working, ${i.status} status: ${i.responseText}.`,n.classList.remove("valid-feedback"),n.classList.add("invalid-feedback"),s.classList.remove("is-valid"),s.classList.add("is-invalid")}function F(e,s){e.classList.remove("is-valid"),e.classList.add("is-invalid"),s.innerText="Something with the browser is not working, please try again or refresh the page.",s.classList.remove("d-none")}c.addEventListener("click",function(){o.innerText="",o.classList.add("d-none"),d.value="",d.classList.remove("is-invalid","is-valid")}),t.addEventListener("click",function(){r.value="",r.classList.remove("is-invalid","is-valid")})})();})(); +(()=>{function u(t){let r=document.getElementById(t);if(r==null)throw new Error(`The ${t} for getElmById() element is null.`);return r}var g="is-invalid",f="d-none",R=100,v=1024*1024,L=100*v;async function Y(t){if(t==null)throw new Error("The file value of checkSHA is null.");try{let r=await z(t),o=await fetch(`/uploader/sha384/${r}`,{method:"PATCH",headers:{"Content-Type":"text/plain"},body:r});if(!o.ok)throw new Error(`Hashing is not possible, server response: ${o.status}`);return await o.text()=="true"}catch(r){console.log(`Hashing is not possible: ${r}`)}}async function z(t){if(t==null)throw new Error("The file value of sha384 is null.");try{let r=await t.arrayBuffer(),o=await crypto.subtle.digest("SHA-384",r);return Array.from(new Uint8Array(o)).map(c=>c.toString(16).padStart(2,"0")).join("")}catch(r){throw new Error(`Could not use arrayBuffer or crypto.subtle: ${r}`)}}function T(t,r){if(t==null)throw new Error("The formId value of progress is null.");if(r==null)throw new Error("The elementId value of progress is null.");htmx.on(`#${t}`,"htmx:xhr:progress",function(o){o.target.id==`${t}`&&htmx.find(`#${r}`).setAttribute("value",o.detail.loaded/o.detail.total*R)})}function $(t){if(t==null)throw new Error("The file value of checkSize is null.");return t.size>L?`The chosen file is too big at ${Math.round(t.size/v)}MB, maximum size is ${L/v}MB.`:""}function B(t,r,o,c){if(t==null)throw new Error("The errors value of checkErrors is null.");if(r==null)throw new Error("The alert value of checkErrors is null.");if(o==null)throw new Error("The fileInput value of checkErrors is null.");if(c==null)throw new Error("The results value of checkErrors is null.");t=t.filter(h=>h!=""),!(t.length<=0)&&(r.innerText=t.join(" "),r.classList.remove(f),o.innerText="",o.classList.add(g),c.classList.add(f))}async function M(t,r,o,c){if(t==null)throw new Error("The file value of checkDuplicate is null.");if(r==null)throw new Error("The alert value of checkDuplicate is null.");if(o==null)throw new Error("The fileInput value of checkDuplicate is null.");if(c==null)throw new Error("The results value of checkDuplicate is null.");await Y(t)!=!1&&(r.innerText=`The chosen file already exists in the database: ${t.name}`,r.classList.remove(f),o.innerText="",o.classList.add(g),c.classList.add(f))}function I(t,r,o){if(t==null)throw new Error("The fileInput value of resetInput is null.");if(r==null)throw new Error("The alert value of resetInput is null.");if(o==null)throw new Error("The results value of resetInput is null.");t.innerText="",t.classList.remove(g),r.innerText="",r.classList.add(f),o.innerText="",o.classList.add(f)}(()=>{"use strict";T("artifact-editor-dl-form","artifact-editor-dl-progress"),T("artifact-editor-preview-form","artifact-editor-preview-progress");let t=u("artifact-editor-preview-reset"),r=u("artifact-editor-replace-preview"),o=u("artifact-editor-dl-alert"),c=u("artifact-editor-dl-reset"),h=u("artifact-editor-last-modified"),x=u("artifact-editor-dl-results"),d=u("artifact-editor-dl-up");d.addEventListener("change",S);async function S(){I(d,o,x);let e=this.files[0],s=[$(e)];B(s,o,d,x),M(e,o,d,x);let n=e.lastModified,i=new Date().getTime(),a=60*60*1e3;i-n{location.reload()},500);return}if(e.detail.failed&&e.detail.xhr){i.classList.add(a),i.innerText=`Something on the server is not working, ${p.status} status: ${p.responseText}.`;return}i.classList.add(a),i.innerText="Something with the browser is not working, please try again or refresh the page."}function y(e,s,n,i){if(e.detail.elt===null||e.detail.elt.id!==`${s}`)return;let a=document.getElementById(n);if(a===null)throw new Error(`The htmx successful input element ${n} is null`);let l=document.getElementById(i);if(l===null)throw new Error(`The htmx successful feedback element ${i} is null`);if(e.detail.successful)return A(e,a,l);if(e.detail.failed&&e.detail.xhr)return F(e,a,l);j(a,l)}function A(e,s,n){let i=e.detail.xhr;n.innerText=`${i.responseText}`,n.classList.remove("invalid-feedback"),n.classList.add("valid-feedback"),s.classList.remove("is-invalid"),s.classList.add("is-valid"),setTimeout(()=>{location.reload()},500)}function F(e,s,n){let i=e.detail.xhr;n.innerText=`Something on the server is not working, ${i.status} status: ${i.responseText}.`,n.classList.remove("valid-feedback"),n.classList.add("invalid-feedback"),s.classList.remove("is-valid"),s.classList.add("is-invalid")}function j(e,s){e.classList.remove("is-valid"),e.classList.add("is-invalid"),s.innerText="Something with the browser is not working, please try again or refresh the page.",s.classList.remove("d-none")}c.addEventListener("click",function(){o.innerText="",o.classList.add("d-none"),d.value="",d.classList.remove("is-invalid","is-valid")}),t.addEventListener("click",function(){r.value="",r.classList.remove("is-invalid","is-valid")})})();})();