youtuber-blog-frontend/build/client/_app/immutable/chunks/forms.b5iAYO0d.js
2024-06-07 17:11:44 +02:00

2 lines
1.6 KiB
JavaScript

import{p as E,i as w,a as L}from"./entry.DqgMqN0S.js";function g(a){const r=JSON.parse(a);return r.data&&(r.data=E(r.data)),r}function y(a){return HTMLElement.prototype.cloneNode.call(a)}function k(a,r=()=>{}){const l=async({action:e,result:c,reset:i=!0,invalidateAll:n=!0})=>{c.type==="success"&&(i&&HTMLFormElement.prototype.reset.call(a),n&&await w()),(location.origin+location.pathname===e.origin+e.pathname||c.type==="redirect"||c.type==="error")&&L(c)};async function s(e){var p,b,f,h;if(((p=e.submitter)!=null&&p.hasAttribute("formmethod")?e.submitter.formMethod:y(a).method)!=="post")return;e.preventDefault();const i=new URL((b=e.submitter)!=null&&b.hasAttribute("formaction")?e.submitter.formAction:y(a).action),n=new FormData(a),u=(f=e.submitter)==null?void 0:f.getAttribute("name");u&&n.append(u,((h=e.submitter)==null?void 0:h.getAttribute("value"))??"");const m=new AbortController;let d=!1;const A=await r({action:i,cancel:()=>d=!0,controller:m,formData:n,formElement:a,submitter:e.submitter})??l;if(d)return;let o;try{const t=await fetch(i,{method:"POST",headers:{accept:"application/json","x-sveltekit-action":"true"},cache:"no-store",body:n,signal:m.signal});o=g(await t.text()),o.type==="error"&&(o.status=t.status)}catch(t){if((t==null?void 0:t.name)==="AbortError")return;o={type:"error",error:t}}A({action:i,formData:n,formElement:a,update:t=>l({action:i,result:o,reset:t==null?void 0:t.reset,invalidateAll:t==null?void 0:t.invalidateAll}),result:o})}return HTMLFormElement.prototype.addEventListener.call(a,"submit",s),{destroy(){HTMLFormElement.prototype.removeEventListener.call(a,"submit",s)}}}export{k as e};