gamebrary/src/App.vue

353 lines
8.8 KiB
Vue
Raw Normal View History

2018-10-19 05:15:28 +00:00
<template>
2019-11-08 20:34:06 +00:00
<div
id="app"
:class="theme"
:style="style"
:dir="dir"
>
<nav-header />
<router-view v-if="user" />
2019-04-09 04:17:26 +00:00
<div
2019-11-08 20:34:06 +00:00
v-else
2019-11-14 21:10:10 +00:00
class="auth"
>
2019-11-08 20:34:06 +00:00
<img src="/static/gamebrary-logo.png" >
<i class="fas fa-sync-alt fa-2x fast-spin" />
<h3>Authorizing</h3>
2018-10-19 05:15:28 +00:00
</div>
2019-11-08 20:34:06 +00:00
<toast />
</div>
2018-10-19 05:15:28 +00:00
</template>
<script>
import NavHeader from '@/components/NavHeader';
import Toast from '@/components/Toast';
2018-11-05 02:28:29 +00:00
import firebase from 'firebase/app';
2019-10-09 16:30:07 +00:00
import { mapState } from 'vuex';
2018-11-05 02:28:29 +00:00
import 'firebase/auth';
import 'firebase/firestore';
2019-04-09 04:17:26 +00:00
import 'firebase/storage';
2018-11-05 02:28:29 +00:00
firebase.initializeApp({
2019-11-08 19:56:03 +00:00
apiKey: 'AIzaSyA6MsmnLtqT4b11r-j15wwreRypO3AodcA',
authDomain: 'gamebrary.com',
databaseURL: 'https://gamebrary-8c736.firebaseio.com',
projectId: 'gamebrary-8c736',
storageBucket: 'gamebrary-8c736.appspot.com',
messagingSenderId: '324529217902',
2018-11-05 02:28:29 +00:00
});
2019-04-09 04:17:26 +00:00
const storage = firebase.storage().ref();
const db = firebase.firestore();
2018-10-19 05:15:28 +00:00
export default {
2019-11-08 19:56:03 +00:00
name: 'App',
2018-10-19 05:15:28 +00:00
2019-11-08 19:56:03 +00:00
components: {
NavHeader,
Toast,
},
2019-11-08 19:56:03 +00:00
computed: {
...mapState(['user', 'platform', 'wallpaperUrl', 'settings']),
2018-11-21 02:43:10 +00:00
2019-11-08 19:56:03 +00:00
dir() {
return this.settings && this.settings.language === 'ar'
? 'rtl'
: 'ltr';
},
2019-11-08 19:56:03 +00:00
style() {
return this.$route.name === 'game-board' && this.wallpaperUrl
? `background-image: url('${this.wallpaperUrl}')`
: null;
},
2019-04-09 04:17:26 +00:00
2019-11-08 19:56:03 +00:00
customWallpaper() {
// eslint-disable-next-line
2019-11-08 20:34:06 +00:00
return this.settings && this.settings.wallpapers && this.platform && this.settings.wallpapers[this.platform.code]
2019-11-08 19:56:03 +00:00
? this.settings.wallpapers[this.platform.code].url
: '';
},
2019-10-09 16:30:07 +00:00
2019-11-08 19:56:03 +00:00
theme() {
const hasPlatform = this.platform && this.platform.code;
const hasTheme = hasPlatform
2019-11-08 20:34:06 +00:00
&& this.settings
&& this.settings[this.platform.code]
&& this.settings[this.platform.code].theme;
2019-10-09 18:55:34 +00:00
2019-11-08 19:56:03 +00:00
const isGameBoard = this.$route.name === 'game-board';
const hasPlatformTheme = hasPlatform && hasTheme;
return isGameBoard && hasPlatformTheme
? `theme-${this.settings[this.platform.code].theme}`
: 'theme-default';
},
},
watch: {
customWallpaper(value) {
if (value) {
if (this.platform) {
this.loadWallpaper();
}
} else {
this.$store.commit('SET_WALLPAPER_URL', '');
}
},
},
mounted() {
// TODO: REMOVE, call action directly
this.$bus.$on('SAVE_TAGS', this.saveTags);
// TODO: REMOVE, call action directly
this.$bus.$on('SAVE_NOTES', this.saveNotes);
this.init();
},
beforeDestroy() {
this.$bus.$off('SAVE_TAGS');
this.$bus.$off('SAVE_NOTES');
},
methods: {
init() {
if (this.user) {
this.syncData();
return;
}
if (this.customWallpaper) {
this.loadWallpaper();
}
firebase.auth().getRedirectResult().then(({ additionalUserInfo, user }) => {
if (additionalUserInfo && additionalUserInfo.isNewUser) {
this.$store.dispatch('SEND_WELCOME_EMAIL', additionalUserInfo);
}
if (user) {
return this.initUser(user);
}
return this.handleAuthRedirect();
});
},
handleAuthRedirect() {
const authProvider = this.$route.params.authProvider || 'google';
const firebaseAuthProvider = authProvider === 'twitter'
? new firebase.auth.TwitterAuthProvider()
: new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithRedirect(firebaseAuthProvider)
.catch((message) => {
this.$bus.$emit('TOAST', {
message,
type: 'error',
});
});
},
loadWallpaper() {
const wallpaperRef = this.customWallpaper;
this.$store.commit('SET_WALLPAPER_URL', '');
storage.child(wallpaperRef).getDownloadURL().then((url) => {
this.$store.commit('SET_WALLPAPER_URL', url);
});
},
saveTags(tags, force) {
if (tags) {
// TOOD: move to actions
db.collection('tags').doc(this.user.uid).set(tags, { merge: !force })
.then(() => {
this.$bus.$emit('TOAST', { message: 'Tags updated' });
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'There was an error saving your tag', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
}
},
saveNotes(notes, force) {
if (notes) {
// TOOD: move to actions
db.collection('notes').doc(this.user.uid).set(notes, { merge: !force })
.then(() => {
this.$bus.$emit('TOAST', { message: 'Notes updated' });
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'There was an error saving your note', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
}
},
syncData() {
// TOOD: move to actions
db.collection('lists').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const gameLists = doc.data();
this.$store.commit('SET_GAME_LISTS', gameLists);
}
});
// TOOD: move to actions
db.collection('settings').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const settings = doc.data();
this.$store.commit('SET_SETTINGS', settings);
}
});
// TOOD: move to actions
db.collection('tags').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const tags = doc.data();
this.$store.commit('SET_TAGS', tags);
}
});
// TOOD: move to actions
db.collection('notes').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const notes = doc.data();
this.$store.commit('SET_NOTES', notes);
}
});
},
initUser(user) {
this.$store.commit('SET_USER', user);
this.loadSettings();
this.loadTags();
this.loadLists();
this.syncData();
},
loadSettings() {
// TOOD: move to actions
const docRef = db.collection('settings').doc(this.user.uid);
2019-10-09 18:55:34 +00:00
2019-11-08 19:56:03 +00:00
docRef.get().then((doc) => {
const hasData = doc && doc.exists;
2019-10-09 16:30:07 +00:00
2019-11-08 19:56:03 +00:00
return hasData
? this.$store.commit('SET_SETTINGS', doc.data())
: this.initSettings();
}).catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
2019-04-09 04:17:26 +00:00
},
2019-11-08 19:56:03 +00:00
loadLists() {
// TOOD: move to actions
db.collection('lists').doc(this.user.uid).get()
.then((doc) => {
if (doc.exists) {
const data = doc.data();
this.$store.commit('SET_GAME_LISTS', data);
} else {
this.initList();
}
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
loadTags() {
// TOOD: move to actions
db.collection('tags').doc(this.user.uid).get()
.then((doc) => {
if (doc.exists) {
const data = doc.data();
this.$store.commit('SET_TAGS', data);
}
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
initList() {
// TOOD: move to actions
db.collection('lists').doc(this.user.uid).set({}, { merge: true })
.then(() => {
this.loadLists();
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
initSettings() {
// TOOD: move to actions
db.collection('settings').doc(this.user.uid).set({}, { merge: true })
.then(() => {
this.loadSettings();
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
},
2018-10-19 05:15:28 +00:00
};
</script>
<style lang="scss" rel="stylesheet/scss">
2019-11-08 20:34:06 +00:00
@import url(https://fonts.googleapis.com/css?family=Fira+Sans:700|Roboto:400,400italic,700);
@import "~styles/styles";
2018-10-19 05:15:28 +00:00
</style>
2019-02-15 22:58:32 +00:00
<style lang="scss" rel="stylesheet/scss" scoped>
2019-11-08 20:34:06 +00:00
@import "~styles/styles";
#app {
background: var(--body-background);
background-size: cover;
}
.auth {
background: var(--body-background);
height: 100vh;
position: fixed;
top: 0;
width: 100vw;
display: flex;
align-items: center;
flex-direction: column;
img {
width: 100px;
margin-top: 100px;
2019-02-21 19:39:00 +00:00
}
2019-11-08 20:34:06 +00:00
i {
margin: $gp;
2019-02-15 22:58:32 +00:00
}
2019-11-08 20:34:06 +00:00
}
2019-02-15 22:58:32 +00:00
</style>