Services refactoring

This commit is contained in:
Bastien Wirtz 2021-12-12 16:28:20 +01:00
parent e6ba84d35a
commit b4a2db6e37
6 changed files with 153 additions and 265 deletions

View File

@ -41,7 +41,7 @@
<SearchInput <SearchInput
class="navbar-item is-inline-block-mobile" class="navbar-item is-inline-block-mobile"
:hotkey=searchHotkey() :hotkey="searchHotkey()"
@input="filterServices" @input="filterServices"
@search-focus="showMenu = true" @search-focus="showMenu = true"
@search-open="navigateToFirstService" @search-open="navigateToFirstService"

View File

@ -19,8 +19,8 @@ export default {
value: String, value: String,
hotkey: { hotkey: {
type: String, type: String,
default: "/" default: "/",
} },
}, },
mounted() { mounted() {
this._keyListener = function (event) { this._keyListener = function (event) {

View File

@ -1,20 +1,6 @@
<template> <template>
<div> <Generic :item="item">
<div class="card" :class="item.class"> <template #content>
<a :href="item.url" :target="item.target" rel="noreferrer">
<div class="card-content">
<div class="media">
<div v-if="item.logo" class="media-left">
<figure class="image is-48x48">
<img :src="item.logo" :alt="`${item.name} logo`" />
</figure>
</div>
<div v-if="item.icon" class="media-left">
<figure class="image is-48x48">
<i style="font-size: 35px" :class="['fa-fw', item.icon]"></i>
</figure>
</div>
<div class="media-content">
<p class="title is-4">{{ item.name }}</p> <p class="title is-4">{{ item.name }}</p>
<p class="subtitle is-6"> <p class="subtitle is-6">
<template v-if="item.subtitle"> <template v-if="item.subtitle">
@ -25,23 +11,23 @@
happily keeping {{ stats.totalRecipes }} recipes organized happily keeping {{ stats.totalRecipes }} recipes organized
</template> </template>
</p> </p>
</div> </template>
</div> </Generic>
<div class="tag" :class="item.tagstyle" v-if="item.tag">
<strong class="tag-text">#{{ item.tag }}</strong>
</div>
</div>
</a>
</div>
</div>
</template> </template>
<script> <script>
import service from "@/mixins/service.js";
import Generic from "./Generic.vue";
export default { export default {
name: "Mealie", name: "Mealie",
mixins: [service],
props: { props: {
item: Object, item: Object,
}, },
components: {
Generic,
},
data: () => ({ data: () => ({
stats: null, stats: null,
meal: null, meal: null,
@ -51,44 +37,20 @@ export default {
}, },
methods: { methods: {
fetchStatus: async function () { fetchStatus: async function () {
if (this.item.subtitle != null) return; // omitting unnecessary ajax call as the subtitle is showing const headers = {
this.meal = await fetch(`${this.item.url}/api/meal-plans/today/`, {
headers: {
Authorization: "Bearer " + this.item.apikey, Authorization: "Bearer " + this.item.apikey,
Accept: "application/json", Accept: "application/json",
}, };
})
.then(function (response) { if (this.item.subtitle != null) return;
if (!response.ok) {
throw new Error("Not 2xx response"); this.meal = await this.fetch("/api/meal-plans/today/", { headers }).catch(
} else { (e) => console.log(e)
if (response != null) { );
return response.json(); this.stats = await this.fetch("/api/debug/statistics/", {
} headers,
} }).catch((e) => console.log(e));
})
.catch((e) => console.log(e));
this.stats = await fetch(`${this.item.url}/api/debug/statistics/`, {
headers: {
Authorization: "Bearer " + this.item.apikey,
Accept: "application/json",
},
})
.then(function (response) {
if (!response.ok) {
throw new Error("Not 2xx response");
} else {
return response.json();
}
})
.catch((e) => console.log(e));
}, },
}, },
}; };
</script> </script>
<style scoped lang="scss">
.media-left img {
max-height: 100%;
}
</style>

View File

@ -1,23 +1,6 @@
<template> <template>
<div> <Generic :item="item">
<div class="card" :class="item.class"> <template #indicator>
<a :href="item.url" :target="item.target" rel="noreferrer">
<div class="card-content">
<div class="media">
<div v-if="item.logo" class="media-left">
<figure class="image is-48x48">
<img :src="item.logo" :alt="`${item.name} logo`" />
</figure>
</div>
<div v-if="item.icon" class="media-left">
<figure class="image is-48x48">
<i style="font-size: 35px" :class="['fa-fw', item.icon]"></i>
</figure>
</div>
<div class="media-content">
<p class="title is-4">{{ item.name }}</p>
<p class="subtitle is-6">{{ item.subtitle }}</p>
</div>
<div class="notifs"> <div class="notifs">
<strong <strong
v-if="config !== null && config.system.news.unread > 0" v-if="config !== null && config.system.news.unread > 0"
@ -44,22 +27,23 @@
>?</strong >?</strong
> >
</div> </div>
</div> </template>
<div class="tag" :class="item.tagstyle" v-if="item.tag"> </Generic>
<strong class="tag-text">#{{ item.tag }}</strong>
</div>
</div>
</a>
</div>
</div>
</template> </template>
<script> <script>
import service from "@/mixins/service.js";
import Generic from "./Generic.vue";
export default { export default {
name: "Medusa", name: "Medusa",
mixins: [service],
props: { props: {
item: Object, item: Object,
}, },
components: {
Generic,
},
data: () => { data: () => {
return { return {
config: null, config: null,
@ -71,15 +55,8 @@ export default {
}, },
methods: { methods: {
fetchConfig: function () { fetchConfig: function () {
fetch(`${this.item.url}/api/v2/config`, { this.fetch("/api/v2/config", {
credentials: "include", headers: { "X-Api-Key": this.item.apikey },
headers: { "X-Api-Key": `${this.item.apikey}` },
})
.then((response) => {
if (response.status != 200) {
throw new Error(response.statusText);
}
return response.json();
}) })
.then((conf) => { .then((conf) => {
this.config = conf; this.config = conf;
@ -94,16 +71,12 @@ export default {
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.media-left img {
max-height: 100%;
}
.notifs { .notifs {
position: absolute; position: absolute;
color: white; color: white;
font-family: sans-serif; font-family: sans-serif;
top: 0.3em; top: 0.3em;
right: 0.5em; right: 0.5em;
}
.notif { .notif {
padding-right: 0.35em; padding-right: 0.35em;
padding-left: 0.35em; padding-left: 0.35em;
@ -113,16 +86,17 @@ export default {
position: relative; position: relative;
margin-left: 0.3em; margin-left: 0.3em;
font-size: 0.8em; font-size: 0.8em;
} &.news {
.news {
background-color: #777777; background-color: #777777;
} }
.warnings { &.warnings {
background-color: #d08d2e; background-color: #d08d2e;
} }
.errors { &.errors {
background-color: #e51111; background-color: #e51111;
} }
}
}
</style> </style>

View File

@ -1,20 +1,6 @@
<template> <template>
<div> <Generic :item="item">
<div class="card" :class="item.class"> <template #content>
<a :href="item.url" :target="item.target" rel="noreferrer">
<div class="card-content">
<div class="media">
<div v-if="item.logo" class="media-left">
<figure class="image is-48x48">
<img :src="item.logo" :alt="`${item.name} logo`" />
</figure>
</div>
<div v-if="item.icon" class="media-left">
<figure class="image is-48x48">
<i style="font-size: 35px" :class="['fa-fw', item.icon]"></i>
</figure>
</div>
<div class="media-content">
<p class="title is-4">{{ item.name }}</p> <p class="title is-4">{{ item.name }}</p>
<p class="subtitle is-6"> <p class="subtitle is-6">
<template v-if="item.subtitle"> <template v-if="item.subtitle">
@ -24,23 +10,23 @@
happily storing {{ api.count }} documents happily storing {{ api.count }} documents
</template> </template>
</p> </p>
</div> </template>
</div> </Generic>
<div class="tag" :class="item.tagstyle" v-if="item.tag">
<strong class="tag-text">#{{ item.tag }}</strong>
</div>
</div>
</a>
</div>
</div>
</template> </template>
<script> <script>
import service from "@/mixins/service.js";
import Generic from "./Generic.vue";
export default { export default {
name: "Paperless", name: "Paperless",
mixins: [service],
props: { props: {
item: Object, item: Object,
}, },
components: {
Generic,
},
data: () => ({ data: () => ({
api: null, api: null,
}), }),
@ -49,36 +35,21 @@ export default {
}, },
methods: { methods: {
fetchStatus: async function () { fetchStatus: async function () {
if (this.item.subtitle != null) return; // omitting unnecessary ajax call as the subtitle is showing if (this.item.subtitle != null) return;
var apikey = this.item.apikey;
const apikey = this.item.apikey;
if (!apikey) { if (!apikey) {
console.error( console.error(
"apikey is not present in config.yml for the paperless entry!" "apikey is not present in config.yml for the paperless entry!"
); );
return; return;
} }
const url = `${this.item.url}/api/documents/`; this.api = await this.fetch("/api/documents/", {
this.api = await fetch(url, {
credentials: "include",
headers: { headers: {
Authorization: "Token " + this.item.apikey, Authorization: "Token " + this.item.apikey,
}, },
}) }).catch((e) => console.log(e));
.then(function (response) {
if (!response.ok) {
throw new Error("Not 2xx response");
} else {
return response.json();
}
})
.catch((e) => console.log(e));
}, },
}, },
}; };
</script> </script>
<style scoped lang="scss">
.media-left img {
max-height: 100%;
}
</style>

View File

@ -1,59 +1,45 @@
<template> <template>
<div> <Generic :item="item">
<div class="card" :class="item.class"> <template #content>
<a :href="item.url" :target="item.target" rel="noreferrer">
<div class="card-content">
<div class="media">
<div v-if="item.logo" class="media-left">
<figure class="image is-48x48">
<img :src="item.logo" :alt="`${item.name} logo`" />
</figure>
</div>
<div v-if="item.icon" class="media-left">
<figure class="image is-48x48">
<i style="font-size: 35px" :class="['fa-fw', item.icon]"></i>
</figure>
</div>
<div class="media-content">
<p class="title is-4">{{ item.name }}</p> <p class="title is-4">{{ item.name }}</p>
<p class="subtitle is-6"> <p class="subtitle is-6">
<template v-if="item.subtitle"> <template v-if="item.subtitle">
{{ item.subtitle }} {{ item.subtitle }}
</template> </template>
<template v-else-if="api"> <template v-else-if="percentage">
{{ percentage }}&percnt; blocked {{ percentage }}&percnt; blocked
</template> </template>
</p> </p>
</template>
<template #indicator>
<div v-if="status" class="status" :class="status">
{{ status }}
</div> </div>
<div v-if="api" class="status" :class="api.status"> </template>
{{ api.status }} </Generic>
</div>
</div>
<div class="tag" :class="item.tagstyle" v-if="item.tag">
<strong class="tag-text">#{{ item.tag }}</strong>
</div>
</div>
</a>
</div>
</div>
</template> </template>
<script> <script>
import service from "@/mixins/service.js";
import Generic from "./Generic.vue";
export default { export default {
name: "PiHole", name: "PiHole",
mixins: [service],
props: { props: {
item: Object, item: Object,
}, },
components: {
Generic,
},
data: () => ({ data: () => ({
api: {
status: "", status: "",
ads_percentage_today: 0, ads_percentage_today: 0,
},
}), }),
computed: { computed: {
percentage: function () { percentage: function () {
if (this.api) { if (this.ads_percentage_today) {
return this.api.ads_percentage_today.toFixed(1); return this.ads_percentage_today.toFixed(1);
} }
return ""; return "";
}, },
@ -63,21 +49,16 @@ export default {
}, },
methods: { methods: {
fetchStatus: async function () { fetchStatus: async function () {
const url = `${this.item.url}/api.php`; const result = await this.fetch("/api.php").catch((e) => console.log(e));
this.api = await fetch(url, {
credentials: "include", this.status = result.status;
}) this.ads_percentage_today = result.ads_percentage_today;
.then((response) => response.json())
.catch((e) => console.log(e));
}, },
}, },
}; };
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.media-left img {
max-height: 100%;
}
.status { .status {
font-size: 0.8rem; font-size: 0.8rem;
color: var(--text-title); color: var(--text-title);