Browse Source

Add store (wip)

pull/937/head
Sergei Birukov 2 years ago
parent
commit
31df6caae4
  1. 62
      webui/src/App.vue
  2. 6
      webui/src/components/Auth.vue
  3. 4
      webui/src/main.js
  4. 45
      webui/src/store/store.js

62
webui/src/App.vue

@ -263,9 +263,15 @@ import Footer from '@/components/Footer.vue';
import API from '@/services/api'; import API from '@/services/api';
import Client from '@/components/Client.vue'; import Client from '@/components/Client.vue';
const authenticated = ref(null); import { useStore } from './store/store';
const authenticating = ref(false); import { storeToRefs } from 'pinia';
const password = ref(null);
const store = useStore();
const { authenticated } = storeToRefs(store);
// const authenticated = ref(null);
// const authenticating = ref(false);
// const password = ref(null);
const requiresPassword = ref(null); const requiresPassword = ref(null);
const clients = ref(null); const clients = ref(null);
@ -395,31 +401,31 @@ async function refresh({ updateCharts = false } = {}) {
}); });
} }
function login(e) { // function login(e) {
e.preventDefault(); // e.preventDefault();
if (!password.value) return; // if (!password.value) return;
if (authenticating.value) return; // if (authenticating.value) return;
authenticating.value = true; // authenticating.value = true;
api // api
.createSession({ // .createSession({
password: password, // password: password,
}) // })
.then(async () => { // .then(async () => {
const session = await api.getSession(); // const session = await api.getSession();
authenticated.value = session.authenticated; // authenticated.value = session.authenticated;
requiresPassword.value = session.requiresPassword; // requiresPassword.value = session.requiresPassword;
return this.refresh(); // return this.refresh();
}) // })
.catch((err) => { // .catch((err) => {
alert(err.message || err.toString()); // alert(err.message || err.toString());
}) // })
.finally(() => { // .finally(() => {
this.authenticating = false; // this.authenticating = false;
this.password = null; // this.password = null;
}); // });
} // }
function logout(e) { function logout(e) {
e.preventDefault(); e.preventDefault();

6
webui/src/components/Auth.vue

@ -40,7 +40,11 @@
<script setup> <script setup>
import IconAvatarDefault from '@/components/icons/IconAvatarDefault.vue'; import IconAvatarDefault from '@/components/icons/IconAvatarDefault.vue';
import LoadingSpinner from './LoadingSpinner.vue'; import LoadingSpinner from '@/components/LoadingSpinner.vue';
import { useStore } from '@/store/store';
import { storeToRefs } from 'pinia';
const store = useStore();
const { password, authenticating, login } = storeToRefs(store);
</script> </script>

4
webui/src/main.js

@ -5,6 +5,4 @@ import { createApp } from 'vue';
import { createPinia } from 'pinia'; import { createPinia } from 'pinia';
const pinia = createPinia(); const pinia = createPinia();
const app = createApp(App).mount('#app'); createApp(App).use(pinia).mount('#app');
app.use(pinia);

45
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,
};
});
Loading…
Cancel
Save