Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pandananta committed Aug 24, 2018
2 parents db49854 + 6b57aba commit ea606c5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
6 changes: 6 additions & 0 deletions deployment/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ CREATE TABLE IF NOT EXISTS survey (
);

CREATE TYPE gender AS ENUM ('male', 'female', 'unknown');
CREATE TYPE age AS ENUM ('0-14', '15-24', '25-64');
CREATE TYPE mode AS ENUM ('pedestrian', 'bicyclist');
CREATE TYPE group_size AS ENUM ('1', '2', '3+');
CREATE TYPE posture AS ('leaning', 'lying', 'sitting', 'sitting on the ground', 'standing');
CREATE TYPE activity AS ('commerical', 'consuming', 'conversing', 'electronics', 'pets', 'idle', 'running');
CREATE TYPE object AS ('luggage', 'push cart', 'stroller');

CREATE TABLE IF NOT EXISTS surveyors (
survey_id UUID references survey(survey_id) NOT NULL,
Expand Down
8 changes: 8 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ <h4>Invite Surveyor</h4>
<input type="submit" value="Authorize Surveyor" >
</form>
</div>
<div id="start-survey" class="container" hidden>
<h4>Create a survey for Study</h4>
<form id="new-survey">
<select id="study-select-2">
</br>
<input type="submit" value="Authorize Surveyor" >
</form>
</div>
</body>
<script src="index.js"></script/>
</html>
41 changes: 36 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ let studies;

const ui = new firebaseui.auth.AuthUI(auth);
let userId;
let userEmail;

ui.start('#firebaseui-auth-container', {
callbacks: {
signInSuccessWithAuthResult: (authResult) => {
console.log('sign in is a succes: ', authResult);
getAvailableStudies();
getAvailableStudies('study-select');
getAvailableStudies('study-select-2');
const elements = document.getElementsByClassName('container')
Array.prototype.forEach.call(elements, element => {
element.hidden = false;
Expand All @@ -38,7 +40,9 @@ ui.start('#firebaseui-auth-container', {
auth.onAuthStateChanged(function(user) {
if (user) {
userId = user.uid;
getAvailableStudies();
userEmail = user.email;
getAvailableStudies('study-select');
getAvailableStudies('study-select-2');
}
});

Expand All @@ -59,7 +63,7 @@ function saveUser(db, user) {
})
}

function getAvailableStudies() {
function getAvailableStudies(elementId) {
if (userId) {
firestore.collection('study')
.where("firebase_uid", "==", userId)
Expand All @@ -74,7 +78,7 @@ function getAvailableStudies() {
studyId: doc.id,
title: data.title
});
const selectElement = document.getElementById('study-select');
const selectElement = document.getElementById(elementId);
while (selectElement.firstChild) {
selectElement.removeChild(selectElement.firstChild)
}
Expand Down Expand Up @@ -109,7 +113,6 @@ window.addEventListener("load", function () {
event.preventDefault();
const study = document.getElementById("study-select").value;
const email = document.getElementById("surveyor-email").value;
console.log('email: ', email);
if (!email || !study) {
alert('must select a study and enter an email');
}
Expand All @@ -129,4 +132,32 @@ window.addEventListener("load", function () {

});

document.getElementById("start-survey").addEventListener('submit', async function (event) {
event.preventDefault();
const study = document.getElementById("study-select-2").value;
if (!study) {
alert('must select a study and enter an email');
}
console.log('study to start survey under: ', study);
const firestoreStudyRef = await firestore.collection('study').doc(study);
firestoreStudyRef.get().then(function(doc) {
if (doc.exists) {
const surveyors = doc.data().surveyors;
if (surveyors.filter(surveyorEmail => surveyorEmail === userEmail).length > 0) {
const newSurvey = {
locationId: 'not relevant yet',
studyId: study,
representation: 'absolute',
method: 'analog',
userId
};
console.log('creating new survey in firestore: ', newSurvey);
firestoreStudyRef.collection('survey').add(newSurvey);
}
}
}).catch((error) => {
console.error(`failure to save surveyor "${email}" to study with id: ${study}, error: ${error}`);
});
});

});
2 changes: 2 additions & 0 deletions src/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function saveStudy(db: firestore.Firestore, study: Study) {
}



/**
* we need to separately add the survey location, for the prototype we only need one location.
*/
Expand All @@ -33,3 +34,4 @@ export function saveSurvey(db: firestore.Firestore, study: Study, location: Loca
console.error("Error adding document: ", error);
});
}

0 comments on commit ea606c5

Please sign in to comment.