Skip to content

Commit

Permalink
Fix the challenge retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
joshSzep committed Jul 14, 2024
1 parent cedd955 commit 6d30bd6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

50 changes: 46 additions & 4 deletions frontend/templates/frontend/tutor.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
}

function App() {
const [challengeGroups, setChallengeGroups] = React.useState([]);
const [currentGroup, setCurrentGroup] = React.useState(null);
const [challengeIds, setChallengeIds] = React.useState([]);
const [currentChallengeIndex, setCurrentChallengeIndex] = React.useState(0);
const [currentChallenge, setCurrentChallenge] = React.useState(null);
const [input, setInput] = React.useState('');
const [startTime, setStartTime] = React.useState(null);
Expand All @@ -114,18 +118,37 @@
const inputRef = React.useRef(null);

React.useEffect(() => {
fetchChallenge();
fetchChallengeGroups();
}, []);

React.useEffect(() => {
if (currentGroup) {
setChallengeIds(currentGroup.challenges);
setCurrentChallengeIndex(0);
}
}, [currentGroup]);

React.useEffect(() => {
if (challengeIds.length > 0 && currentChallengeIndex < challengeIds.length) {
fetchChallenge(challengeIds[currentChallengeIndex]);
}
}, [challengeIds, currentChallengeIndex]);

React.useEffect(() => {
if (challengeIds.length > 0 && currentChallengeIndex < challengeIds.length) {
fetchChallenge(challengeIds[currentChallengeIndex]);
}
}, [challengeIds, currentChallengeIndex]);

React.useEffect(() => {
if (isChallengeLoaded && inputRef.current) {
inputRef.current.focus();
}
}, [isChallengeLoaded]);

const fetchChallenge = async () => {
const fetchChallenge = async (challengeId) => {
try {
const response = await fetch('../api/v1/challenges/challenge:0669309f-4572-714f-8000-3acabe8e0abf');
const response = await fetch(`../api/v1/challenges/${challengeId}`);
if (!response.ok) {
throw new Error('Failed to fetch challenge');
}
Expand All @@ -138,6 +161,23 @@
}
};

const fetchChallengeGroups = async () => {
try {
const response = await fetch('../api/v1/challenge_groups/');
if (!response.ok) {
throw new Error('Failed to fetch challenge groups');
}
const data = await response.json();
setChallengeGroups(data);
if (data.length > 0) {
setCurrentGroup(data[0]);
}
} catch (error) {
console.error('Error fetching challenge groups:', error);
setDebug('Error fetching challenge groups. Please try again.');
}
};

const handleInputChange = (e) => {
const newInput = e.target.value;
if (!startTime) {
Expand Down Expand Up @@ -185,7 +225,9 @@
setShowAccuracy(true);

setTimeout(() => {
fetchChallenge();
const nextIndex = (currentChallengeIndex + 1) % challengeIds.length;
setCurrentChallengeIndex(nextIndex);
setIsChallengeLoaded(false);
setInput('');
setStartTime(null);
setShowEnglish(false);
Expand Down
5 changes: 1 addition & 4 deletions render.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ services:
name: hangul_typing_tutor
env: python
buildCommand: ./render_build.sh
startCommand: gunicorn hangul_tutor.wsgi:application
envVars:
- key: PORT
value: 1000
startCommand: ./render_run.sh

0 comments on commit 6d30bd6

Please sign in to comment.