Skip to content

Commit

Permalink
Resolved issues, ready for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
FarmerJohnsBessie committed Jul 16, 2024
1 parent 5086852 commit 5edc9ae
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 75 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_API_URL=http://localhost:8000
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_API_URL=https://satduel-e07814415d4e.herokuapp.com
3 changes: 2 additions & 1 deletion src/components/BattleHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const MatchHistory = ({ user_id = null }) => {
useEffect(() => {
const fetchMatchHistory = async () => {
try {
const url = isOwnHistory ? '/api/match/get_match_history/' : `/api/match/get_match_history/${user_id}/`;
const baseUrl = process.env.REACT_APP_API_URL;
const url = isOwnHistory ? `${baseUrl}/api/match/get_match_history/` : `${baseUrl}/api/match/get_match_history/${user_id}/`;
const response = await axios.get(url, {
headers: {
Authorization: `Bearer ${token}`
Expand Down
15 changes: 8 additions & 7 deletions src/components/ConfirmEmail.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// src/ConfirmEmail.js
import React, {useEffect, useState} from 'react';
import {useParams, useNavigate} from 'react-router-dom';
import React, { useEffect, useState } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import axios from 'axios';
import {message, Spin} from 'antd';
import { message, Spin } from 'antd';

const ConfirmEmail = () => {
const {key} = useParams();
const { key } = useParams();
const navigate = useNavigate();
const [loading, setLoading] = useState(true);

useEffect(() => {
const confirmEmail = async () => {
try {
await axios.post('/auth/registration/verify-email/', {
const baseUrl = process.env.REACT_APP_API_URL;
await axios.post(`${baseUrl}/auth/registration/verify-email/`, {
key: key
});
message.success('Email confirmed successfully!');
Expand All @@ -28,8 +29,8 @@ const ConfirmEmail = () => {
}, [key, navigate]);

return (
<div style={{textAlign: 'center', marginTop: '50px'}}>
{loading ? <Spin size="large"/> : null}
<div style={{ textAlign: 'center', marginTop: '50px' }}>
{loading ? <Spin size="large" /> : null}
</div>
);
};
Expand Down
5 changes: 3 additions & 2 deletions src/components/FriendList.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const FriendsList = () => {
useEffect(() => {
const fetchFriends = async () => {
try {
const response = await axios.get('/api/profile/friends/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.get(`${baseUrl}/api/profile/friends/`, {
headers: {
'Authorization': `Bearer ${token}`
}
Expand Down Expand Up @@ -49,4 +50,4 @@ const FriendsList = () => {
);
};

export default FriendsList;
export default FriendsList;
6 changes: 4 additions & 2 deletions src/components/FriendRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const FriendRequests = () => {
useEffect(() => {
const fetchFriendRequests = async () => {
try {
const response = await axios.get('/api/profile/friend_requests/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.get(`${baseUrl}/api/profile/friend_requests/`, {
headers: {
'Authorization': `Bearer ${token}`
}
Expand All @@ -26,7 +27,8 @@ const FriendRequests = () => {

const respondToRequest = async (requestId, status) => {
try {
await axios.post(`/api/profile/respond_friend_request/${requestId}/`, { status }, {
const baseUrl = process.env.REACT_APP_API_URL;
await axios.post(`${baseUrl}/api/profile/respond_friend_request/${requestId}/`, { status }, {
headers: {
'Authorization': `Bearer ${token}`
}
Expand Down
9 changes: 6 additions & 3 deletions src/components/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ function Profile({user_id = null}) {
useEffect(() => {
const fetchProfile = async () => {
try {
const url = isOwnProfile ? '/api/profile/' : `/api/profile/view_profile/${user_id}/`;
const baseUrl = process.env.REACT_APP_API_URL;
const url = isOwnProfile ? `${baseUrl}/api/profile/` : `${baseUrl}/api/profile/view_profile/${user_id}/`;
const response = await axios.get(url, {
headers: {
'Authorization': `Bearer ${token}`
Expand Down Expand Up @@ -73,7 +74,8 @@ function Profile({user_id = null}) {
const onFinish = async (values) => {
if (!isOwnProfile) return;
try {
const response = await axios.patch('/api/profile/update_biography/', values, {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.patch(`${baseUrl}/api/profile/update_biography/`, values, {
headers: {
'Authorization': `Bearer ${token}`
}
Expand All @@ -95,7 +97,8 @@ function Profile({user_id = null}) {

const sendFriendRequest = async () => {
try {
await axios.post('/api/profile/send_friend_request/',
const baseUrl = process.env.REACT_APP_API_URL;
await axios.post(`${baseUrl}/api/profile/send_friend_request/`,
{to_user_id: user_id},
{
headers: {
Expand Down
5 changes: 3 additions & 2 deletions src/components/Question.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ function Question({questionData, onSubmit, status, questionNumber}) {
useEffect(() => {
const getAnswer = async () => {
try {
const response = await axios.post('/api/get_answer/', {question_id: id});
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/get_answer/`, {question_id: id});
setAnswer(response.data.answer);
setExplanation(response.data.explanation);
setAnswerChoice(response.data.answer_choice);
Expand Down Expand Up @@ -217,4 +218,4 @@ function Question({questionData, onSubmit, status, questionNumber}) {
);
}

export default Question;
export default Question;
3 changes: 2 additions & 1 deletion src/components/SearchUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const SearchUsers = () => {
setQuery(value); // Update the query state with the current search input
if (value) {
try {
const response = await axios.get(`/api/profile/search/?q=${value}`, {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.get(`${baseUrl}/api/profile/search/?q=${value}`, {
headers: {
'Authorization': `Bearer ${token}`
}
Expand Down
10 changes: 6 additions & 4 deletions src/context/AuthContext.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { createContext, useContext, useState, useEffect } from 'react';
import axios from "axios";
import Cookies from 'js-cookie';
import {message} from 'antd';
import { message } from 'antd';

const AuthContext = createContext(null);

Expand All @@ -21,7 +21,8 @@ export const AuthProvider = ({ children }) => {

const logout = async () => {
try {
let response = await axios.post('/api/logout/');
const baseUrl = process.env.REACT_APP_API_URL;
let response = await axios.post(`${baseUrl}/api/logout/`);
setUser(null);
setToken(null);
Cookies.remove('accessToken');
Expand All @@ -36,7 +37,8 @@ export const AuthProvider = ({ children }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
const refreshAccessToken = async () => {
try {
const response = await axios.post('/api/token/refresh/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/token/refresh/`, {
refresh: JSON.parse(Cookies.get('refreshToken'))
});
const newAccessToken = response.data.access;
Expand All @@ -58,7 +60,7 @@ export const AuthProvider = ({ children }) => {
console.error('Error getting user from cookie', error);
}
setLoading(false);
},[loading]);
}, [loading]);

useEffect(() => {
if (token) {
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useOpponentProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { useAuth } from "../context/AuthContext";

const fetchOpponentProgress = async ({ roomId, token, setOpponentProgress }) => {
try {
const response = await axios.post('/api/match/get_opponent_progress/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/match/get_opponent_progress/`, {
room_id: roomId,
}, {
headers: {
Expand Down
20 changes: 13 additions & 7 deletions src/pages/BattleResultPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ function BattleResultPage() {
const [results, setResults] = useState(null);
const navigate = useNavigate();


useEffect(() => {
const fetchResults = async () => {
try {
const response = await axios.post('/api/match/get_results/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/match/get_results/`, {
room_id: roomId,
});
setResults(response.data);
Expand All @@ -193,29 +193,35 @@ function BattleResultPage() {
const user2Score = calculateScore(results.user2_results);
const winner = user1Score > user2Score ? results.user1_results[0].user.username :
user2Score > user1Score ? results.user2_results[0].user.username : 'Tie';

const setWinner = async (winner) => {
try {
await axios.post('/api/match/set_winner/', {
const baseUrl = process.env.REACT_APP_API_URL;
await axios.post(`${baseUrl}/api/match/set_winner/`, {
room_id: roomId,
winner: winner
});
} catch (err) {
console.error('Error setting winner:', err);
}
}
};

const setScore = async () => {
try {
await axios.post('/api/match/set_score/', {
const baseUrl = process.env.REACT_APP_API_URL;
await axios.post(`${baseUrl}/api/match/set_score/`, {
room_id: roomId,
user1_score: user1Score,
user2_score: user2Score,
});
} catch (err) {
console.error('Error setting score:', err);
}
}
};

setScore();
setWinner(winner);

return (
<PageBackground>
<ResultPageContainer>
Expand Down Expand Up @@ -256,4 +262,4 @@ function BattleResultPage() {
);
}

export default BattleResultPage;
export default BattleResultPage;
31 changes: 16 additions & 15 deletions src/pages/DuelBattlePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const TimeDisplay = styled.div`
margin-top: 16px;
`;


const DuelBattle = () => {
const {token, loading} = useAuth(); // Get the token from the AuthContext
const {roomId} = useParams(); // Get the roomId from the URL params
Expand All @@ -62,11 +61,11 @@ const DuelBattle = () => {
const [timeLeft, setTimeLeft] = useState(null);
const navigate = useNavigate();


useEffect(() => {
const fetchEndTime = async () => {
try {
const response = await axios.post('/api/match/get_end_time/',
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/match/get_end_time/`,
{
room_id: roomId
});
Expand All @@ -82,11 +81,11 @@ const DuelBattle = () => {
useEffect(() => {
const endMatch = async () => {
try {
await axios.post('/api/match/end_match/', {
const baseUrl = process.env.REACT_APP_API_URL;
await axios.post(`${baseUrl}/api/match/end_match/`, {
room_id: roomId
});
} catch
(err) {
} catch (err) {
message.error(err.response.data.error || 'An error occurred while ending the match.');
}
};
Expand Down Expand Up @@ -119,7 +118,8 @@ const DuelBattle = () => {
useOpponentProgress(roomId, setOpponentProgress);

const fetchTrackedQuestions = async (roomId, token) => {
const response = await axios.post('/api/match/questions/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/match/questions/`, {
room_id: roomId
}, {
headers: {
Expand All @@ -131,7 +131,8 @@ const DuelBattle = () => {

// Fetch full question details for a given question ID
const fetchQuestionDetails = async (questionId, token) => {
const response = await axios.post('/api/get_question/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/get_question/`, {
question_id: questionId
}, {
headers: {
Expand All @@ -142,8 +143,6 @@ const DuelBattle = () => {
};

// Combine tracked questions with their full details


useEffect(() => {
const combineQuestionsWithDetails = async (trackedQuestions, token) => {
const questionPromises = trackedQuestions.map(async (trackedQuestion) => {
Expand Down Expand Up @@ -180,15 +179,17 @@ const DuelBattle = () => {
}, [roomId, token, loading]);

const checkAnswer = async (id, choice) => {
console.log(`Question: ${id}, Chosen answer: ${choice}`);
const response = await axios.post('/api/check_answer/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/check_answer/`, {
question_id: id,
selected_choice: choice
});
return response.data.result;
};

const updateStatus = async (tracked_question_id, result) => {
const response = await axios.post('/api/match/update/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/match/update/`, {
tracked_question_id: tracked_question_id,
result: result
},
Expand All @@ -204,7 +205,8 @@ const DuelBattle = () => {
));
}
console.log(response.data);
}
};

const handleQuestionSubmit = async (id, choice) => {
const trackedQuestionId = trackedQuestionMap[id]; // Get the tracked question ID from the map
const result = await checkAnswer(id, choice);
Expand All @@ -218,7 +220,6 @@ const DuelBattle = () => {
return <div>Loading battle information...</div>;
}


return (
<DuelBattleContainer>
<QuestionsSection>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function HomePage() {
<Row gutter={16}>
<Col>
<StyledButton>
<Link to="/questions">Start Practice</Link>
<Link to="/trainer">Start Practice</Link>
</StyledButton>
</Col>
<Col>
Expand Down
8 changes: 5 additions & 3 deletions src/pages/InfiniteQuestionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ function InfiniteQuestionsPage() {
const fetchNextQuestion = async () => {
try {
setLoading(true);
const response = await axios.get('/api/questions/?num=1');
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.get(`${baseUrl}/api/questions/?num=1`);
setCurrentQuestion(response.data[0]);
setQuestionStatus('Blank');
} catch (error) {
Expand All @@ -108,7 +109,8 @@ function InfiniteQuestionsPage() {

const handleQuestionSubmit = async (id, choice) => {
try {
const response = await axios.post('/api/check_answer/', {
const baseUrl = process.env.REACT_APP_API_URL;
const response = await axios.post(`${baseUrl}/api/check_answer/`, {
question_id: id,
selected_choice: choice
});
Expand Down Expand Up @@ -175,4 +177,4 @@ function InfiniteQuestionsPage() {
);
}

export default InfiniteQuestionsPage;
export default InfiniteQuestionsPage;
Loading

0 comments on commit 5edc9ae

Please sign in to comment.