From 690fb21ad33d11be2946fa2680a3d9ffd15f6f33 Mon Sep 17 00:00:00 2001
From: "Aamon P. Hoffmann" <aamon.hoffmann@student.uni-halle.de>
Date: Mon, 4 Nov 2024 17:59:18 +0100
Subject: [PATCH] added .env file

---
 chorechef_frontend/.env                        |  1 +
 chorechef_frontend/src/components/Chores.vue   | 15 ++++++++-------
 chorechef_frontend/src/components/Mealplan.vue |  9 +++++----
 chorechef_frontend/src/components/Settings.vue |  9 +++++----
 4 files changed, 19 insertions(+), 15 deletions(-)
 create mode 100644 chorechef_frontend/.env

diff --git a/chorechef_frontend/.env b/chorechef_frontend/.env
new file mode 100644
index 0000000..39f80fd
--- /dev/null
+++ b/chorechef_frontend/.env
@@ -0,0 +1 @@
+VITE_BACKEND_URL=http://localhost:8001
diff --git a/chorechef_frontend/src/components/Chores.vue b/chorechef_frontend/src/components/Chores.vue
index 7e79812..ed4a7dc 100644
--- a/chorechef_frontend/src/components/Chores.vue
+++ b/chorechef_frontend/src/components/Chores.vue
@@ -327,6 +327,7 @@ interface User {
   user_id: number;
   username: string;
 }
+const BACKEND_URL = import.meta.env.VITE_BACKEND_URL;
 
 export default {
   data() {
@@ -367,7 +368,7 @@ export default {
             this.users.length
         ].user_id;
 
-      fetch(`http://localhost:8000/chores/${chore.chore_id}/`, {
+      fetch(`${BACKEND_URL}/chores/${chore.chore_id}/`, {
         method: "PATCH",
         headers: {
           "Content-Type": "application/json",
@@ -402,7 +403,7 @@ export default {
             this.users.length
         ].user_id;
 
-      fetch(`http://localhost:8000/chores/${chore.chore_id}/`, {
+      fetch(`${BACKEND_URL}/chores/${chore.chore_id}/`, {
         method: "PATCH",
         headers: {
           "Content-Type": "application/json",
@@ -457,7 +458,7 @@ export default {
         return;
       }
 
-      fetch("http://localhost:8000/chores/", {
+      fetch(`${BACKEND_URL}/chores/`, {
         method: "POST",
         headers: {
           "Content-Type": "application/json",
@@ -494,7 +495,7 @@ export default {
       });
     },
     saveChore(chore: Chore) {
-      fetch(`http://localhost:8000/chores/${chore.chore_id}/`, {
+      fetch(`${BACKEND_URL}/chores/${chore.chore_id}/`, {
         method: "PUT",
         headers: {
           "Content-Type": "application/json",
@@ -506,7 +507,7 @@ export default {
       });
     },
     deleteChore(chore: Chore) {
-      fetch(`http://localhost:8000/chores/${chore.chore_id}/`, {
+      fetch(`${BACKEND_URL}/chores/${chore.chore_id}/`, {
         method: "DELETE",
       }).then(() => {
         this.allChores = this.allChores.filter(
@@ -575,12 +576,12 @@ export default {
     },
   },
   async mounted() {
-    fetch("http://localhost:8000/chores/")
+    fetch(`${BACKEND_URL}/chores/`)
       .then((response) => response.json())
       .then((data) => {
         this.allChores = data;
       });
-    fetch("http://localhost:8000/users/")
+    fetch(`${BACKEND_URL}/users/`)
       .then((response) => response.json())
       .then((data) => {
         // sort by id to keep the order consistent
diff --git a/chorechef_frontend/src/components/Mealplan.vue b/chorechef_frontend/src/components/Mealplan.vue
index ff36eac..51cd708 100644
--- a/chorechef_frontend/src/components/Mealplan.vue
+++ b/chorechef_frontend/src/components/Mealplan.vue
@@ -107,6 +107,7 @@ import { ref } from "vue";
 import { format, add, setDefaultOptions } from "date-fns";
 import { de } from "date-fns/locale";
 setDefaultOptions({ locale: de });
+const BACKEND_URL = import.meta.env.VITE_BACKEND_URL;
 interface Meal {
   meal_name: string;
   meal_description: string;
@@ -165,7 +166,7 @@ export default {
     },
 
     async fetchMeals() {
-      const response = await fetch("http://localhost:8000/meals");
+      const response = await fetch(`${BACKEND_URL}/meals`);
       const data = await response.json();
       return data as Meal[];
     },
@@ -182,7 +183,7 @@ export default {
       if (index !== -1) {
         this.meals.splice(index, 1);
       }
-      fetch(`http://localhost:8000/meals/${date}/`, {
+      fetch(`${BACKEND_URL}/${date}/`, {
         method: "DELETE",
       });
     },
@@ -198,7 +199,7 @@ export default {
       console.log(JSON.stringify(this.editingMeal));
 
       if (index === -1) {
-        let resp = await fetch("http://localhost:8000/meals/", {
+        let resp = await fetch(`${BACKEND_URL}/meals/`, {
           method: "POST",
           headers: {
             "Content-Type": "application/json",
@@ -208,7 +209,7 @@ export default {
         console.log(resp);
         console.log(resp.json());
       } else {
-        fetch(`http://localhost:8000/meals/${this.editingMeal!.meal_date}/`, {
+        fetch(`${VITE_BACKEND_URL}/meals/${this.editingMeal!.meal_date}/`, {
           method: "PUT",
           headers: {
             "Content-Type": "application/json",
diff --git a/chorechef_frontend/src/components/Settings.vue b/chorechef_frontend/src/components/Settings.vue
index b2101ed..f8abf13 100644
--- a/chorechef_frontend/src/components/Settings.vue
+++ b/chorechef_frontend/src/components/Settings.vue
@@ -98,6 +98,7 @@ import {
 </template>
 
 <script lang="ts">
+const BACKEND_URL = import.meta.env.VITE_BACKEND_URL;
 interface User {
   user_id: number;
   username: string;
@@ -113,7 +114,7 @@ export default {
   },
   methods: {
     async fetchUsers() {
-      const response = await fetch("http://localhost:8000/users/");
+      const response = await fetch(`${BACKEND_URL}/users/`);
       this.users = await response.json();
     },
     addUser() {
@@ -123,7 +124,7 @@ export default {
       this.userToEdit = user;
     },
     deleteUser(user: User) {
-      fetch("http://localhost:8000/users/" + user.user_id + "/", {
+      fetch(`${BACKEND_URL}/users/${user.user_id}/`, {
         method: "DELETE",
       }).then(() => {
         this.fetchUsers();
@@ -131,7 +132,7 @@ export default {
     },
     updateUser(user: User) {
       console.log(user);
-      fetch("http://localhost:8000/users/" + user.user_id + "/", {
+      fetch(`${BACKEND_URL}/users/${user.user_id}/`, {
         method: "PUT",
         headers: {
           "Content-Type": "application/json",
@@ -143,7 +144,7 @@ export default {
       });
     },
     saveUser() {
-      fetch("http://localhost:8000/users/", {
+      fetch(`${BACKEND_URL}/users/`, {
         method: "POST",
         headers: {
           "Content-Type": "application/json",
-- 
GitLab