Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trả lời câu hỏi #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 105 additions & 14 deletions screens/AnswerQuestion.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,83 @@
import React from 'react';
import { SafeAreaView, Text, View, Image, StyleSheet, TouchableOpacity } from 'react-native';
import axios from 'axios';
import React, { useState, useEffect } from 'react';
import { SafeAreaView, Text, View, Image, StyleSheet, TouchableOpacity, Alert } from 'react-native';
//import {TouchableOpacity} from 'react-native-gesture-handler';
import Icon from 'react-native-vector-icons/Ionicons';
import API_URL from '../services/apiRoute';
import AsyncStorage from '@react-native-async-storage/async-storage';

const AnswerQuestion = ({navigation, route}) => {
const [questions, setQuestions] = useState([{}]);
const {topicId} = route.params;

useEffect(() => {
const getQuestionAnswers = async () => {
const userToken = await AsyncStorage.getItem('user_token');
axios
.get(`${API_URL}/api/user/questions-answers`, {params: {id: topicId}, headers: {'authentication': userToken}})
.then((res) => {
setQuestions(res.data);
if (!res.data?.length) return navigation.navigate("Tendency");
})
.catch((err) => console.log(err))
}
getQuestionAnswers()
},[]);

const postAnswers = async () => {
try {
const userToken = await AsyncStorage.getItem('user_token');
await axios
.post(`${API_URL}/api/user/questions-answers/answers`, { answers }, {
headers: {'authentication': userToken}
})
.then((res) => {
Alert.alert('thanh cong');
navigation.navigate("Tendency");
})
.catch((err) => Alert.alert('please try again'));
} catch (error) {
console.log(error);
}
}

const [currentQuestion, setCurrentQuestion] = useState(0);
const [selectedAnswer, setSelectedAnswer] = useState(null);
const [answers, setAnswers] = useState([]);

useEffect(() => {
const handle = async () => {
if (currentQuestion === questions.length) {
await postAnswers();
}
}
handle();
}, [currentQuestion]);

const handleSelectAnswer = (answerIndex) => {
setSelectedAnswer(answerIndex);
}

const handleSkipQuestion = () => {
if (selectedAnswer !== null) {
setAnswers(prevAnswers => [...prevAnswers, selectedAnswer]);
} else {
setAnswers(prevAnswers => [...prevAnswers, null]);
}
setSelectedAnswer(null);
setCurrentQuestion((prevQuestion) => prevQuestion + 1);
}

const handleContinue = () => {
if (selectedAnswer === null) {
Alert.alert('Please select an answer before continuing.');
} else {
setAnswers(prevAnswers => [...prevAnswers, selectedAnswer]);
setSelectedAnswer(null);
setCurrentQuestion((prevQuestion) => prevQuestion + 1);
}
}

const AnswerQuestion = () => {
return (
<SafeAreaView style={styles.container}>
<View
Expand All @@ -16,6 +90,7 @@ const AnswerQuestion = () => {
>
<Image source={require('../assets/img/HoneyaaLogo.png')} style={styles.logo} />
<TouchableOpacity
onPress={() => navigation.navigate("Home")}
style={[
styles.btnClose,
{
Expand All @@ -40,33 +115,49 @@ const AnswerQuestion = () => {
<Text style={styles.txtContent}>Tiêu đề</Text>
</View>
<View style={styles.wrapQuestion}>
<Text style={styles.txtQuestion}>Question</Text>
<Text style={styles.txtQuestion}>{questions[currentQuestion]?.content}</Text>
</View>
<TouchableOpacity>
<TouchableOpacity onPress={() => handleSelectAnswer(questions[currentQuestion]?.answers?.[0]?.id)}>
<View style={styles.wrapAnswer}>
<Text style={styles.txtAnswer}>Question</Text>
<Text
style={[styles.txtAnswer, selectedAnswer === questions[currentQuestion]?.answers?.[0]?.id && {color: '#007AFF'}]}
>
{questions[currentQuestion]?.answers?.[0]?.content}
</Text>
</View>
</TouchableOpacity>
<TouchableOpacity>
<TouchableOpacity onPress={() => handleSelectAnswer(questions[currentQuestion]?.answers?.[1]?.id)}>
<View style={styles.wrapAnswer}>
<Text style={styles.txtAnswer}>Question</Text>
<Text
style={[styles.txtAnswer, selectedAnswer === questions[currentQuestion]?.answers?.[1]?.id && {color: '#007AFF'}]}
>
{questions[currentQuestion]?.answers?.[1]?.content}
</Text>
</View>
</TouchableOpacity>
<TouchableOpacity>
<TouchableOpacity onPress={() => handleSelectAnswer(questions[currentQuestion]?.answers?.[2]?.id)}>
<View style={styles.wrapAnswer}>
<Text style={styles.txtAnswer}>Question</Text>
<Text
style={[styles.txtAnswer, selectedAnswer === questions[currentQuestion]?.answers?.[2]?.id && {color: '#007AFF'}]}
>
{questions[currentQuestion]?.answers?.[2]?.content}
</Text>
</View>
</TouchableOpacity>
<TouchableOpacity>
<TouchableOpacity onPress={() => handleSelectAnswer(questions[currentQuestion]?.answers?.[3]?.id)}>
<View style={styles.wrapAnswer}>
<Text style={styles.txtAnswer}>Question</Text>
<Text
style={[styles.txtAnswer, selectedAnswer === questions[currentQuestion]?.answers?.[3]?.id && {color: '#007AFF'}]}
>
{questions[currentQuestion]?.answers?.[3]?.content}
</Text>
</View>
</TouchableOpacity>
<View style={[{ flexDirection: 'row', width: '100%', justifyContent: 'space-between' }]}>
<TouchableOpacity style={styles.btnSkip}>
<Text style={styles.txtSkip}>Skip</Text>
<Text style={styles.txtSkip} onPress={() => handleSkipQuestion()}>Skip</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.btnContinue}>
<TouchableOpacity style={styles.btnContinue} onPress={() => handleContinue()}>
<Text style={styles.txtContinue}>Continue</Text>
</TouchableOpacity>
</View>
Expand Down
12 changes: 6 additions & 6 deletions screens/Discover.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { SafeAreaView, Text, View, Image, TouchableOpacity, StyleSheet } from 'react-native';
import { SafeAreaView, Text, View, Image, TouchableOpacity, StyleSheet, Alert } from 'react-native';

const Discover = () => {
const Discover = ({ navigation, route }) => {
return (
<SafeAreaView style={styles.container}>
<View style={[{ justifyContent: 'center', alignItems: 'center', marginTop: 70 }]}>
Expand Down Expand Up @@ -37,7 +37,7 @@ const Discover = () => {
</View>
<Text style={[styles.txt, { marginTop: 15 }]}>For you</Text>
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate("AnswerQuestion", {topicId: 1})}>
<View style={styles.wrapItem}>
<View style={styles.wrapImage}>
<Image
Expand All @@ -49,7 +49,7 @@ const Discover = () => {
<Text style={styles.txtItem}>Travel</Text>
</View>
</TouchableOpacity>
<TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate("AnswerQuestion", {topicId: 2})}>
<View style={styles.wrapItem}>
<View style={styles.wrapImage}>
<Image
Expand All @@ -63,7 +63,7 @@ const Discover = () => {
</TouchableOpacity>
</View>
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate("AnswerQuestion", {topicId: 3})}>
<View style={styles.wrapItem}>
<View style={styles.wrapImage}>
<Image
Expand All @@ -75,7 +75,7 @@ const Discover = () => {
<Text style={styles.txtItem}>Eat</Text>
</View>
</TouchableOpacity>
<TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate("AnswerQuestion", {topicId: 4})}>
<View style={styles.wrapItem}>
<View style={styles.wrapImage}>
<Image
Expand Down
Loading