Skip to content

Instantly share code, notes, and snippets.

@daviddsp
Last active July 27, 2018 06:23
Show Gist options
  • Select an option

  • Save daviddsp/0af3d1d6a2ab2eda88123787cd7068e0 to your computer and use it in GitHub Desktop.

Select an option

Save daviddsp/0af3d1d6a2ab2eda88123787cd7068e0 to your computer and use it in GitHub Desktop.
quiz.js
import React, { Component } from 'react';
import styles from '../style';
import { Text, View, StyleSheet, Image,Button,TextInput,TouchableHighlight, Alert,
Keyboard,ScrollView, AsyncStorage, FlatList} from 'react-native';
import Icon from 'react-native-vector-icons/SimpleLineIcons';
import { List, ListItem } from 'react-native-elements'
import Animbutton from '../common/animbutton';
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
//const token='';
class Choice extends Component{
constructor(props) {
super(props);
this.state = { answer: '' } ;
}
/*method(question) {
if ( this.props.answer == question) {
console.log("si es");
//this.props.history.push('/signup');
this.props.navigator.push("signup")
this.props.navigator.replace({
component: Signup,
title: 'Signup',
})
//this.props.navigation.push('taskdue')
} else {
console.log("Noooo");
}
}*/
answer(text) {
this.setState({ answer: text })
}
render(){
return <Animbutton
onColor={"gray"}
effect={"bounce"}
text={this.props.text}
_onPress={
(status) => {
if (status == true){
if ( this.props.answer == this.props.text) {
//console.log("si es");
this.props.navigation.navigate('Quizcorrect')
} else {
console.log("Noooo");
this.props.navigation.navigate('quizCorrect')
}
}
}
}/>
}
}
class Choices extends Component{
constructor(props) {
super(props);
}
options = this.props.options.map((option) => {
cont = (Math.random()*100).toFixed(0)
return (
<View key={cont}>
<Choice
text={option}
answer={this.props.answer}
action={this.props.action}
></Choice>
</View>
)
})
render(){
return this.options
}
}
export default class Quiz extends Component {
constructor(props) {
super(props);
choice = new Choice();
this.state = {
safetyQuestionnaire: {},
quizQuesitonnaire : {
questions : []
}
}
}
componentDidMount(){
this.GetDatacheckin();
}
async GetDatacheckin(){
try {
var Token = await AsyncStorage.getItem('@MySuperStore:token');
} catch(error){
console.log('Error Checkin')
}
//Token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjIiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiZ2VuQHRob3VnaHRsYWIuY29tIiwiQXNwTmV0LklkZW50aXR5LlNlY3VyaXR5U3RhbXAiOiJUUEZFVllTWlIzTU9RRE1YRzJWRUhZWjZTTFdZSUVLMiIsImh0dHA6Ly9zY2hlbWFzLmFja25vd2xvZ3kuY29tL2NsYWltcy9oYXNkYXNoYm9hcmRhY2Nlc3MiOiIiLCJodHRwOi8vc2NoZW1hcy5hY2tub3dsb2d5LmNvbS9jbGFpbXMvdGVuYW50IjoiMyIsImp0aSI6IjRhN2I1ZDYyLWJhZTktNDU4MS04ZDVjLTRjZjA4YjEzODM5ZSIsImlhdCI6MTUzMTc1MjA5OSwiZXhwIjoxNTM0MzQ0MDk5LCJpc3MiOiJBY2tub3dsb2d5LkFQSSIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NjEyOTUvIn0.v78odj6nPqu6VRZ9d_IY9pt1D-R4ycJuM4JqSnM775Q'
var url = 'http://api.acknowlogy.tl2.thoughtlab.info/api/v1/checkin';
fetch (url, {
method: 'POST',
body: JSON.stringify({"projectId": 1, }),
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + Token
}
})
.then((res) => res.json())
.then((responseJson) => {
let previous = this.state
previous.quizQuesitonnaire = responseJson.quizQuesitonnaire
choice.answer(previous.quizQuesitonnaire.questions[0].metaData.answer)
this.setState(previous)
}).catch((error) =>{
console.error(error);
});
}
_onPressButton() {
console.log("llegp")
}
handler() {
console.log('Esto es un evento del padre')
/*
this.setState({
messageShown: true
});
*/
}
async GetSafetyQuestionnaire(){
try {
const value = await AsyncStorage.getItem('@MySuperStore:safetyQuestionnaire');
let previous = this.state
previous.safetyQuestionnaire = value
this.setState(previous)
console.log(this.state)
} catch (error) {
console.log("Error retrieving data" + error);
}
}
render() {
Quiz = this.state.quizQuesitonnaire.questions.map((item,k) => {
cont = (Math.random()*100).toFixed(0)
console.log(cont)
return (
<View key={'Question-'+item.id}>
<View style={styles.formSafety}>
<Text style={{fontWeight:'bold', marginBottom:-20, color: '#E6E6E6',}}>Question:</Text>
<Text style={styles.date}>
{item.text}
</Text>
</View>
<Choices
id={'Choise-'+cont}
options={item.metaData.choices}
answer={item.metaData.answer}
action={this.handler}
onPress={this._onPressButton}
></Choices>
</View>
)
});
return (
<ScrollView>
<View style={styles.container}>
<Image
source={require('../assets/background5.png')}
style={styles.bgapp}
/>
<Text style={styles.date}>{today}</Text>
<Text style={styles.tittleSafety}>Safety Quiz</Text>
<View style={styles.timeline}>
<View style={styles.block}>
<Text style={styles.inactive}>1</Text>
<Text style={styles.personal}>Task</Text>
</View>
<View style={styles.block}>
<Text style={styles.active}>2</Text>
<Text style={styles.personal}>Quiz</Text>
</View>
<View style={styles.block}>
<Text style={styles.inactive}>3</Text>
<Text style={styles.personal}>Concern</Text>
</View>
<View style={styles.block}>
<Text style={styles.inactive}>4</Text>
<Text style={styles.personal}>Review</Text>
</View>
</View>
<View style={styles.formSafety}>
<View>
{Quiz}
</View>
</View>
<View>
</View>
</View>
</ScrollView>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment