diff --git a/dist/alt-text-generator.js b/dist/alt-text-generator.js index 65e1376..566eea8 100644 --- a/dist/alt-text-generator.js +++ b/dist/alt-text-generator.js @@ -1 +1 @@ -function p(a,e,i,l,n,r,d,_){var t=typeof a=="function"?a.options:a;e&&(t.render=e,t.staticRenderFns=i,t._compiled=!0),l&&(t.functional=!0),r&&(t._scopeId="data-v-"+r);var s;if(d?(s=function(o){o=o||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,!o&&typeof __VUE_SSR_CONTEXT__<"u"&&(o=__VUE_SSR_CONTEXT__),n&&n.call(this,o),o&&o._registeredComponents&&o._registeredComponents.add(d)},t._ssrRegister=s):n&&(s=_?function(){n.call(this,(t.functional?this.parent:this).$root.$options.shadowRoot)}:n),s)if(t.functional){t._injectStyles=s;var f=t.render;t.render=function(h,u){return s.call(u),f(h,u)}}else{var c=t.beforeCreate;t.beforeCreate=c?[].concat(c,s):[s]}return{exports:a,options:t}}const g={mixins:[Fieldtype],props:{meta:Array,value:String},data(){return{isLoading:!1,responseData:this.responseData,data:this.value}},watch:{data(a){this.update(a)}},methods:{handleButtonClick(){this.isLoading=!0,this.sendDataToBackend()},async sendDataToBackend(){const a={imageUrl:window.location.href,textLanguage:this.meta.language},e=JSON.stringify(a),l=await(await fetch("/api/altTextGeneratorEndpoint",{method:"POST",headers:{"Content-Type":"application/json"},body:e})).text();try{const n=JSON.parse(l);console.log(n);const r=n.altText;localStorage.setItem("altTextToStore",r);const d=document.querySelector('input[name="alt_de"]');this.update(d,{elementId:r}),this.isLoading=!1}catch(n){console.log(n)}}}};var m=function(){var e=this,i=e._self._c;return i("div",{attrs:{disabled:"this.isLoading"}},[i("button",{staticClass:"btn",attrs:{id:"requestButton",type:"button"},on:{click:e.handleButtonClick}},[e._v(" Generate ")]),e.isLoading?i("div",{staticClass:"loading-indicator"},[e._v(" Generating alt text... ")]):e._e()])},v=[],C=p(g,m,v,!1,null,null,null,null);const T=C.exports;Statamic.booting(()=>{Statamic.$components.register("generate_button-fieldtype",T)}); +function h(o,e,r,s,i,l,d,p){var t=typeof o=="function"?o.options:o;e&&(t.render=e,t.staticRenderFns=r,t._compiled=!0),s&&(t.functional=!0),l&&(t._scopeId="data-v-"+l);var a;if(d?(a=function(n){n=n||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,!n&&typeof __VUE_SSR_CONTEXT__<"u"&&(n=__VUE_SSR_CONTEXT__),i&&i.call(this,n),n&&n._registeredComponents&&n._registeredComponents.add(d)},t._ssrRegister=a):i&&(a=p?function(){i.call(this,(t.functional?this.parent:this).$root.$options.shadowRoot)}:i),a)if(t.functional){t._injectStyles=a;var u=t.render;t.render=function(c,_){return a.call(_),u(c,_)}}else{var f=t.beforeCreate;t.beforeCreate=f?[].concat(f,a):[a]}return{exports:o,options:t}}const m={mixins:[Fieldtype],props:{meta:Array,value:String},data(){return{isLoading:!1,responseData:""}},methods:{sendData(){let o=JSON.stringify({imageUrl:window.location.href,textLanguage:this.meta.language});fetch("/api/altTextGeneratorEndpoint",{method:"POST",headers:{"Content-Type":"application/json"},body:o}).then(e=>{this.responseData=e.data}).catch(e=>{console.log(e)})}}};var v=function(){var e=this,r=e._self._c;return r("div",[r("button",{on:{click:e.sendData}},[e._v("Generate")]),r("input",{directives:[{name:"model",rawName:"v-model",value:e.responseData,expression:"responseData"}],attrs:{type:"text",id:e.altText},domProps:{value:e.responseData},on:{input:function(s){s.target.composing||(e.responseData=s.target.value)}}})])},g=[],C=h(m,v,g,!1,null,null,null,null);const y=C.exports;Statamic.booting(()=>{Statamic.$components.register("generate_button-fieldtype",y)}); diff --git a/resources/js/components/GenerateButton.vue b/resources/js/components/GenerateButton.vue index a3644ce..1a5320d 100644 --- a/resources/js/components/GenerateButton.vue +++ b/resources/js/components/GenerateButton.vue @@ -1,16 +1,7 @@ @@ -18,7 +9,6 @@ export default { mixins: [Fieldtype], - props: { meta: Array, value: String, @@ -27,54 +17,32 @@ export default { data() { return { isLoading: false, - responseData: this.responseData, - data: this.value, + responseData: '', }; }, - watch: { - data(data) { - this.update(data); - }, - }, - methods: { - - handleButtonClick() { - this.isLoading = true; - this.sendDataToBackend(); - }, - - async sendDataToBackend() { - const bodyData = { - imageUrl: window.location.href, - textLanguage: this.meta.language, - }; - - const jsonBody = JSON.stringify(bodyData); - const response = await fetch('/api/altTextGeneratorEndpoint', { + sendData() { + let jsonBody = JSON.stringify( + { + imageUrl: window.location.href, + textLanguage: this.meta.language + } + ) + const response = fetch('/api/altTextGeneratorEndpoint', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: jsonBody, - }); - - const responseData = await response.text(); - - try { - const data = JSON.parse(responseData); - console.log(data); - const altText = data.altText; - localStorage.setItem('altTextToStore', altText); - const textField = document.querySelector('input[name="alt_de"]'); - this.update(textField, { value: altText }); - this.isLoading = false; - } catch (error) { - console.log(error); + }) + .then(response => { + this.responseData = response.data; + }) + .catch(error => { + console.log(error) + }) } }, - - }, }; diff --git a/src/Controllers/AltTextGeneratorController.php b/src/Controllers/AltTextGeneratorController.php index 7850a3f..02331ba 100644 --- a/src/Controllers/AltTextGeneratorController.php +++ b/src/Controllers/AltTextGeneratorController.php @@ -32,18 +32,13 @@ public function submitData(Request $request) $altText = $this->generateAltText($url, $textLanguage); - $this->saveAltText($altText, $url, $textLanguage); + // $this->saveAltText($altText, $url, $textLanguage); $responseData = [ 'altText' => $altText, - 'textLanguage' => $textLanguage ]; - $jsonResponse = json_encode($responseData); - - header('Content-Type: application/json; charset=utf-8'); - - echo $jsonResponse; + echo json_encode($responseData); } @@ -66,12 +61,12 @@ public function generateAltText(string $url, string $textLanguage) return $altText; } - +/* public function saveAltText(string $altText, string $imageUrl, string $textLanguage): void { $imageDataService = new ImageDataService(); $filePath = $imageDataService->getFilePathFromUrl($imageUrl); $yamlWriter = new YamlWriterService(); $yamlWriter->addDataToYaml($altText, $textLanguage, $filePath); - } + }*/ }