I am working on creating two side by side components that are very similar. A majority of the code is dedicated to curating and formatting the content from the Firebase. I want to abstract these methods into 1 class that we can use for all of our Firebase calls and handling. One of the problems I see is that we have listeners that we establish, and my understanding of how listeners are a bit weak, but I think I can instantiate one and return it in the function and use it in the class that implements the method.
Does anyone have a good example of how to start an abstract class. For example I have to authenticate the firebase and would like to only have to do that once when my app mounts.
Here is some example code:
class WatchQueue extends React.Component {
constructor(props) {
super(props);
this.state = {
matched: false,
walker_uuid: '',
watcher_uuid: uuid.v1()
};
}
async updateWalkers() {
var database = firebase.database().ref("users");
console.log("Updating walkers");
//TODO make the list a queue so that no one is left behind
var snapshot = await database.limitToLast(1).orderByChild("havePaired").equalTo(false).on('value', (snapshot) => {
console.log("Testing snapshot: " + snapshot);
snapshot.forEach((childSnapshot) => {
console.log("Parsing...");
var childKey = childSnapshot.key;
var childData = childSnapshot.val().havePaired;
console.log("One result: " + childKey + " " + childData);
//Next: set havePaired to true, and set the watcher uuid to this device's uuid
this.updateDatabase(childSnapshot.key);
this.setState({walker_uuid: childKey});
})
});
}
updateDatabase(userId){
console.log("Updating database...")
firebase
.database()
.ref('users/' + userId)
.update({havePaired: true,
watcher_uuid: this.state.watcher_uuid});
this.setState({matched: true});
}
Now both updateDatabase() and updateWalker() should be called from a separate abstract class.
I don't believe the abstract class would extend the component, it isn't a component itself. It also might not be viewed as a Class in javacsript, I might just have to import the file as: functions so I can then call functions.doSomething() and that'll work. Has anyone else had success with this?