Hi everyone,
I’m trying to build a custom panel field which prints a button.
On button click, i’d like to fetch data from my plugin api.
But i’m facing 2 issues:
- I can’t get component props formatted when passing query syntax to field
plugin index.php
Kirby::plugin('kaliel/kirby-zoho', [
'options' => ['cache' => true],
'api' => [
'routes' => [
[
'pattern' => 'profs',
'action' => function () {
return ['hello' => 'world'];
}
]
]
],
'routes' => require_once 'config/routes.php',
'fields' => require_once 'config/fields.php'
]);
blueprint.yml
fields:
generateProfs:
type: generateProfs
label: Click here to generate teachers
url: {{site.url}}/api/profs
And here is my vue component:
generateProfs.vue
<template>
<k-button @click="onClick(url, $event)">{{ label }}</k-button>
</template>
<script>
export default {
props: {
label: String,
url: String
},
methods: {
onClick: (url, event) => {
/**
* url is not formatted here, got :
* {{site.url}}/api/profs
*/
console.log(url);
/**
* fetch api
* Got Kirby\\Exception\\PermissionException
* with message: "Unauthenticated"
*/
fetch('../api/profs').then(response => {
return response.json();
}).then(console.log);
}
}
};
</script>
<style>
</style>
My second issue is that when i hard code the request url, i get and Unauthorized exception from fetching url. But since, i’m logged in the panel, i don’t know why this occurs.
Finally, is there a better way to make api calls from Vue components ?
Thanks.