gamebrary/src/App.vue

363 lines
11 KiB
Vue
Raw Normal View History

2018-10-19 05:15:28 +00:00
<template>
2019-04-09 04:17:26 +00:00
<div
id="app"
:class="{ dark: darkModeEnabled }"
:style="style"
:dir="dir"
2019-04-09 04:17:26 +00:00
>
2019-01-11 21:52:08 +00:00
<nav-header />
2018-10-19 05:15:28 +00:00
2019-06-18 21:18:26 +00:00
<router-view v-if="user || isPublic" />
2019-01-19 05:23:26 +00:00
2019-02-08 06:13:48 +00:00
<div class="auth" v-else>
2019-02-15 22:58:32 +00:00
<img src='/static/gamebrary-logo.png' />
2019-04-04 04:55:30 +00:00
<i class="fas fa-sync-alt fa-2x fast-spin" />
<h3>Authorizing</h3>
2019-02-08 06:13:48 +00:00
</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';
import Toast from '@/components/Toast';
2018-11-05 02:28:29 +00:00
import firebase from 'firebase/app';
2019-04-09 04:17:26 +00:00
import { mapState, mapGetters } from 'vuex';
import { debounce } from 'lodash';
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({
apiKey: 'AIzaSyA6MsmnLtqT4b11r-j15wwreRypO3AodcA',
authDomain: 'gamebrary.com',
databaseURL: 'https://gamebrary-8c736.firebaseio.com',
projectId: 'gamebrary-8c736',
storageBucket: 'gamebrary-8c736.appspot.com',
messagingSenderId: '324529217902',
});
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 {
name: 'App',
components: {
NavHeader,
2019-01-19 05:23:26 +00:00
Toast,
2018-10-19 05:15:28 +00:00
},
computed: {
2019-04-09 04:17:26 +00:00
...mapState(['user', 'platform', 'wallpaperUrl', 'settings']),
2019-02-21 19:39:00 +00:00
...mapGetters(['darkModeEnabled']),
2018-11-21 02:43:10 +00:00
dir() {
return this.settings && this.settings.language === 'ar'
? 'rtl'
: 'ltr';
},
2018-11-21 02:43:10 +00:00
isPublic() {
2019-02-16 04:45:14 +00:00
return this.$route.name === 'share-list';
2018-11-21 02:43:10 +00:00
},
2019-04-09 04:17:26 +00:00
style() {
return this.$route.name === 'game-board' && this.wallpaperUrl
? `background-image: url('${this.wallpaperUrl}')`
: null;
},
customWallpaper() {
2019-04-09 06:16:53 +00:00
// eslint-disable-next-line
2019-04-09 06:10:53 +00:00
return this.settings && this.settings.wallpapers && this.platform && this.settings.wallpapers[this.platform.code]
2019-04-09 04:17:26 +00:00
? this.settings.wallpapers[this.platform.code].url
: '';
},
},
watch: {
customWallpaper(value) {
if (value) {
2019-04-09 06:10:53 +00:00
if (this.platform) {
this.loadWallpaper();
}
2019-04-09 04:17:26 +00:00
} else {
this.$store.commit('SET_WALLPAPER_URL', '');
}
},
},
mounted() {
2019-02-08 07:42:00 +00:00
this.$bus.$on('SAVE_SETTINGS', this.saveSettings);
this.$bus.$on('SAVE_TAGS', this.saveTags);
2019-04-19 20:27:45 +00:00
this.$bus.$on('SAVE_NOTES', this.saveNotes);
this.init();
},
beforeDestroy() {
this.$bus.$off('SAVE_SETTINGS');
2019-02-08 06:15:16 +00:00
this.$bus.$off('SAVE_TAGS');
2019-04-19 20:27:45 +00:00
this.$bus.$off('SAVE_NOTES');
},
methods: {
init() {
if (this.isPublic) {
return;
}
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();
});
},
2019-05-17 02:32:52 +00:00
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',
});
});
},
2019-04-09 04:17:26 +00:00
loadWallpaper() {
const wallpaperRef = this.customWallpaper;
this.$store.commit('SET_WALLPAPER_URL', '');
storage.child(wallpaperRef).getDownloadURL().then((url) => {
this.$store.commit('SET_WALLPAPER_URL', url);
});
},
saveSettings: debounce(
// eslint-disable-next-line
function(settings) {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
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' });
2019-08-09 19:12:11 +00:00
this.$router.push({ name: 'sessionExpired' });
});
}, 500),
2019-02-08 06:15:16 +00:00
saveTags(tags, force) {
if (tags) {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
2019-02-08 06:15:16 +00:00
db.collection('tags').doc(this.user.uid).set(tags, { merge: !force })
.then(() => {
this.$bus.$emit('TOAST', { message: 'Tags updated' });
})
.catch(() => {
2019-08-09 19:12:11 +00:00
this.$bus.$emit('TOAST', { message: 'There was an error saving your tag', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
2019-02-08 06:15:16 +00:00
});
}
},
2019-04-19 20:27:45 +00:00
saveNotes(notes, force) {
if (notes) {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
2019-04-19 20:27:45 +00:00
db.collection('notes').doc(this.user.uid).set(notes, { merge: !force })
.then(() => {
this.$bus.$emit('TOAST', { message: 'Notes updated' });
})
.catch(() => {
2019-08-09 19:12:11 +00:00
this.$bus.$emit('TOAST', { message: 'There was an error saving your note', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
2019-04-19 20:27:45 +00:00
});
}
},
2018-11-27 23:59:19 +00:00
syncData() {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
2018-11-27 23:59:19 +00:00
db.collection('lists').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const gameLists = doc.data();
this.$store.commit('SET_GAME_LISTS', gameLists);
}
});
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
2018-11-27 23:59:19 +00:00
db.collection('settings').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const settings = doc.data();
this.$store.commit('SET_SETTINGS', settings);
}
});
2019-02-08 06:15:16 +00:00
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
2019-02-08 06:15:16 +00:00
db.collection('tags').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const tags = doc.data();
this.$store.commit('SET_TAGS', tags);
}
});
2019-04-19 20:27:45 +00:00
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
2019-04-19 20:27:45 +00:00
db.collection('notes').doc(this.user.uid)
.onSnapshot((doc) => {
if (doc.exists) {
const notes = doc.data();
this.$store.commit('SET_NOTES', notes);
}
});
2018-11-27 23:59:19 +00:00
},
2019-09-23 20:03:30 +00:00
initUser(user) {
this.$store.commit('SET_USER', user);
this.loadSettings();
2019-02-08 06:15:16 +00:00
this.loadTags();
this.loadLists();
2019-02-08 06:15:16 +00:00
this.syncData();
},
loadSettings() {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
const docRef = db.collection('settings').doc(this.user.uid);
docRef.get().then((doc) => {
2019-08-09 19:17:43 +00:00
const hasData = doc && doc.exists;
return hasData
? this.$store.commit('SET_SETTINGS', doc.data())
: this.initSettings();
}).catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
2019-08-09 19:12:11 +00:00
this.$router.push({ name: 'sessionExpired' });
});
},
loadLists() {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
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' });
2019-08-09 19:12:11 +00:00
this.$router.push({ name: 'sessionExpired' });
2019-02-08 06:15:16 +00:00
});
},
loadTags() {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
2019-02-08 06:15:16 +00:00
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' });
2019-08-09 19:12:11 +00:00
this.$router.push({ name: 'sessionExpired' });
});
},
initList() {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
db.collection('lists').doc(this.user.uid).set({}, { merge: true })
.then(() => {
this.loadLists();
2019-08-09 19:12:11 +00:00
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
},
initSettings() {
2019-09-23 20:03:30 +00:00
// TOOD: move to actions
db.collection('settings').doc(this.user.uid).set({}, { merge: true })
.then(() => {
this.loadSettings();
2019-08-09 19:12:11 +00:00
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
},
},
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);
2019-09-10 20:02:16 +00:00
@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-09-10 20:02:16 +00:00
@import "~styles/styles";
2019-02-15 22:58:32 +00:00
2019-02-21 19:39:00 +00:00
#app {
background-color: $color-gray;
2019-04-09 04:17:26 +00:00
background-size: cover;
2019-02-21 19:39:00 +00:00
&.dark {
2019-02-22 06:14:11 +00:00
background-color: $color-darkest-gray;
2019-02-21 19:39:00 +00:00
}
}
2019-02-15 22:58:32 +00:00
.auth {
2019-04-04 04:55:30 +00:00
background: $color-white;
2019-02-15 22:58:32 +00:00
height: 100vh;
position: fixed;
top: 0;
width: 100vw;
display: flex;
align-items: center;
2019-04-04 04:55:30 +00:00
flex-direction: column;
img {
width: 100px;
margin-top: 100px;
}
2019-02-15 22:58:32 +00:00
i {
margin: $gp;
}
}
</style>