diff --git a/expo_project/lib/firebaseSingleton.js b/expo_project/lib/firebaseSingleton.js new file mode 100644 index 0000000..ae0f0ab --- /dev/null +++ b/expo_project/lib/firebaseSingleton.js @@ -0,0 +1,13 @@ +import * as firebase from "firebase"; +import * as firestore from 'firebase/firestore'; // important side effects, go oo-programming + +const firebaseConfig = { + apiKey: "AIzaSyD_6-qVGk9CiFyhv6wmGp-PWb1b8-sCytc", + authDomain: "gehl-921be.firebaseapp.com", + databaseURL: "https://gehl-921be.firebaseio.com", + projectId: "gehl-921be" +}; + +firebase.initializeApp(firebaseConfig); + +export default firebase; diff --git a/expo_project/package.json b/expo_project/package.json index 680a0f7..b6b03cd 100644 --- a/expo_project/package.json +++ b/expo_project/package.json @@ -10,7 +10,7 @@ "dependencies": { "@expo/samples": "2.1.1", "expo": "^28.0.0", - "firebase": "^5.3.0", + "firebase": "^5.4.1-0", "lodash": "4.17.4", "moment": "^2.22.2", "prop-types": "^15.6.2", diff --git a/expo_project/screens/HomeScreen.js b/expo_project/screens/HomeScreen.js index 60aa652..c4669c3 100644 --- a/expo_project/screens/HomeScreen.js +++ b/expo_project/screens/HomeScreen.js @@ -8,6 +8,7 @@ import { Animated } from "react-native"; import { withNavigation } from "react-navigation"; +import { firestore } from 'firebase'; import * as _ from "lodash"; import Colors, { iconColors } from "../constants/Colors"; import { ScrollView } from "../node_modules/react-native-gesture-handler"; @@ -26,6 +27,25 @@ const MIN_DRAWER_OFFSET = 0; // fix this const DRAWER_HEIGHT = height - HEADER_HEIGHT; const INITIAL_DRAWER_OFFSET = DRAWER_HEIGHT; +const studyId = '50Kb9Jfa1ejkURIIE3T2'; // todo should be dynamically set +const surveyId = 'UaAyBbLNOobGO2prwpsT'; // todo should be dynamically set + + +function _markerToDataPoint(marker) { + const dataPoint = {} + fields = [ 'gender', 'groupSize', 'mode', 'object', 'posture', 'timestamp' ]; + fields.forEach((field) => { + if (marker[field]) { + dataPoint[field] = marker[field] + } + }); + + if (marker.coordinate) { + dataPoint.location = marker.coordinate; + } + return dataPoint; +} + class HomeScreen extends React.Component { static navigationOptions = { title: "Long press map to add a pin" @@ -36,6 +56,9 @@ class HomeScreen extends React.Component { this.drawerOffsetY = new Animated.Value(INITIAL_DRAWER_OFFSET); this.drawerOffsetY.addListener(({ value }) => (this._value = value)); + // firestore has its own timestamp type + this.firestore = this.props.screenProps.firebase.firestore(); + this.firestore.settings({ timestampsInSnapshots: true }); this.state = { activeMarkerId: null, @@ -138,7 +161,6 @@ class HomeScreen extends React.Component { } setFormResponse(id, key, value, selectableHeight) { - // TODO: add logic for updating in db const markersCopy = [...this.state.markers]; const marker = _.find(markersCopy, { id @@ -149,6 +171,11 @@ class HomeScreen extends React.Component { this.setState({ markers: markersCopy }); + this.firestore + .collection('study').doc(studyId) + .collection('survey').doc(surveyId) + .collection('dataPoints').doc(marker.id) + .set(_markerToDataPoint(marker)); const currentScrollPosition = this.state.formScrollPosition; const currentDrawerOffset = this.drawerOffsetY._value; @@ -186,30 +213,36 @@ class HomeScreen extends React.Component { } createNewMarker(e) { - // TODO: add logic for inserting into db const markersCopy = [...this.state.markers]; const date = moment(); const dateLabel = date.format("HH:mm"); const timestamp = date.format("x"); - const id = timestamp + ""; // placeholder const title = "Person " + (markersCopy.length + 1); const marker = { coordinate: e.nativeEvent.coordinate, color: this.getRandomIconColor(), - gender: null, - position: null, title, - dateLabel, - id + dateLabel }; - markersCopy.push(marker); - this.setState( - { markers: markersCopy, activeMarkerId: id }, - this.resetDrawer - ); - } + this.firestore + .collection('study').doc(studyId) + .collection('survey').doc(surveyId) + .collection('dataPoints') + .add(_markerToDataPoint(marker)) + .then((doc) => { + const { id, timestamp } = doc; + marker.id = id; + marker.timestamp = timestamp; + markersCopy.push(marker); + this.setState( + { markers: markersCopy, activeMarkerId: id }, + this.resetDrawer + ); + }); + + } setMarkerLocation(e) { // TODO: add logic for updating in db @@ -222,6 +255,12 @@ class HomeScreen extends React.Component { this.setState({ markers: markersCopy }); + + this.firestore + .collection('study').doc(studyId) + .collection('survey').doc(surveyId) + .collection('dataPoints').doc(marker.firestoreId) + .update({location: marker.coordinate}); } } diff --git a/expo_project/yarn.lock b/expo_project/yarn.lock index 8e7f9dc..a3bd7cc 100644 --- a/expo_project/yarn.lock +++ b/expo_project/yarn.lock @@ -615,9 +615,9 @@ version "0.3.4" resolved "https://registry.yarnpkg.com/@firebase/auth-types/-/auth-types-0.3.4.tgz#253b1b2d9b520a0b945d4617c8418f0f19a4159f" -"@firebase/auth@0.7.2": - version "0.7.2" - resolved "https://registry.yarnpkg.com/@firebase/auth/-/auth-0.7.2.tgz#6432f22f370d003f046bf6a395f6af1369c2ecc1" +"@firebase/auth@0.7.3-0": + version "0.7.3-0" + resolved "https://registry.yarnpkg.com/@firebase/auth/-/auth-0.7.3-0.tgz#f08bc4048c9dabc808040893b3c50d71ed2d1eae" dependencies: "@firebase/auth-types" "0.3.4" @@ -639,9 +639,9 @@ version "0.6.0" resolved "https://registry.yarnpkg.com/@firebase/firestore-types/-/firestore-types-0.6.0.tgz#560befea439a4a9d830866fa5f5859e4bb30d770" -"@firebase/firestore@0.7.0": - version "0.7.0" - resolved "https://registry.yarnpkg.com/@firebase/firestore/-/firestore-0.7.0.tgz#1abbabac0f6a194513d625c5a71f62390e7ac2e3" +"@firebase/firestore@0.7.1-0": + version "0.7.1-0" + resolved "https://registry.yarnpkg.com/@firebase/firestore/-/firestore-0.7.1-0.tgz#34ed91391d32c30fdfc8d73a4291dd13e3a53f76" dependencies: "@firebase/firestore-types" "0.6.0" "@firebase/logger" "0.1.1" @@ -2989,14 +2989,14 @@ find-up@^2.0.0, find-up@^2.1.0: dependencies: locate-path "^2.0.0" -firebase@^5.3.0: - version "5.4.0" - resolved "https://registry.yarnpkg.com/firebase/-/firebase-5.4.0.tgz#80f639313416053ba229f8b11327832250ee01a1" +firebase@^5.4.1-0: + version "5.4.1-0" + resolved "https://registry.yarnpkg.com/firebase/-/firebase-5.4.1-0.tgz#89bffa050cdb469254d1944aa9c14ab9fea7e59a" dependencies: "@firebase/app" "0.3.3" - "@firebase/auth" "0.7.2" + "@firebase/auth" "0.7.3-0" "@firebase/database" "0.3.4" - "@firebase/firestore" "0.7.0" + "@firebase/firestore" "0.7.1-0" "@firebase/functions" "0.3.0" "@firebase/messaging" "0.3.5" "@firebase/polyfill" "0.3.3" @@ -3163,7 +3163,7 @@ glob@^6.0.1: once "^1.3.0" path-is-absolute "^1.0.0" -glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2: +glob@^7.0.3, glob@^7.1.1, glob@^7.1.2: version "7.1.2" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" dependencies: @@ -3174,6 +3174,17 @@ glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2: once "^1.3.0" path-is-absolute "^1.0.0" +glob@^7.0.5: + version "7.1.3" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1" + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + global@^4.3.0: version "4.3.2" resolved "https://registry.yarnpkg.com/global/-/global-4.3.2.tgz#e76989268a6c74c38908b1305b10fc0e394e9d0f" @@ -3397,12 +3408,18 @@ iconv-lite@0.4.19: version "0.4.19" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b" -iconv-lite@0.4.23, iconv-lite@^0.4.17, iconv-lite@^0.4.4, iconv-lite@~0.4.13: +iconv-lite@0.4.23, iconv-lite@^0.4.17: version "0.4.23" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.23.tgz#297871f63be507adcfbfca715d0cd0eed84e9a63" dependencies: safer-buffer ">= 2.1.2 < 3" +iconv-lite@^0.4.4, iconv-lite@~0.4.13: + version "0.4.24" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" + dependencies: + safer-buffer ">= 2.1.2 < 3" + idx@^2.1.0: version "2.4.0" resolved "https://registry.yarnpkg.com/idx/-/idx-2.4.0.tgz#e89e6650c889a44bf889f79d47f40fe09b4eeaa3" @@ -4800,7 +4817,11 @@ mv@^2.1.1, mv@~2: ncp "~2.0.0" rimraf "~2.4.0" -nan@^2.0.0, nan@^2.9.2: +nan@^2.0.0: + version "2.11.0" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.11.0.tgz#574e360e4d954ab16966ec102c0c049fd961a099" + +nan@^2.9.2: version "2.10.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.10.0.tgz#96d0cd610ebd58d4b4de9cc0c6828cda99c7548f"