From ea4f63a3cfdaccb9451eafc1ba8aac98577e1150 Mon Sep 17 00:00:00 2001 From: Sergei Birukov Date: Thu, 21 Mar 2024 11:33:06 +0300 Subject: [PATCH] Add store (wip) --- webui/src/App.vue | 62 +++++++++++++++++++---------------- webui/src/components/Auth.vue | 6 +++- webui/src/main.js | 4 +-- webui/src/store/store.js | 45 +++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 webui/src/store/store.js diff --git a/webui/src/App.vue b/webui/src/App.vue index e8594da0..5e88d045 100644 --- a/webui/src/App.vue +++ b/webui/src/App.vue @@ -263,9 +263,15 @@ import Footer from '@/components/Footer.vue'; import API from '@/services/api'; import Client from '@/components/Client.vue'; -const authenticated = ref(null); -const authenticating = ref(false); -const password = ref(null); +import { useStore } from './store/store'; +import { storeToRefs } from 'pinia'; + +const store = useStore(); +const { authenticated } = storeToRefs(store); + +// const authenticated = ref(null); +// const authenticating = ref(false); +// const password = ref(null); const requiresPassword = ref(null); const clients = ref(null); @@ -395,31 +401,31 @@ async function refresh({ updateCharts = false } = {}) { }); } -function login(e) { - e.preventDefault(); - - if (!password.value) return; - if (authenticating.value) return; - - authenticating.value = true; - api - .createSession({ - password: password, - }) - .then(async () => { - const session = await api.getSession(); - authenticated.value = session.authenticated; - requiresPassword.value = session.requiresPassword; - return this.refresh(); - }) - .catch((err) => { - alert(err.message || err.toString()); - }) - .finally(() => { - this.authenticating = false; - this.password = null; - }); -} +// function login(e) { +// e.preventDefault(); + +// if (!password.value) return; +// if (authenticating.value) return; + +// authenticating.value = true; +// api +// .createSession({ +// password: password, +// }) +// .then(async () => { +// const session = await api.getSession(); +// authenticated.value = session.authenticated; +// requiresPassword.value = session.requiresPassword; +// return this.refresh(); +// }) +// .catch((err) => { +// alert(err.message || err.toString()); +// }) +// .finally(() => { +// this.authenticating = false; +// this.password = null; +// }); +// } function logout(e) { e.preventDefault(); diff --git a/webui/src/components/Auth.vue b/webui/src/components/Auth.vue index b41f7d26..49ca3c11 100644 --- a/webui/src/components/Auth.vue +++ b/webui/src/components/Auth.vue @@ -40,7 +40,11 @@ diff --git a/webui/src/main.js b/webui/src/main.js index 93b2ae9b..ce446318 100644 --- a/webui/src/main.js +++ b/webui/src/main.js @@ -5,6 +5,4 @@ import { createApp } from 'vue'; import { createPinia } from 'pinia'; const pinia = createPinia(); -const app = createApp(App).mount('#app'); - -app.use(pinia); +createApp(App).use(pinia).mount('#app'); diff --git a/webui/src/store/store.js b/webui/src/store/store.js new file mode 100644 index 00000000..095f2b3c --- /dev/null +++ b/webui/src/store/store.js @@ -0,0 +1,45 @@ +import API from '@/services/api'; +import { ref } from 'vue'; + +import { defineStore } from 'pinia'; + +const api = new API(); + +export const useStore = defineStore('store', () => { + const authenticated = ref(null); + const authenticating = ref(false); + const password = ref(null); + + function login(e) { + e.preventDefault(); + + if (!password.value) return; + if (authenticating.value) return; + + authenticating.value = true; + api + .createSession({ + password: password, + }) + .then(async () => { + const session = await api.getSession(); + authenticated.value = session.authenticated; + requiresPassword.value = session.requiresPassword; + return this.refresh(); + }) + .catch((err) => { + alert(err.message || err.toString()); + }) + .finally(() => { + this.authenticating = false; + this.password = null; + }); + } + + return { + authenticated, + authenticating, + login, + password, + }; +});