Files
pleno-vue/src/components/menus/admin/AdminLeftMenu.vue
T

335 lines
11 KiB
Vue

<script setup>
import MenuDefault from "@/components/menus/MenuDefault.vue";
import { ref, watch, onMounted } from "vue";
import { SessionUser } from "@/components/session/token/SessionUser.vue";
import { useRoute } from "vue-router";
import { isAccessibleVisibleNamedDepartment, sortByDepartmentPriorityOrder } from "@/services/departmentVisibility.js";
// Get the department ID from the URL
const route = useRoute();
const department_id = route.params.departmentId || SessionUser.functions.getDepartmentIdFromUrl() || null;
// Define the isLoaded function
// This is here, because the departments need to be loaded before the menu is shown,
// otherwise the menu will look for a department with an id not defined.
const isLoaded = ref(false);
// Load the departments
const departments = ref([]);
const loadDepartments = () => {
SessionUser.objects.departments.get
.all()
.then((departments) => {
const options = [];
sortByDepartmentPriorityOrder(departments).forEach((department) => {
options.push({
label: department.name,
value: department.id,
icon: "fas fa-building",
children: [],
hidden: !isAccessibleVisibleNamedDepartment(department, SessionUser.canAccessAssignedDepartment),
});
});
menu_items.value[1].options = options;
isLoaded.value = true;
})
.catch((error) => {
console.error(error);
});
};
loadDepartments();
// Check if the department ID is undefined
const isDepartmentIdUndefined = () => {
return SessionUser.functions.getDepartmentIdFromUrl() === undefined;
};
// Check for changes in the users permissions
watch(SessionUser.permissions, () => {
loadDepartments();
});
// Check for changes in the url
watch(route, () => {
// If the department ID in the URL changes, reset the emphasis checks
if (route.params.departmentId !== department_id) {
department_id.value = route.params.departmentId || SessionUser.functions.getDepartmentIdFromUrl() || null;
checkEmphasis(true); // Forcefully check emphasis for the new department
}
});
const emphasis = ref({
// Bookings
bookings: {
emphasis: false,
lastChecked: null,
lastCheckedDepartmentId: null, // Store the last checked department ID
checkFunction: () => {
/**
* Check if the department has any pending bookings planned for today
*/
SessionUser.request(SessionUser.objects.bookings.meta.endpoint, "GET", {
filters:
"department:" +
department_id +
",status:pending,date-date_from:" +
new Date().toISOString().split("T")[0] +
",date-date_to:" +
new Date().toISOString().split("T")[0],
limit: 1, // Limit to 1 booking, we only need to know if there are any bookings or not
page: 1,
})
.then((response) => {
emphasis.value.bookings.emphasis = response.data.data.length > 0;
})
.catch((error) => {
console.error(error);
});
},
},
// Daily Report
dailyReport: {
emphasis: false,
lastChecked: null,
lastCheckedDepartmentId: null, // Store the last checked department ID
checkFunction: () => {
/**
* Check if the department has a daily report for today
*/
SessionUser.request(SessionUser.objects.department_daily_reports.meta.endpoint + "/get", "GET", {
date: new Date().toISOString().split("T")[0],
id: department_id, // This refers to the department ID
})
.then((response) => {
emphasis.value.dailyReport.emphasis = !response.data.data.water_usage;
})
.catch((error) => {
console.error(error);
});
},
},
});
const checkEmphasis = (forcefully = false) => {
// Check if any emphasis checks are needed
for (const key in emphasis.value) {
// If the emphasis check is forced, run it regardless of the last checked time
if (forcefully) {
emphasis.value[key].lastChecked = null; // Reset last checked time
emphasis.value[key].emphasis = false; // Reset emphasis
}
// Check if the emphasis check function exists
if (!emphasis.value[key] || !emphasis.value[key].checkFunction) {
console.error(`Emphasis check for ${key} does not exist.`);
continue;
}
// Check if the last checked time exists
if (!emphasis.value[key].lastChecked) {
// Set the last checked time to 30 seconds ago if it doesn't exist
emphasis.value[key].lastChecked = new Date(Date.now() - 30000);
}
// Check if the last-checked time is more than 30 seconds ago
const now = new Date();
const timeDifference = now - emphasis.value[key].lastChecked;
if (timeDifference > 30000 || forcefully || emphasis.value[key].lastCheckedDepartmentId !== department_id) {
// If it is, run the check function
emphasis.value[key].checkFunction();
// Update the last checked time
emphasis.value[key].lastChecked = now;
} else {
// Skipping emphasis check for ${key}, last checked less than 30 seconds ago.
}
}
};
checkEmphasis(); // Initial check for emphasis
// Call the checkEmphasis function on mounted
onMounted(() => {
checkEmphasis();
// Set an interval to check emphasis every 30 seconds
setInterval(() => {
checkEmphasis();
}, 30000);
});
const menu_items = ref([
{
name: "Overblik",
route: "/admin",
icon: "fas fa-tachometer-alt",
children: [],
permissions: ["admin"],
},
{
name: "Vælg afdeling",
options: [
{
label: "Afdeling 1",
value: 1,
icon: "fas fa-building",
children: [],
},
{
label: "Afdeling 2",
value: 2,
icon: "fas fa-building",
children: [],
},
{
label: "Afdeling 3",
value: 3,
icon: "fas fa-building",
children: [],
},
],
selected:
SessionUser.functions.getDepartmentIdFromUrl() !== null
? parseInt(SessionUser.functions.getDepartmentIdFromUrl())
: null,
onSelect: async (value) => {
selectDepartment(value);
},
},
{
name: "Kassesystem",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/pos",
permissions: ["add_order"],
icon: "fas fa-cogs",
children: [
{
name: "Opret ny",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/pos",
icon: "fas fa-plus",
children: [],
hidden: SessionUser.functions.getDepartmentIdFromUrl() === undefined,
permissions: ["add_order"],
},
{
name: "Transaktioner",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/pos/orders",
icon: "fas fa-exchange-alt",
children: [],
hidden: SessionUser.functions.getDepartmentIdFromUrl() === undefined,
permissions: ["list_orders"],
},
{
name: "Synkronisering",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/pos/sync",
icon: "fas fa-exchange-alt",
children: [],
hidden: SessionUser.functions.getDepartmentIdFromUrl() === undefined,
permissions: ["list_potential_order_matches"],
},
],
hidden: SessionUser.functions.getDepartmentIdFromUrl() === undefined,
},
{
name: "Bookinger",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/bookings",
icon: "fas fa-exchange-alt",
children: [],
hidden: isDepartmentIdUndefined,
permissions: ["list_bookings"],
// emphasis: () => { return emphasis.value.bookings.emphasis; }
},
{
name: "Tidsbooking",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/time-bookings",
icon: "fas fa-exchange-alt",
children: [
{
name: "Opret ny",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/time-bookings/new",
icon: "fas fa-exchange-alt",
children: [],
hidden: isDepartmentIdUndefined,
permissions: ["department_timebookings_create"],
},
{
name: "Bookinger",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/time-bookings",
icon: "fas fa-exchange-alt",
children: [],
hidden: isDepartmentIdUndefined,
permissions: ["department_timebookings_entries_get"],
},
{
name: "Åbningstider",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/time-bookings/opening-hours",
icon: "fas fa-exchange-alt",
children: [],
hidden: isDepartmentIdUndefined,
permissions: ["department_timebookings_opening_hours_get"],
},
{
name: "Vasketyper",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/time-bookings/types",
icon: "fas fa-exchange-alt",
children: [],
hidden: isDepartmentIdUndefined,
permissions: ["department_timebookings_types_get"],
},
],
hidden: isDepartmentIdUndefined,
permissions: ["department_timebookings_entries_get"],
},
{
name: "Dagsopgørelse",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/daily-report",
icon: "fas fa-exchange-alt",
children: [],
hidden: SessionUser.functions.getDepartmentIdFromUrl() === undefined,
permissions: ["list_department_daily_reports"],
emphasis: () => {
return emphasis.value.dailyReport.emphasis;
},
},
{
name: "Mål",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/goals",
icon: "fas fa-bullseye",
children: [],
hidden: SessionUser.functions.getDepartmentIdFromUrl() === undefined,
permissions: ["list_department_goals"],
},
{
name: "Notifikationer",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/notifications",
icon: "fas fa-bell",
children: [],
hidden: SessionUser.functions.getDepartmentIdFromUrl() === undefined,
permissions: ["department_notification_sms_get"],
},
{
name: "Intranet ( udvikles )",
route: "/admin/" + SessionUser.functions.getDepartmentIdFromUrl() + "/modules/daily-report",
icon: "fas fa-exchange-alt",
children: [],
hidden: SessionUser.functions.getDepartmentIdFromUrl() === undefined,
permissions: ["list_department_intranet_reports"],
},
]);
const selectDepartment = (department) => {
// Get the current path (After /admin/:department_id)
const currentPath = window.location.pathname.split("/").slice(3).join("/");
console.log("Selected department: " + department);
menu_items.value[1].selected = department;
// Hide the menu
menu_items.value[1].isExpanded = false;
// Redirect to the department
// Check emphasis for the new department
checkEmphasis(true); // Forcefully check emphasis for the new department
SessionUser.functions.redirectTo.department(department, currentPath);
};
</script>
<template>
<MenuDefault v-if="isLoaded" v-model:menu_items="menu_items" :show_icons="false" />
</template>
<style scoped></style>