Created
November 29, 2018 06:05
-
-
Save codyswain/a8a6a1e9dac16ccb4f427f7f129e97ea to your computer and use it in GitHub Desktop.
The only important line of HomeScreen.js is line 34. The variable assignment here returns a Promise but no data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as firebase from 'firebase' | |
import 'firebase/firestore'; | |
class FireBase { | |
constructor() { | |
let config = { | |
apiKey: "AIzaSyAO8BB0W6mXDr3ihvkMn-uJjopgfsf_qow", | |
authDomain: "cherishly-412dd.firebaseapp.com", | |
databaseURL: "https://cherishly-412dd.firebaseio.com", | |
projectId: "cherishly-412dd", | |
storageBucket: "cherishly-412dd.appspot.com", | |
messagingSenderId: "378187715149" | |
} | |
if (!firebase.apps.length) { | |
firebase.initializeApp(config) | |
} | |
const firestore = firebase.firestore(); | |
// Random thing required by firestore | |
const settings = {timestampsInSnapshots: true}; | |
firestore.settings(settings); | |
}; | |
// Retrieve data for specific collection and document | |
downloadData = (collectionId, documentId) => { | |
var ref = firebase.firestore().collection(collectionId).doc(documentId); | |
ref.get().then(function(doc) { | |
if (doc.exists) { | |
console.log("Document data:", doc.data()); | |
return doc.data(); | |
} else { | |
// doc.data() will be undefined in this case | |
console.log("No such document!"); | |
return null; | |
} | |
}).catch(function(error) { | |
console.log("Error getting document:", error); | |
return null; | |
}); | |
}; | |
// Retrieve all data within a collection | |
downloadAllData = async (collectionId) => { | |
var docList = []; | |
var ref = firebase.firestore().collection(collectionId); | |
ref.get().then(function(querySnapshot){ | |
querySnapshot.forEach(function(doc){ | |
var docDict = {}; | |
docDict["id"] = doc.id; | |
docDict["data"] = doc.data(); | |
docList.push(docDict); | |
}); | |
// The doc list prints to console here | |
console.log(docList); | |
return docList; | |
}); | |
} | |
uploadData = (collectionId, documentId, data) => { | |
firebase.firestore().collection(collectionId).doc(documentId).set({ | |
data, | |
}) | |
}; | |
} | |
// Helper function to check if a list is empty | |
function isEmpty(obj) { | |
for(var key in obj) { | |
if(obj.hasOwnProperty(key)) | |
return false; | |
} | |
return true; | |
} | |
Fire = new FireBase(); | |
export default Fire; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import { ScrollView, StyleSheet, View, Button, Text, TouchableHighlight } from 'react-native'; | |
import Fire from '../components/firebase/Firebase'; | |
import Navbar from '../components/navbar'; | |
import AddButton from '../components/addbutton'; | |
import PopupWindow from '../components/popupwindow'; | |
import TestAddButton from '../components/testAddButton'; | |
export default class HomeScreen extends Component { | |
constructor(props) { | |
super(props); | |
this.handleAddPhotoButton = this.handleAddPhotoButton.bind(this); | |
this.handleTestButtonPress = this.handleTestButtonPress.bind(this); | |
this.data = []; | |
} | |
// Handle state of popup window here | |
state = { | |
popupWindow: false, | |
loadingData: false, | |
}; | |
// Event handler for add photo button | |
// Pass this handler to child component | |
handleAddPhotoButton = (e) => { | |
e.preventDefault(); | |
e.persist(); | |
this.setState(prevState => ({ | |
popupWindow: !prevState.popupWindow | |
})); | |
var data = Fire.downloadAllData('users'); | |
// Here the doc data doesn't print to console | |
console.log(data); | |
// Fire.downloadData("sample", "test15") | |
// To upload to firebase | |
// Fire.uploadData("sample", "test15", {"egf":"blah"});; | |
console.log(this.state.popupWindow); | |
} | |
handleTestButtonPress = (e) => { | |
e.preventDefault(); | |
console.log(this.data); | |
} | |
// Removes extra whitespace introducedß by navigation | |
static navigationOptions = { | |
header: null, | |
}; | |
render() { | |
const {navigate} = this.props.navigation; | |
return ( | |
<View style={{flex: 1}}> | |
<Navbar navigation={this.props.navigation} /> | |
<PopupWindow status={this.state.popupWindow} /> | |
<View style={styles.main}> | |
<ScrollView> | |
<Text>Cherishly is a photo sharing application!</Text> | |
</ScrollView> | |
</View> | |
<AddButton handler={this.handleAddPhotoButton} /> | |
<TestAddButton handler={this.handleTestButtonPress} /> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
main : { | |
flex: 9, | |
backgroundColor: 'white', | |
justifyContent: 'center', | |
alignItems: 'center' | |
}, | |
addButton: { | |
position: 'absolute', | |
bottom: 30, | |
right: 30, | |
width: 80, | |
height: 80, | |
zIndex: 1, | |
justifyContent: 'center', | |
alignItems: 'center', | |
borderRadius: 50, | |
backgroundColor: '#F35F64', | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment