cmc_fe/src/CMCApp.vue

114 lines
3.3 KiB
Vue
Raw Normal View History

2023-01-19 13:44:49 +01:00
<template>
<v-app>
<MyNav :user="user" :site_info="site_info" />
<v-main class="ma-4">
2023-03-17 17:02:55 +01:00
<v-banner v-if="!site_info.backend_connected"
icon="mdi-exclamation"
color="error"
text="Cannot connect to the data service. Please contact support." >
</v-banner>
<router-view :site_info="site_info" :user_info="user_info"></router-view>
2023-01-19 13:44:49 +01:00
</v-main>
</v-app>
</template>
<script>
import MyNav from './components/MyNav.vue'
import axios from 'axios'
export default {
name: 'App',
components: {
MyNav,
},
data() {
return {
site_info: {
2023-03-17 17:02:55 +01:00
name: "",
features: {},
backend_connected: false
2023-01-19 13:44:49 +01:00
},
user: {
first_name: "",
last_name: "",
email: "",
token: "",
logged_in: false
},
user_info: {}
}
},
watch: {
'user.logged_in'(val) {
console.log("Login status is " + val)
},
'site_info.name'() {
document.title = this.site_info.name
}
},
methods: {
checkLoginStatus() {
let url = this.$api_url + "/users/check_login"
console.log("Checking login status...")
axios
.post(url)
.then(resp => {
this.user.logged_in = resp.data.logged_in
this.user.first_name = resp.data.user.first_name
if (this.user.logged_in) {
console.log("Logged in.")
if (window.location.pathname == "/login") {
this.$router.push("/")
}
this.getUserInfo()
} else {
console.log("Not logged in.")
this.$router.push("/login")
}
})
.catch(error => {
console.log("Error checking login status..." + error)
this.$router.push("/login")
})
},
async getSiteInfo() {
2023-03-17 17:02:55 +01:00
console.log("Trying to get site Info...")
2023-01-19 13:44:49 +01:00
axios
.get(this.$api_url + "/info")
2023-03-17 17:02:55 +01:00
.then(response => {
this.site_info = response.data
this.site_info.backend_connected = true
})
.catch(error => {
this.site_info.name = "Error getting data connection"
this.site_info.backend_connected = false
console.log(error)
setTimeout(() => {
this.getSiteInfo()
},5000)
})
2023-01-19 13:44:49 +01:00
},
async getUserInfo() {
let url = this.$api_url + "/users/info"
console.log(url)
axios.get(url)
.then(resp => {
this.user_info = resp.data
})
.catch(err => {
console.log(err)
})
}
},
created() {
this.getSiteInfo()
if (window.location.pathname != "/login") {
this.checkLoginStatus()
}
}
}
</script>