A React Native extension for DoubleDutch apps, allowing for users to ask questions and share answers at an event.
DoubleDutch ❤️ React Native. Get started
This extension connects to a simple firebase database via @doubledutch/firebase-connector on a per-event basis.
As the mobile client is the driver for content creation, this is the focus of our data model.
Questions, answers, and votes for each are writable by the users that create
them and readable by everyone, so we store them all under public/users/:userId
via fbc.database.public.userRef()
public/users/:userId/questions
{
"abc123": {
"text": "Where is good to eat around here?",
"creator": {},
"dateCreate": 1522097081,
"block": false,
"lastEdit": 1522097081
}
}
public/users/:userId/answers
{
":answerId": {
"text": "Papalote has good burritos",
"creator": {},
"dateCreate": 1522097081,
"block": false,
"lastEdit": 1522097081,
"questionId": "abc123"
}
}
public/users/:userId/questionVotes
{
":questionId": true
}
public/users/:userId/answerVotes
{
":answerId": true
}
For simplicity, mobile clients download all questions, comments, and votes up front, with the expectation that a single event will not have too much data that would make this impractical.
The firebase data is mapped by the client to state:
questions
answersByQuestion
votesByQuestion
votesByAnswer
The mobile client side is critical for organizing and associating the objects. An example method is found below:
fbc.database.public.allRef('comments').on('child_added', data => {
this.setState(state => {
const comment = data.val()
const commentsForQuestion = this.state.comments[comment.questionId]
if (commentsForQuestion) {
var newCommentsForQuestion = [...commentsForQuestion, {...comment, key: data.key}]
} else {
var newCommentsForQuestion = [{...comment, key: data.key}]
}
return {comments: {...state.comments, [comment.questionId]: newCommentsForQuestion}}
})
})
Here we are organizing comments via the question key. This ensures that we can effictely organize comments even if data for the questions has not been downloaded. It also allows us to quickly find the correct comments to display for each question.