Menu - Pre 9 .net

This commit is contained in:
Elias Jansson
2025-05-06 13:19:13 +02:00
parent 3cdf630af2
commit a140a59f66
19 changed files with 849 additions and 378 deletions

View File

@@ -208,3 +208,7 @@
.new-meal-form button:hover {
background-color: #F57C00;
}
.meal-hover {
cursor: pointer;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@@ -1,99 +1,84 @@
angular.module('mealMenuApp', [])
.controller('MealMenuController', function ($scope, $http) {
$scope.isEditing = false;
$scope.toggleEditMode = function () {
$scope.isEditing = !$scope.isEditing;
};
angular.module('mealMenuApp', ['ngSanitize'])
.controller('MealMenuController', function ($scope, $http, $sce) {
console.log("Controller initierad");
$scope.viewMode = 'list';
$scope.tooltip = {};
$scope.meals = [];
$scope.menu = {};
$scope.showNewMealForm = false;
$scope.newMeal = {};
$scope.selectedMealDay = null;
$scope.selectedMealType = null;
const today = new Date();
$scope.selectedWeek = getWeek(today);
$scope.selectedYear = today.getFullYear();
$scope.daysOfWeek = ["Måndag", "Tisdag", "Onsdag", "Torsdag", "Fredag", "Lördag", "Söndag"];
$scope.loadMeals = function () {
$http.get('/api/mealMenuApi/getMeals')
.then(response => {
$scope.meals = response.data;
console.log("Hämtar måltider...");
return $http.get('/api/mealMenuApi/getMeals')
.then(res => {
console.log("Måltider hämtade:", res.data);
$scope.meals = res.data;
return res;
})
.catch(error => console.error("Error fetching meals:", error));
.catch(err => console.error("Fel vid hämtning av måltider:", err));
};
$scope.loadMenu = function () {
console.log("Hämtar meny för vecka:", $scope.selectedWeek, $scope.selectedYear);
$http.get('/api/mealMenuApi/menu', {
params: { weekNumber: $scope.selectedWeek, year: $scope.selectedYear }
}).then(response => {
console.log("Veckomenydata:", response.data); // ✅ logga ut
}).then(res => {
console.log("Menyposter hämtade:", res.data);
$scope.menu = {};
response.data.forEach(item => {
const dayOfWeek = $scope.daysOfWeek[item.dayOfWeek - 1];
if (!$scope.menu[dayOfWeek]) $scope.menu[dayOfWeek] = {};
if (item.breakfastMealId) {
$scope.menu[dayOfWeek].breakfastMealId = item.breakfastMealId;
$scope.menu[dayOfWeek].breakfastMealName = getMealNameById(item.breakfastMealId);
}
if (item.lunchMealId) {
$scope.menu[dayOfWeek].lunchMealId = item.lunchMealId;
$scope.menu[dayOfWeek].lunchMealName = getMealNameById(item.lunchMealId);
}
if (item.dinnerMealId) {
$scope.menu[dayOfWeek].dinnerMealId = item.dinnerMealId;
$scope.menu[dayOfWeek].dinnerMealName = getMealNameById(item.dinnerMealId);
res.data.forEach(item => {
const dayIndex = item.DayOfWeek - 1;
if (dayIndex < 0 || dayIndex >= $scope.daysOfWeek.length) {
console.warn("Ogiltig dag:", item.DayOfWeek);
return;
}
const day = $scope.daysOfWeek[dayIndex];
$scope.menu[day] = {};
['breakfast', 'lunch', 'dinner'].forEach(type => {
// Konvertera till PascalCase
const capitalType = type.charAt(0).toUpperCase() + type.slice(1);
const idKey = capitalType + 'MealId';
const nameKey = capitalType + 'MealName';
if (item[idKey]) {
const m = $scope.meals.find(x => x.Id === item[idKey]);
console.log(`Match för ${type} (${day}):`, m);
$scope.menu[day][type + 'MealId'] = item[idKey];
$scope.menu[day][type + 'MealName'] = m?.Name || item[nameKey] || 'Okänd rätt';
if (m?.ImageUrl) {
$scope.menu[day].imageUrl = m.ImageUrl;
}
}
});
});
}).catch(error => console.error("Error fetching weekly menu:", error));
console.log("Byggd meny:", $scope.menu);
}).catch(err => console.error("Fel vid hämtning av veckomeny:", err));
};
function getMealNameById(mealId) {
const meal = $scope.meals.find(m => m.id === mealId);
return meal ? meal.name : "Unknown Meal";
}
$scope.handleMealSelection = function (day, mealType) {
if ($scope.menu[day][mealType + "MealId"] === "new") {
$scope.showNewMealForm = true;
$scope.newMeal = { name: "", description: "", proteinType: "", carbType: "", recipeUrl: "" };
$scope.selectedMealDay = day;
$scope.selectedMealType = mealType;
}
$scope.openMeal = function (mealId) {
if (!mealId) return;
window.open('/Meal/View/' + mealId, '_blank');
};
$scope.saveNewMeal = function () {
if (!$scope.newMeal.name) {
alert("Meal name is required");
return;
}
$http.post('/api/mealMenuApi/addMeal', $scope.newMeal)
.then(response => {
const addedMeal = response.data;
$scope.meals.push(addedMeal);
$scope.menu[$scope.selectedMealDay][$scope.selectedMealType + "MealId"] = addedMeal.id;
$scope.showNewMealForm = false;
$scope.newMeal = {}; // Reset new meal data after save
})
.catch(error => console.error("Error saving new meal:", error));
$scope.getDayImage = function (day) {
const item = $scope.menu[day];
return (item && item.imageUrl) || '/images/default-meal.jpg';
};
$scope.cancelNewMeal = function () {
$scope.showNewMealForm = false;
$scope.newMeal = {}; // Reset new meal data when canceled
$scope.getMealIdByDay = function (day) {
const item = $scope.menu[day] || {};
return item.dinnerMealId || item.lunchMealId || item.breakfastMealId || null;
};
function getWeek(date) {
const day = date.getDay() || 7;
date.setDate(date.getDate() + 4 - day);
const yearStart = new Date(date.getFullYear(), 0, 1);
return Math.ceil(((date - yearStart) / 86400000 + 1) / 7);
}
$scope.goToPreviousWeek = function () {
if ($scope.selectedWeek === 1) {
$scope.selectedYear--;
@@ -114,6 +99,17 @@
$scope.loadMenu();
};
$scope.loadMeals();
$scope.loadMenu();
function getWeek(d) {
d = new Date(d.getFullYear(), d.getMonth(), d.getDate());
const dayNum = d.getDay() || 7;
d.setDate(d.getDate() + 4 - dayNum);
const yearStart = new Date(d.getFullYear(), 0, 1);
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
}
console.log("Initierar måltidsladdning...");
$scope.loadMeals().then(() => {
console.log("Laddar meny efter måltider...");
$scope.loadMenu();
});
});