#57 makeRequest uses env variable so all requests use the configuration

This commit is contained in:
2023-04-14 12:55:43 -04:00
parent 1f0e8ca905
commit c1a8f04dc7
7 changed files with 31 additions and 11 deletions

View File

@@ -4,11 +4,29 @@ services:
image: comp2707-frontend image: comp2707-frontend
build: frontend/ build: frontend/
container_name: comp2707-frontend container_name: comp2707-frontend
environment:
- BACKEND_URL="http://backend:5000"
ports: ports:
- 8080:8080 - 8080:8080
backend: backend:
image: comp2707-backend image: comp2707-backend
build: backend/ build: backend/
container_name: comp2707-backend container_name: comp2707-backend
environment:
- DB_HOST=db
- DB_USER=joe
- DB_PASSWORD=mama
ports: ports:
- 5000:5000 - 5000:5000
db:
image: mariadb:latest
environment:
- MARIADB_USER=joe
- MARIADB_PASWWORD=mama
- MARIADB_ROOT_PASSWORD=mama
volumes:
- db-volume:/var/lib/mysql
volumes:
db-volume:
driver: local

View File

@@ -12,7 +12,7 @@ const CoursesWidget = ({ className = "" }) => {
if (!currentUser.id) { if (!currentUser.id) {
return; return;
} }
makeRequest({ url: `http://localhost:5000/user/${currentUser.id}/courses` }) makeRequest({ endpoint: `user/${currentUser.id}/courses` })
.then((resp) => resp.json()) .then((resp) => resp.json())
.then((data) => { .then((data) => {
setCourseData(data.courses); setCourseData(data.courses);

View File

@@ -7,7 +7,7 @@ const CoursePage = ({ id }) => {
const [courseData, setCourseData] = useState({}); const [courseData, setCourseData] = useState({});
useEffect(() => { useEffect(() => {
makeRequest({ url: `http://localhost:5000/course/${id}` }) makeRequest({ endpoint: `course/${id}` })
.then((resp) => resp.json()) .then((resp) => resp.json())
.then((data) => { .then((data) => {
setCourseData(data); setCourseData(data);

View File

@@ -11,7 +11,7 @@ const ManagePage = () => {
useEffect(() => { useEffect(() => {
makeRequest({ makeRequest({
url: `http://localhost:5000/user/${currentUser.id}/courses`, endpoint: `user/${currentUser.id}/courses`,
method: "GET", method: "GET",
}) })
.then((req) => req.json()) .then((req) => req.json())

View File

@@ -9,7 +9,7 @@ const ManageStutentsPage = ({ cid }) => {
const submitStudentForm = async (username) => { const submitStudentForm = async (username) => {
await makeRequest({ await makeRequest({
url: `http://localhost:5000/user/${username}/enroll/${cid}`, endpoint: `user/${username}/enroll/${cid}`,
method: "POST", method: "POST",
}); });
window.location.reload(); window.location.reload();
@@ -48,7 +48,7 @@ const ManageStutentsPage = ({ cid }) => {
}; };
useEffect(() => { useEffect(() => {
makeRequest({ makeRequest({
url: `http://localhost:5000/course/${cid}/students`, endpoint: `course/${cid}/students`,
method: "GET", method: "GET",
}) })
.then((req) => req.json()) .then((req) => req.json())
@@ -62,7 +62,7 @@ const ManageStutentsPage = ({ cid }) => {
const sendUnenrollRequest = (uid) => { const sendUnenrollRequest = (uid) => {
makeRequest({ makeRequest({
url: `http://localhost:5000/user/${uid}/enroll/${cid}`, endpoint: `user/${uid}/enroll/${cid}`,
method: "DELETE", method: "DELETE",
}).then((resp) => { }).then((resp) => {
window.location.reload(); window.location.reload();

View File

@@ -17,7 +17,7 @@ const RegisterPage = () => {
const sendRegisterRequest = (e) => { const sendRegisterRequest = (e) => {
e?.preventDefault(); e?.preventDefault();
makeRequest({ makeRequest({
url: "http://localhost:5000/register", endpoint: "register",
method: "POST", method: "POST",
body: { body: {
role, role,

View File

@@ -1,4 +1,6 @@
const makeRequest = ({ url, method, body = null }): Promise<Response> => { const { REACT_APP_BACKEND_URL } = process.env;
const makeRequest = ({ endpoint, method, body = null }): Promise<Response> => {
const req: RequestInit = { const req: RequestInit = {
method: method, method: method,
credentials: "include", credentials: "include",
@@ -8,7 +10,7 @@ const makeRequest = ({ url, method, body = null }): Promise<Response> => {
if (body) { if (body) {
req["body"] = JSON.stringify(body); req["body"] = JSON.stringify(body);
} }
return fetch(url, req); return fetch(`${REACT_APP_BACKEND_URL}/${endpoint}`, req);
}; };
const sendLoginRequest = async ( const sendLoginRequest = async (
@@ -17,7 +19,7 @@ const sendLoginRequest = async (
): Promise<object> => { ): Promise<object> => {
const p: Promise<object> = new Promise(async (res) => { const p: Promise<object> = new Promise(async (res) => {
await makeRequest({ await makeRequest({
url: "http://localhost:5000/login", endpoint: "login",
method: "POST", method: "POST",
body: { username, password }, body: { username, password },
}) })
@@ -36,7 +38,7 @@ const sendLoginRequest = async (
const sendLogoutRequest = async (): Promise<object> => { const sendLogoutRequest = async (): Promise<object> => {
const p: Promise<object> = new Promise(async (res) => { const p: Promise<object> = new Promise(async (res) => {
await makeRequest({ await makeRequest({
url: "http://localhost:5000/logout", endpoint: "logout",
method: "POST", method: "POST",
}) })
.then((resp) => resp.json()) .then((resp) => resp.json())