Last active
April 8, 2021 17:33
-
-
Save robinheinze/ca77a07c65f12537b5cf81dd110f6032 to your computer and use it in GitHub Desktop.
Question Screen with List
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, { useEffect, useState } from "react" | |
import { observer } from "mobx-react-lite" | |
import { FlatList, TextStyle, View, ViewStyle } from "react-native" | |
import { Screen, Text } from "../../components" | |
import { color, spacing } from "../../theme" | |
import { useStores } from "../../models" | |
import { Question } from "../../models/question/question" | |
import { decodeHTMLEntities } from "../../utils/html-decode" | |
const ROOT: ViewStyle = { | |
flex: 1, | |
backgroundColor: color.background, | |
paddingHorizontal: spacing.large, | |
} | |
const HEADER_CONTAINER: ViewStyle = { | |
marginTop: spacing.huge, | |
marginBottom: spacing.medium, | |
} | |
const QUESTION_LIST: ViewStyle = { | |
marginBottom: spacing.large, | |
} | |
const QUESTION_WRAPPER: ViewStyle = { | |
borderBottomColor: color.line, | |
borderBottomWidth: 1, | |
paddingVertical: spacing.large, | |
} | |
const QUESTION: TextStyle = { | |
fontWeight: "bold", | |
fontSize: 16, | |
marginVertical: spacing.medium, | |
} | |
const ANSWER: TextStyle = { | |
fontSize: 12, | |
} | |
const ANSWER_WRAPPER: ViewStyle = { | |
paddingVertical: spacing.smaller, | |
} | |
export const QuestionScreen = observer(function QuestionScreen() { | |
// Are we refreshing the data | |
const [refreshing, setRefreshing] = useState(false) | |
// Pull in one of our MST stores | |
const { questionStore } = useStores() | |
const { questions } = questionStore | |
useEffect(() => { | |
fetchQuestions() | |
}, []) | |
const fetchQuestions = () => { | |
setRefreshing(true) | |
questionStore.getQuestions() | |
setRefreshing(false) | |
} | |
const renderQuestion = ({ item }) => { | |
const question: Question = item | |
return ( | |
<View style={QUESTION_WRAPPER}> | |
<Text style={QUESTION} text={decodeHTMLEntities(question.question)} /> | |
<View> | |
{question.allAnswers.map((a, index) => { | |
return ( | |
<View key={index} style={ANSWER_WRAPPER}> | |
<Text style={ANSWER} text={decodeHTMLEntities(a)} /> | |
</View> | |
) | |
})} | |
</View> | |
</View> | |
) | |
} | |
return ( | |
<Screen style={ROOT} preset="fixed"> | |
<View style={HEADER_CONTAINER}> | |
<Text preset="header" tx="questionScreen.header" /> | |
</View> | |
<FlatList | |
style={QUESTION_LIST} | |
data={questionStore.questions} | |
renderItem={renderQuestion} | |
extraData={{ extraDataForMobX: questions.length > 0 ? questions[0].question : "" }} | |
keyExtractor={(item) => item.id} | |
onRefresh={fetchQuestions} | |
refreshing={refreshing} | |
/> | |
</Screen> | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment