Add and store user preferences

This commit is contained in:
Conor McNamara
2023-03-15 12:04:58 +00:00
parent 97ff51e5d4
commit 89dcd43afe
4 changed files with 94 additions and 38 deletions

View File

@ -199,13 +199,18 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => {
}) })
}) })
exports.securefunction = functions.https.onCall((data, context) => { // secure function to fetch a user's filter preferences from the database
if (typeof context.auth === undefined) { exports.getPreferences = functions.https.onCall((data, context) => {
// user not logged in if (!context.auth) return "Error request is not verified"
return "User is not logged in" return admin.firestore().collection('preferences').doc(context.auth.uid).get().then((preferences) => {
} return preferences.data()
else { })
// user logged in })
return "User is logged in"
} // secure function to set a user's filter prefernces in the database
exports.postPreferences = functions.https.onCall((data, context) => {
if (!context.auth) return "Error request is not verified"
return admin.firestore().collection('preferences').doc(context.auth.uid).set({data}).then(() => {
return "Successfully saved preferences"
})
}) })

View File

@ -34,6 +34,7 @@
<script> <script>
import app from "../api/firebase" import app from "../api/firebase"
import { getAuth, onAuthStateChanged, signOut } from "firebase/auth" import { getAuth, onAuthStateChanged, signOut } from "firebase/auth"
import { store } from '../store/store'
export default { export default {
name: "Navbar", name: "Navbar",
@ -49,6 +50,7 @@ export default {
const auth = getAuth(app); const auth = getAuth(app);
onAuthStateChanged(auth, (user) => { onAuthStateChanged(auth, (user) => {
user ? this.isLoggedIn = true : this.isLoggedIn = false user ? this.isLoggedIn = true : this.isLoggedIn = false
store.setLoginStatus(this.isLoggedIn)
}) })
}, },

View File

@ -1,7 +1,7 @@
<template> <template>
<Navbar /> <Navbar />
<nav class="navbar navbar-light bg-light"> <nav v-if="this.trainCoordinates.length>0" class="navbar navbar-light bg-light">
<div class="container-fluid" @change="decideShowStations();"> <div class="container-fluid" @change="decideShowStations();">
<input type="checkbox" id="showMainlandStations" v-model="showMainlandStations"/> <input type="checkbox" id="showMainlandStations" v-model="showMainlandStations"/>
<label for="showMainlandStations">Show Mainland Stations</label> <label for="showMainlandStations">Show Mainland Stations</label>
@ -24,6 +24,8 @@
<input type="checkbox" id="showNotYetRunning" v-model="showNotYetRunning"/> <input type="checkbox" id="showNotYetRunning" v-model="showNotYetRunning"/>
<label for="showNotYetRunning">Show Not-Yet Running Trains</label> <label for="showNotYetRunning">Show Not-Yet Running Trains</label>
</div> </div>
<button v-if="store.loggedIn" @click="postPreferences()">Save Preferences</button>
</nav> </nav>
<transition id="sidebar" name="slideLeft"> <transition id="sidebar" name="slideLeft">
@ -101,20 +103,6 @@ export default {
const strokeColor = ref('black'); const strokeColor = ref('black');
const fillColor = ref('red'); const fillColor = ref('red');
let showTrains = [];
let showStations = [];
let showMainlandStations = true;
let showDARTStations = true;
let showLate = true;
let showOnTime = true;
let showEarly = true;
let showMainland = true;
let showDART = true;
let showRunning = true;
let showTerminated = true;
let showNotYetRunning = true;
return { return {
center, center,
projection, projection,
@ -135,16 +123,15 @@ export default {
isPaused: false, isPaused: false,
store, store,
showMainlandStations, showMainlandStations: true,
showDARTStations, showDARTStations: true,
showLate, showLate: true,
showOnTime, showOnTime: true,
showEarly, showMainland: true,
showMainland, showDART: true,
showDART, showRunning: true,
showRunning, showTerminated: true,
showTerminated, showNotYetRunning: true,
showNotYetRunning
} }
}, },
@ -163,12 +150,68 @@ export default {
else { else {
this.getTrainAndStationData(); this.getTrainAndStationData();
} }
// request new data every 60 seconds // request new data every 60 seconds
// window.setInterval(this.getLiveTrainData, 60000); // window.setInterval(this.getLiveTrainData, 60000);
}, },
methods: { methods: {
getPreferences() {
if (!store.loggedIn) return
const functions = getFunctions(app);
let host = window.location.hostname
if (host === '127.0.0.1' || host == 'localhost') {
connectFunctionsEmulator(functions, host, 5001);
}
const getPreferencesData = httpsCallable(functions, 'getPreferences')
getPreferencesData().then((response) => {
if (response.data.data) {
this.showMainlandStations = response.data.data["showMainlandStations"]
this.showDARTStations = response.data.data["showDARTStations"]
this.showLate = response.data.data["showLate"]
this.showOnTime = response.data.data["showOnTime"]
this.showMainland = response.data.data["showMainland"]
this.showDART = response.data.data["showDART"]
this.showRunning = response.data.data["showRunning"]
this.showTerminated = response.data.data["showTerminated"]
this.showNotYetRunning = response.data.data["showNotYetRunning"]
}
})
.catch((error) => {
console.log(error.message)
})
},
postPreferences() {
if (!store.loggedIn) return
let preferences = {
"showMainlandStations": this.showMainlandStations,
"showDARTStations": this.showDARTStations,
"showLate": this.showLate,
"showOnTime": this.showOnTime,
"showMainland": this.showMainland,
"showDART": this.showDART,
"showRunning": this.showRunning,
"showTerminated": this.showTerminated,
"showNotYetRunning": this.showNotYetRunning
}
const functions = getFunctions(app);
let host = window.location.hostname
if (host === '127.0.0.1' || host == 'localhost') {
connectFunctionsEmulator(functions, host, 5001);
}
const postPreferencesData = httpsCallable(functions, 'postPreferences')
postPreferencesData(preferences).then(() => {
console.log("Sent preferences successfully")
})
.catch((error) => {
console.log(error.message)
})
},
// method to determine whether or not to show each train // method to determine whether or not to show each train
decideShowTrains() { decideShowTrains() {
for (let i = 0; i < this.showTrains.length; i++) { for (let i = 0; i < this.showTrains.length; i++) {
@ -360,9 +403,10 @@ export default {
}) })
} }
catch (error) { catch (error) {
console.log(error) console.log(error.message)
loader.hide(); loader.hide();
} }
this.getPreferences()
}) })
}, },

View File

@ -10,6 +10,7 @@ export const store = reactive({
rawData: {}, rawData: {},
displaySelectedTrain: false, displaySelectedTrain: false,
displayedSelectedStation: false, displayedSelectedStation: false,
loggedIn: false,
setInsights(insights) { setInsights(insights) {
this.insights = insights this.insights = insights
@ -49,5 +50,9 @@ export const store = reactive({
setDisplaySelectedStation(bool) { setDisplaySelectedStation(bool) {
this.displaySelectedStation = bool this.displaySelectedStation = bool
},
setLoginStatus(bool) {
this.loggedIn = bool
} }
}) })