diff --git a/chorechef_frontend/src/components/Chores.vue b/chorechef_frontend/src/components/Chores.vue index 0b5876ac96f37ec2fb66371972da55c7073e9fc8..7e7981244984e34cb749708d23464faef68fe61c 100644 --- a/chorechef_frontend/src/components/Chores.vue +++ b/chorechef_frontend/src/components/Chores.vue @@ -128,7 +128,6 @@ import "air-datepicker/air-datepicker.css"; <!-- Chore User --> <div v-if="editingChore === chore" class="w-2/12"> <select - v-on:keyup.enter="updateChore(chore)" v-model="chore.chore_user" class="rounded-xl px-3 shadow-lg py-0 bg-secondary-50 border-none focus:ring-2 focus:ring-secondary-600 w-7/12" > @@ -269,10 +268,10 @@ import "air-datepicker/air-datepicker.css"; v-model="addingChore.chore_user" class="rounded-xl px-3 shadow-lg py-0 bg-secondary-50 border-none focus:ring-2 focus:ring-secondary-600 w-7/12" :class="{ - 'border-red-600 border-4': addingChore.chore_user === '', + 'border-red-600 border-4': addingChore.chore_user === -1, }" > - <option value="" disabled>Nutzer</option> + <option value="-1" disabled>Nutzer</option> <option v-for="user in users" :value="user.user_id" @@ -288,7 +287,7 @@ import "air-datepicker/air-datepicker.css"; <div class="rounded-xl shadow-lg bg-secondary-100 hover:scale-110 hover:drop-shadow-lg hover:cursor-pointer px-1" v-if=" - addingChore.chore_name === '' || addingChore.chore_user === '' + addingChore.chore_name === '' || addingChore.chore_user === -1 " > <FontAwesomeIcon @@ -321,18 +320,14 @@ interface Chore { chore_user: number; chore_date: string; chore_done: boolean; + chore_frequency: number; + chore_frequency_unit: string; } interface User { user_id: number; username: string; } -var unitConversion = { - Tage: "days", - Wochen: "weeks", - Monate: "months", -}; - export default { data() { return { @@ -344,11 +339,24 @@ export default { }, props: { onDashboard: Boolean, currDate: Date }, methods: { + unitConversion(unitString: string): string { + switch (unitString) { + case "Tage": + return "days"; + case "Wochen": + return "weeks"; + case "Monate": + return "months"; + default: + return "days"; + } + }, forwardChore(chore: Chore) { // change date to next date + let unit = this.unitConversion(chore.chore_frequency_unit); let newDate: string = format( add(chore.chore_date, { - [unitConversion[chore.chore_frequency_unit]]: chore.chore_frequency, + [unit]: chore.chore_frequency, }), "yyyy-MM-dd", ); @@ -378,9 +386,10 @@ export default { }, backwardChore(chore: Chore) { // change date to previous date + let unit = this.unitConversion(chore.chore_frequency_unit); let newDate: string = format( add(chore.chore_date, { - [unitConversion[chore.chore_frequency_unit]]: -chore.chore_frequency, + [unit]: -chore.chore_frequency, }), "yyyy-MM-dd", ); @@ -410,7 +419,8 @@ export default { addChore() { this.addingChore = { chore_name: "", - chore_user: "", + chore_user: -1, + chore_id: -1, chore_date: format(new Date(), "yyyy-MM-dd"), chore_done: false, chore_frequency: 1, @@ -422,17 +432,27 @@ export default { locale: localeDe, dateFormat: "dd.MM.yyyy", onSelect: (ret) => { - console.log(ret.date); - console.log(ret.formattedDate); - this.addingChore.chore_date = format(ret.date, "yyyy-MM-dd"); + if (!this.addingChore) { + return; + } + if (ret.date instanceof Date) { + this.addingChore.chore_date = format(ret.date, "yyyy-MM-dd"); + } else { + this.addingChore.chore_date = format(new Date(), "yyyy-MM-dd"); + } }, }); }); }, saveNewChore() { + if (!this.addingChore) { + return; + } if ( this.addingChore.chore_name === "" || - this.addingChore.chore_user === "" + this.users.find( + (user) => user.user_id === this.addingChore?.chore_user, + ) === undefined ) { return; } @@ -444,12 +464,14 @@ export default { }, body: JSON.stringify(this.addingChore), }).then(() => { - this.allChores.push(this.addingChore); + this.allChores.push( + this.addingChore ? this.addingChore : ({} as Chore), + ); this.addingChore = null; }); }, editChore(chore: Chore) { - this.editingChore = chore; + this.editingChore = chore ? chore : ({} as Chore); //wait for next tick to create the datepicker this.$nextTick(() => { new AirDatepicker("#chore-date-picker", { @@ -459,7 +481,14 @@ export default { onSelect: (ret) => { console.log(ret.date); console.log(ret.formattedDate); - this.editingChore.chore_date = format(ret.date, "yyyy-MM-dd"); + if (!this.editingChore) { + return; + } + if (ret.date instanceof Date) { + this.editingChore.chore_date = format(ret.date, "yyyy-MM-dd"); + } else { + this.editingChore.chore_date = format(new Date(), "yyyy-MM-dd"); + } }, }); }); @@ -472,7 +501,7 @@ export default { }, body: JSON.stringify(chore), }).then(() => { - chore = this.editingChore; + chore = this.editingChore ? this.editingChore : chore; this.editingChore = null; }); }, @@ -513,6 +542,8 @@ export default { let theDate: Date; if (typeof date === "string") { theDate = new Date(date); + } else { + theDate = date; } return format(theDate, "dd.MM.yyyy"); }, @@ -523,7 +554,10 @@ export default { return this.allChores .filter((chore: Chore) => { return ( - this.compareDates(new Date(chore.chore_date), this.currDate) <= 0 + this.compareDates( + new Date(chore.chore_date), + this.currDate ? this.currDate : new Date(), + ) <= 0 ); }) .sort((a: Chore, b: Chore) => { @@ -532,7 +566,7 @@ export default { } else { return this.allChores .sort((a: Chore, b: Chore) => { - a.chore_id - b.chore_id; + return a.chore_id - b.chore_id; }) .sort((a: Chore, b: Chore) => { return compareAsc(a.chore_date, b.chore_date); diff --git a/chorechef_frontend/src/components/Mealplan.vue b/chorechef_frontend/src/components/Mealplan.vue index 85b164c68937d4dca0a1f7419b0660a039f0978e..ff36eacbfe897b9bcd899a03bc3a85a6acb814ff 100644 --- a/chorechef_frontend/src/components/Mealplan.vue +++ b/chorechef_frontend/src/components/Mealplan.vue @@ -71,7 +71,10 @@ import { ref } from "vue"; meals.find( (meal) => meal.meal_date === - format(add(currDate, { days: n - 1 }), "yyyy-MM-dd"), + format( + add(currDate ? currDate : new Date(), { days: n - 1 }), + "yyyy-MM-dd", + ), )?.meal_name }} </div> @@ -89,7 +92,10 @@ import { ref } from "vue"; meals.find( (meal) => meal.meal_date === - format(add(currDate, { days: n - 1 }), "yyyy-MM-dd"), + format( + add(currDate ? currDate : new Date(), { days: n - 1 }), + "yyyy-MM-dd", + ), )?.meal_description }} </div> @@ -130,9 +136,12 @@ export default { }, }, methods: { - mealOnPos(n: int) { + mealOnPos(n: number) { console.log("mealOnPos"); - const date = format(add(this.currDate, { days: n - 1 }), "yyyy-MM-dd"); + const date = format( + add(this.currDate ? this.currDate : new Date(), { days: n - 1 }), + "yyyy-MM-dd", + ); return ( this.meals.find((meal) => meal.meal_date === date) || { meal_name: "", @@ -142,11 +151,17 @@ export default { } ); }, - dateOnPos(n: int) { - return format(add(this.currDate, { days: n - 1 }), "EEEEEE, d. MMM"); + dateOnPos(n: number) { + return format( + add(this.currDate ? this.currDate : new Date(), { days: n - 1 }), + "EEEEEE, d. MMM", + ); }, - compDateOnPos(n: int) { - return format(add(this.currDate, { days: n - 1 }), "yyyy-MM-dd"); + compDateOnPos(n: number) { + return format( + add(this.currDate ? this.currDate : new Date(), { days: n - 1 }), + "yyyy-MM-dd", + ); }, async fetchMeals() { @@ -154,12 +169,15 @@ export default { const data = await response.json(); return data as Meal[]; }, - startEditing(n: int) { + startEditing(n: number) { this.editingMeal = this.mealOnPos(n); this.rollBackMeal = { ...this.editingMeal }; }, - deleteMeal(n: int) { - const date = format(add(this.currDate, { days: n - 1 }), "yyyy-MM-dd"); + deleteMeal(n: number) { + const date = format( + add(this.currDate ? this.currDate : new Date(), { days: n - 1 }), + "yyyy-MM-dd", + ); const index = this.meals.findIndex((meal) => meal.meal_date === date); if (index !== -1) { this.meals.splice(index, 1); diff --git a/chorechef_frontend/src/components/Settings.vue b/chorechef_frontend/src/components/Settings.vue index 361300dbdba24bdba5abf6938173ee2e33de00d4..b2101edce7f05b220fe7fa31a87a43c570b9af5a 100644 --- a/chorechef_frontend/src/components/Settings.vue +++ b/chorechef_frontend/src/components/Settings.vue @@ -117,7 +117,7 @@ export default { this.users = await response.json(); }, addUser() { - this.userToAdd = {}; + this.userToAdd = {} as User; }, editUser(user: User) { this.userToEdit = user;