From 6b57aba4d42eeff9a377e9130f07946506547793 Mon Sep 17 00:00:00 2001 From: Eric Sebastian Date: Wed, 22 Aug 2018 18:01:12 -0400 Subject: [PATCH] html screen to add a survey to a study --- deployment/init.sql | 6 ++++++ index.html | 8 ++++++++ index.js | 41 ++++++++++++++++++++++++++++++++++++----- src/firestore_client.ts | 2 ++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/deployment/init.sql b/deployment/init.sql index 3aec943..fe9150c 100644 --- a/deployment/init.sql +++ b/deployment/init.sql @@ -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, diff --git a/index.html b/index.html index 2cfcbf3..40dccde 100644 --- a/index.html +++ b/index.html @@ -45,6 +45,14 @@

Invite Surveyor

+ diff --git a/index.js b/index.js index e3d7b5f..e73bf5d 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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'); } }); @@ -59,7 +63,7 @@ function saveUser(db, user) { }) } -function getAvailableStudies() { +function getAvailableStudies(elementId) { if (userId) { firestore.collection('study') .where("firebase_uid", "==", userId) @@ -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) } @@ -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'); } @@ -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}`); + }); + }); + }); diff --git a/src/firestore_client.ts b/src/firestore_client.ts index 424473e..e07e5e9 100644 --- a/src/firestore_client.ts +++ b/src/firestore_client.ts @@ -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. */ @@ -33,3 +34,4 @@ export function saveSurvey(db: firestore.Firestore, study: Study, location: Loca console.error("Error adding document: ", error); }); } +