gamebrary/src/App.vue

204 lines
5.6 KiB
Vue
Raw Normal View History

2018-10-19 05:15:28 +00:00
<template>
<div id="app">
2019-01-11 21:52:08 +00:00
<nav-header />
2018-10-19 05:15:28 +00:00
2019-02-08 06:13:48 +00:00
<main :class="{ 'logged-in': user && !isPublic }" v-if="user && !isPublic">
2018-10-19 05:15:28 +00:00
<router-view />
</main>
2019-01-19 05:23:26 +00:00
2019-02-08 06:13:48 +00:00
<div class="auth" v-else>
<i class="fas fa-circle-notch fast-spin fa-3x" />
<h3>Authorizing</h3>
</div>
2019-01-19 05:23:26 +00:00
<toast />
2018-10-19 05:15:28 +00:00
</div>
</template>
<script>
import NavHeader from '@/components/NavHeader/NavHeader';
2019-01-19 05:23:26 +00:00
import Toast from '@/components/Toast/Toast';
2018-11-05 02:28:29 +00:00
import firebase from 'firebase/app';
import { debounce } from 'lodash';
2018-11-05 02:28:29 +00:00
import 'firebase/auth';
import 'firebase/firestore';
import { mapState } from 'vuex';
2018-11-05 02:28:29 +00:00
firebase.initializeApp({
apiKey: 'AIzaSyA6MsmnLtqT4b11r-j15wwreRypO3AodcA',
authDomain: 'gamebrary.com',
databaseURL: 'https://gamebrary-8c736.firebaseio.com',
projectId: 'gamebrary-8c736',
storageBucket: 'gamebrary-8c736.appspot.com',
messagingSenderId: '324529217902',
});
const db = firebase.firestore();
db.settings({
timestampsInSnapshots: true,
});
2018-10-19 05:15:28 +00:00
export default {
name: 'App',
components: {
NavHeader,
2019-01-19 05:23:26 +00:00
Toast,
2018-10-19 05:15:28 +00:00
},
computed: {
...mapState(['user', 'platform']),
2018-11-21 02:43:10 +00:00
isPublic() {
return this.$route.name === 'shareList';
},
},
mounted() {
if (this.isPublic) {
return;
}
this.$bus.$on('SAVE_SETTINGS', this.saveSettings);
2018-11-27 23:59:19 +00:00
if (this.user) {
this.syncData();
if (!this.platform) {
this.$router.push({ name: 'platforms' });
}
2019-01-24 06:18:34 +00:00
} else {
firebase.auth().getRedirectResult().then(({ user }) => {
if (user) {
this.init(user);
this.syncData();
this.$router.push({ name: 'platforms' });
} else {
const GoogleAuth = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithRedirect(GoogleAuth)
.catch((error) => {
/* eslint-disable */
console.log(error);
});
}
});
2018-11-27 23:59:19 +00:00
}
},
beforeDestroy() {
this.$bus.$off('SAVE_SETTINGS');
},
methods: {
saveSettings: debounce(
// eslint-disable-next-line
function(settings) {
db.collection('settings').doc(this.user.uid).set(settings, { merge: true })
.then(() => {
this.$store.commit('SET_SETTINGS', settings);
this.$bus.$emit('TOAST', { message: 'Settings saved' });
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'There was an error saving your settings', type: 'error' });
});
}, 500),
2018-11-27 23:59:19 +00:00
syncData() {
db.collection('lists').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const gameLists = doc.data();
this.$store.commit('SET_GAME_LISTS', gameLists);
}
});
db.collection('settings').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const settings = doc.data();
this.$store.commit('SET_SETTINGS', settings);
}
});
},
init(user) {
this.$store.commit('SET_USER', user);
this.loadSettings();
this.loadLists();
},
loadSettings() {
const docRef = db.collection('settings').doc(this.user.uid);
docRef.get().then((doc) => {
if (doc.exists) {
this.$store.commit('SET_SETTINGS', doc.data());
} else {
this.initSettings();
}
}).catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
});
},
loadLists() {
db.collection('lists').doc(this.user.uid).get()
.then((doc) => {
if (doc.exists) {
2018-11-21 02:43:10 +00:00
const data = doc.data();
this.$store.commit('SET_GAME_LISTS', data);
} else {
this.initList();
}
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
});
},
initList() {
db.collection('lists').doc(this.user.uid).set({}, { merge: true })
.then(() => {
this.loadLists();
});
},
initSettings() {
db.collection('settings').doc(this.user.uid).set({}, { merge: true })
.then(() => {
this.loadSettings();
});
},
},
2018-10-19 05:15:28 +00:00
};
</script>
<style lang="scss" rel="stylesheet/scss">
@import url(https://fonts.googleapis.com/css?family=Fira+Sans:700|Roboto:400,400italic,700);
2018-10-19 05:15:28 +00:00
@import "~styles/styles.scss";
body {
2018-10-29 23:53:49 +00:00
background: $color-dark-gray;
2018-10-19 05:15:28 +00:00
margin: 0;
font-size: 14px;
}
2018-11-27 07:15:27 +00:00
#app > main {
background: $color-gray;
2018-10-19 05:15:28 +00:00
}
h1, h2, h3, h4, h5, h6 {
font-family: "Fira Sans", sans-serif;
font-weight: 700;
}
2018-10-19 05:15:28 +00:00
body, p, a, li, blockquote {
font-family: "Roboto", sans-serif;
font-weight: 400;
2018-10-19 05:15:28 +00:00
}
</style>