Merge pull request #51 from 0hAodha/preferences
Add and store user preferences t
This commit is contained in:
@ -49,8 +49,8 @@ exports.postStationData = functions.https.onRequest((request, response) => {
|
||||
cors(request, response, () => {
|
||||
var batchWrite = db.batch();
|
||||
jsonData.forEach((doc) => {
|
||||
// append if the dartCodes hashset is empty or the current station is not present
|
||||
if (dartCodes.size == 0 || !dartCodes.has(doc["StationCode"][0])) {
|
||||
// append if the dartCodes hashset is empty or the current station is not present, and ignoring positions of zero
|
||||
if ((dartCodes.size == 0 || !dartCodes.has(doc["StationCode"][0])) && !(doc["StationLongitude"] == 0 || doc["StationLatitude"] == 0)) {
|
||||
doc["StationType"] = [stationTypeCode]
|
||||
var docID = db.collection('stations').doc(doc["StationCode"][0])
|
||||
batchWrite.set(docID, doc);
|
||||
@ -199,13 +199,18 @@ exports.postLiveTrainData = functions.https.onRequest((request, response) => {
|
||||
})
|
||||
})
|
||||
|
||||
exports.securefunction = functions.https.onCall((data, context) => {
|
||||
if (typeof context.auth === undefined) {
|
||||
// user not logged in
|
||||
return "User is not logged in"
|
||||
}
|
||||
else {
|
||||
// user logged in
|
||||
return "User is logged in"
|
||||
}
|
||||
})
|
||||
// secure function to fetch a user's filter preferences from the database
|
||||
exports.getPreferences = functions.https.onCall((data, context) => {
|
||||
if (!context.auth) return "Error request is not verified"
|
||||
return admin.firestore().collection('preferences').doc(context.auth.uid).get().then((preferences) => {
|
||||
return preferences.data()
|
||||
})
|
||||
})
|
||||
|
||||
// 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"
|
||||
})
|
||||
})
|
@ -34,6 +34,7 @@
|
||||
<script>
|
||||
import app from "../api/firebase"
|
||||
import { getAuth, onAuthStateChanged, signOut } from "firebase/auth"
|
||||
import { store } from '../store/store'
|
||||
|
||||
export default {
|
||||
name: "Navbar",
|
||||
@ -49,6 +50,7 @@ export default {
|
||||
const auth = getAuth(app);
|
||||
onAuthStateChanged(auth, (user) => {
|
||||
user ? this.isLoggedIn = true : this.isLoggedIn = false
|
||||
store.setLoginStatus(this.isLoggedIn)
|
||||
})
|
||||
},
|
||||
|
||||
|
@ -56,15 +56,6 @@ export default {
|
||||
|
||||
created() {
|
||||
this.user = auth.currentUser
|
||||
const functions = getFunctions(app)
|
||||
let host = window.location.hostname
|
||||
if (host === '127.0.0.1' || host === 'localhost') {
|
||||
connectFunctionsEmulator(functions, host, 5001);
|
||||
}
|
||||
const secureFunction = httpsCallable(functions, 'securefunction')
|
||||
secureFunction().then((response) => {
|
||||
console.log(response);
|
||||
})
|
||||
},
|
||||
|
||||
methods: {
|
||||
|
@ -60,7 +60,7 @@ export default {
|
||||
signInWithEmailAndPassword(auth, this.email, this.password)
|
||||
.then((userCredential) => {
|
||||
const user = userCredential.user
|
||||
this.$router.push({path:'/account'})
|
||||
this.$router.push({path:'/'})
|
||||
})
|
||||
.catch((error) => {
|
||||
this.FirebaseError = error.message
|
||||
|
@ -1,14 +1,14 @@
|
||||
<template>
|
||||
<Navbar />
|
||||
|
||||
<nav class="navbar navbar-light bg-light">
|
||||
<div class="container-fluid" @change="decideShowStations();">
|
||||
<nav v-if="this.trainCoordinates.length>0" class="navbar navbar-light bg-light">
|
||||
<div class="container-fluid" @change="decideShowStations()">
|
||||
<input type="checkbox" id="showMainlandStations" v-model="showMainlandStations"/>
|
||||
<label for="showMainlandStations">Show Mainland Stations</label>
|
||||
<input type="checkbox" id="showDARTStations" v-model="showDARTStations"/>
|
||||
<label for="showDARTStations">Show DART Stations</label>
|
||||
</div>
|
||||
<div class="container-fluid" @change="decideShowTrains();">
|
||||
<div class="container-fluid" @change="decideShowTrains()">
|
||||
<input type="checkbox" id="showLate" v-model="showLate"/>
|
||||
<label for="showLate">Show Late Trains</label>
|
||||
<input type="checkbox" id="showOnTime" v-model="showOnTime"/>
|
||||
@ -24,6 +24,8 @@
|
||||
<input type="checkbox" id="showNotYetRunning" v-model="showNotYetRunning"/>
|
||||
<label for="showNotYetRunning">Show Not-Yet Running Trains</label>
|
||||
</div>
|
||||
|
||||
<button v-if="store.loggedIn" @click="postPreferences()">Save Preferences</button>
|
||||
</nav>
|
||||
|
||||
<transition id="sidebar" name="slideLeft">
|
||||
@ -43,7 +45,7 @@
|
||||
|
||||
<!-- train overlay -->
|
||||
<template v-for="coordinate, i in trainCoordinates" :position="inline-block">
|
||||
<ol-overlay v-if="showTrains[i]" :position="coordinate" :positioning="center-center" :offset="[-14,-16]">
|
||||
<ol-overlay v-if="showTrains[i]" :position="coordinate" :offset="[-14,-16]">
|
||||
<div class="overlay-content" @click="getSelectedTrain(i)">
|
||||
<div v-if="getTrainType(i) === 'DART'">
|
||||
<img v-if="isTrainRunning(i) && isTrainLate(i)" src="../assets/red-train-tram-solid.png" class="trainMapIcon" alt="Late DART Icon">
|
||||
@ -61,7 +63,7 @@
|
||||
|
||||
<!-- station overlay -->
|
||||
<template v-for="coordinate, i in stationCoordinates" :position="inline-block">
|
||||
<ol-overlay v-if="showStations[i]" :position="coordinate" :positioning="center-center" :offset="[-14,-16]">
|
||||
<ol-overlay v-if="showStations[i]" :position="coordinate" :offset="[-14,-16]">
|
||||
<div class="overlay-content" @click="getSelectedStation(i)">
|
||||
<img src="../assets/station.png" class="stationMapIcon" alt="Station Icon">
|
||||
</div>
|
||||
@ -79,8 +81,7 @@
|
||||
|
||||
<script>
|
||||
import { store } from '../store/store';
|
||||
import { ref } from 'vue';
|
||||
import { fromLonLat, toLonLat } from 'ol/proj.js';
|
||||
import { fromLonLat } from 'ol/proj.js';
|
||||
import { getFunctions, httpsCallable, connectFunctionsEmulator } from "firebase/functions";
|
||||
import app from '../api/firebase';
|
||||
import Navbar from '../components/Navbar.vue';
|
||||
@ -92,38 +93,11 @@ export default {
|
||||
name: "MapPage",
|
||||
|
||||
data() {
|
||||
const center = ref(fromLonLat([-7.5029786, 53.4494762]));
|
||||
const projection = ref('EPSG:3857');
|
||||
const zoom = ref(7);
|
||||
const rotation = ref(0);
|
||||
const radius = ref(10);
|
||||
const strokeWidth = ref(1);
|
||||
const strokeColor = ref('black');
|
||||
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 {
|
||||
center,
|
||||
projection,
|
||||
zoom,
|
||||
rotation,
|
||||
radius,
|
||||
strokeWidth,
|
||||
strokeColor,
|
||||
fillColor,
|
||||
center: fromLonLat([-7.5029786, 53.4494762]),
|
||||
projection: 'EPSG:3857',
|
||||
zoom: 7,
|
||||
rotation: 0,
|
||||
|
||||
showTrains: [],
|
||||
showStations: [],
|
||||
@ -135,17 +109,16 @@ export default {
|
||||
isPaused: false,
|
||||
store,
|
||||
|
||||
showMainlandStations,
|
||||
showDARTStations,
|
||||
showLate,
|
||||
showOnTime,
|
||||
showEarly,
|
||||
showMainland,
|
||||
showDART,
|
||||
showRunning,
|
||||
showTerminated,
|
||||
showNotYetRunning
|
||||
}
|
||||
showMainlandStations: true,
|
||||
showDARTStations: true,
|
||||
showLate: true,
|
||||
showOnTime: true,
|
||||
showMainland: true,
|
||||
showDART: true,
|
||||
showRunning: true,
|
||||
showTerminated: true,
|
||||
showNotYetRunning: true,
|
||||
}
|
||||
},
|
||||
|
||||
components: {
|
||||
@ -163,38 +136,96 @@ export default {
|
||||
else {
|
||||
this.getTrainAndStationData();
|
||||
}
|
||||
|
||||
// request new data every 60 seconds
|
||||
// window.setInterval(this.getLiveTrainData, 60000);
|
||||
// window.setInterval(this.getTrainAndStationData, 60000);
|
||||
},
|
||||
|
||||
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"]
|
||||
|
||||
// update the map with the user's preferences
|
||||
console.log("got preferences")
|
||||
this.decideShowStations()
|
||||
this.decideShowTrains()
|
||||
}
|
||||
})
|
||||
.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
|
||||
decideShowTrains() {
|
||||
for (let i = 0; i < this.showTrains.length; i++) {
|
||||
// determine whether or not the tain is a DART or not
|
||||
let isDART = this.getTrainType(i) == "DART";
|
||||
|
||||
if ((this.showRunning && this.allTrains[i]["TrainStatus"][0] == "R") || (this.showTerminated && this.allTrains[i]["TrainStatus"][0] == "T") || this.showNotYetRunning && this.allTrains[i]["TrainStatus"][0] == "N") {
|
||||
if ((this.showDART && isDART) || (this.showMainland && !isDART)) {
|
||||
this.showTrains[i] = (this.showLate && this.isTrainLate(i)) || (this.showOnTime && !this.isTrainLate(i)); // || (this.showMainland && !isDART) || (this.showDART && isDART);
|
||||
}
|
||||
else {
|
||||
this.showTrains[i] = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.showTrains[i] = false;
|
||||
}
|
||||
}
|
||||
for (var i=0; i<this.showTrains.length; i++) {
|
||||
let isDART = this.getTrainType(i) == "DART";
|
||||
if ((this.showRunning && this.allTrains[i]["TrainStatus"][0] == "R") || (this.showTerminated && this.allTrains[i]["TrainStatus"][0] == "T") || this.showNotYetRunning && this.allTrains[i]["TrainStatus"][0] == "N") {
|
||||
if ((this.showDART && isDART) || (this.showMainland && !isDART)) {
|
||||
this.showTrains[i] = (this.showLate && this.isTrainLate(i)) || (this.showOnTime && !this.isTrainLate(i));
|
||||
}
|
||||
else {
|
||||
this.showTrains[i] = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.showTrains[i] = false;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// method to determine whether or not to show each station
|
||||
decideShowStations() {
|
||||
for (let i = 0; i < this.showStations.length; i++) {
|
||||
let isDARTStation = this.allStations[i]["StationType"][0] == "DART";
|
||||
this.showStations[i] = (this.showDARTStations && isDARTStation) || (this.showMainlandStations && !isDARTStation);
|
||||
}
|
||||
for (var i=0; i<this.showStations.length; i++) {
|
||||
let isDARTStation = this.getStationType(i) == "DART";
|
||||
this.showStations[i] = (this.showDARTStations && isDARTStation) || (this.showMainlandStations && !isDARTStation);
|
||||
}
|
||||
},
|
||||
|
||||
// method to display a selected train
|
||||
@ -228,12 +259,8 @@ export default {
|
||||
|
||||
// method to determine whether or not a selected train is running
|
||||
isTrainRunning(i) {
|
||||
if (this.allTrains[i]["TrainStatus"][0] == "R") {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
if (this.allTrains[i]["TrainStatus"][0] == "R") return true;
|
||||
else return false;
|
||||
},
|
||||
|
||||
// method that returns the type of train (either "Train" or "DART")
|
||||
@ -241,6 +268,11 @@ export default {
|
||||
return this.allTrains[i]["TrainType"][0];
|
||||
},
|
||||
|
||||
// method that returns the type of station (either "Train" or "DART")
|
||||
getStationType(i) {
|
||||
return this.allStations[i]["StationType"][0]
|
||||
},
|
||||
|
||||
// method to fetch live train and station data from the database
|
||||
getTrainAndStationData() {
|
||||
const functions = getFunctions(app);
|
||||
@ -259,15 +291,16 @@ export default {
|
||||
try {
|
||||
if (!response.data) throw new Error("Error fetching live train data from the database");
|
||||
var insights = {
|
||||
"totalNumTrains": 0,
|
||||
"numRunningTrains": 0,
|
||||
"numLateRunningTrains": 0,
|
||||
"numTrains": 0,
|
||||
"numDarts": 0,
|
||||
"totalNumStations": 0,
|
||||
"numTrainStations": 0,
|
||||
"numDartStations": 0
|
||||
};
|
||||
"totalNumTrains": 0,
|
||||
"numRunningTrains": 0,
|
||||
"numLateRunningTrains": 0,
|
||||
"numTrains": 0,
|
||||
"numDarts": 0,
|
||||
"totalNumStations": 0,
|
||||
"numTrainStations": 0,
|
||||
"numDartStations": 0
|
||||
};
|
||||
|
||||
var unorderedTrains = [];
|
||||
var currentMessages = [];
|
||||
var latest = null;
|
||||
@ -279,13 +312,11 @@ export default {
|
||||
for (var i=0; i<response.data.length; i++) {
|
||||
let train = response.data[i];
|
||||
this.allTrains[i] = train;
|
||||
|
||||
// filling showTrains witht the default value true
|
||||
this.showTrains[i] = true;
|
||||
|
||||
this.trainCoordinates[i] = ref(fromLonLat([train["TrainLongitude"][0], train["TrainLatitude"][0]]))
|
||||
this.trainCoordinates[i] = fromLonLat([train["TrainLongitude"][0], train["TrainLatitude"][0]])
|
||||
insights["totalNumTrains"] += 1
|
||||
|
||||
// filling showTrains with the default value - true
|
||||
this.showTrains[i] = true;
|
||||
if (train["TrainType"][0] == "Train") insights["numTrains"] += 1;
|
||||
else if (train["TrainType"][0] == "DART") insights["numDarts"] += 1;
|
||||
|
||||
@ -316,7 +347,6 @@ export default {
|
||||
// train is early or ontime
|
||||
else {
|
||||
if (!earliest) earliest = train;
|
||||
|
||||
// check for a new earliest train (early trains are -x mins late)
|
||||
if (num < currEarliestTime) {
|
||||
earliest = train;
|
||||
@ -341,26 +371,27 @@ export default {
|
||||
const getStationData = httpsCallable(functions, 'getStationData');
|
||||
getStationData().then((response) => {
|
||||
if (!response.data) throw new Error("Error fetching station from the database");
|
||||
|
||||
for (var i=0; i<response.data.length; i++) {
|
||||
let station = response.data[i];
|
||||
this.allStations[i] = station;
|
||||
this.stationCoordinates[i] = fromLonLat([station["StationLongitude"][0], station["StationLatitude"][0]])
|
||||
insights["totalNumStations"] += 1
|
||||
|
||||
// setting the station to show on the map by default - true
|
||||
this.showStations[i] = true;
|
||||
|
||||
this.stationCoordinates[i] = ref(fromLonLat([station["StationLongitude"][0], station["StationLatitude"][0]]))
|
||||
insights["totalNumStations"] += 1
|
||||
|
||||
if (station["StationType"][0] == "DART") insights["numDartStations"] += 1;
|
||||
else if (station["StationType"][0] == "Train") insights["numTrainStations"] += 1;
|
||||
}
|
||||
|
||||
store.setInsights(insights);
|
||||
loader.hide();
|
||||
// request the user's preferences
|
||||
this.getPreferences()
|
||||
})
|
||||
}
|
||||
catch (error) {
|
||||
console.log(error)
|
||||
console.log(error.message)
|
||||
loader.hide();
|
||||
}
|
||||
})
|
||||
@ -373,12 +404,11 @@ export default {
|
||||
if (host === '127.0.0.1' || host === 'localhost') {
|
||||
connectFunctionsEmulator(functions, host, 5001);
|
||||
}
|
||||
// post live train data
|
||||
|
||||
const postTrainData = httpsCallable(functions, 'postLiveTrainData');
|
||||
postTrainData().then((response) => {
|
||||
// post station data
|
||||
postTrainData().then(() => {
|
||||
const postStationData = httpsCallable(functions, 'postStationData');
|
||||
postStationData().then((reponse) => {
|
||||
postStationData().then(() => {
|
||||
this.getTrainAndStationData()
|
||||
})
|
||||
})
|
||||
@ -464,4 +494,4 @@ export default {
|
||||
text-align: bottom;
|
||||
font-size: 16px;
|
||||
}
|
||||
</style>
|
||||
</style>
|
@ -2,7 +2,6 @@ import {getAuth, onAuthStateChanged} from "firebase/auth"
|
||||
import app from '../api/firebase';
|
||||
|
||||
function isAuth(to, from, next) {
|
||||
console.log("Checking auth")
|
||||
const auth = getAuth(app)
|
||||
onAuthStateChanged(auth, (user) => {
|
||||
// user is logged in, continue to page
|
||||
|
@ -10,6 +10,7 @@ export const store = reactive({
|
||||
rawData: {},
|
||||
displaySelectedTrain: false,
|
||||
displayedSelectedStation: false,
|
||||
loggedIn: false,
|
||||
|
||||
setInsights(insights) {
|
||||
this.insights = insights
|
||||
@ -49,5 +50,9 @@ export const store = reactive({
|
||||
|
||||
setDisplaySelectedStation(bool) {
|
||||
this.displaySelectedStation = bool
|
||||
},
|
||||
|
||||
setLoginStatus(bool) {
|
||||
this.loggedIn = bool
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user