Use enum to identify routes.

This commit is contained in:
baldo 2022-08-23 15:07:37 +02:00
parent 68f893aa91
commit 4db53af7ce
5 changed files with 47 additions and 34 deletions

View file

@ -1,29 +1,25 @@
<script setup lang="ts"> <script setup lang="ts">
import {computed, defineProps} from "vue"; import {computed, defineProps} from "vue";
import type {ComponentVariant, NodesFilter} from "@/types"; import type {ComponentVariant, NodesFilter} from "@/types";
import type {RouteName} from "@/router";
import router, {route} from "@/router";
interface Props { interface Props {
title: string; title: string;
icon: string; icon: string;
variant: ComponentVariant; variant: ComponentVariant;
value: number; value: number;
link: string; route: RouteName;
filter?: NodesFilter; filter?: NodesFilter;
} }
const props = defineProps<Props>(); const props = defineProps<Props>();
const linkTarget = computed(() => { const linkTarget = computed(() => {
if (props.filter) { const query = props.filter && {
return {
path: props.link,
query: {
filter: JSON.stringify(props.filter), filter: JSON.stringify(props.filter),
}, };
} return route(props.route, query);
} else {
return props.link;
}
}); });
</script> </script>

View file

@ -1,5 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { useConfigStore } from "@/stores/config"; import { useConfigStore } from "@/stores/config";
import {route, RouteName} from "@/router";
const config = useConfigStore(); const config = useConfigStore();
</script> </script>
@ -7,7 +8,7 @@ const config = useConfigStore();
<template> <template>
<header v-if="config.getConfig"> <header v-if="config.getConfig">
<nav> <nav>
<RouterLink class="logo" to="/"> <RouterLink class="logo" :to="route(RouteName.HOME)">
<img <img
src="/icon.svg" src="/icon.svg"
alt="Symbol: Gelbes Zahnrad in zwei konzentrischen Kreisen (hellgrau und magenta)" alt="Symbol: Gelbes Zahnrad in zwei konzentrischen Kreisen (hellgrau und magenta)"
@ -16,12 +17,12 @@ const config = useConfigStore();
</RouterLink> </RouterLink>
<h1> <h1>
<RouterLink to="/">
{{ config.getConfig.community.name }} Knotenverwaltung {{ config.getConfig.community.name }} Knotenverwaltung
<RouterLink :to="route(RouteName.HOME)">
</RouterLink> </RouterLink>
</h1> </h1>
<RouterLink class="admin-link" to="/admin"> <RouterLink class="admin-link" :to="route(RouteName.ADMIN)">
<i class="fa fa-wrench" aria-hidden="true"/> Admin-Panel <i class="fa fa-wrench" aria-hidden="true"/> Admin-Panel
</RouterLink> </RouterLink>
</nav> </nav>

View file

@ -1,25 +1,38 @@
import {createRouter, createWebHistory} from "vue-router"; import {createRouter, createWebHistory, type LocationQueryRaw, type RouteLocationRaw} from "vue-router";
import AdminDashboardView from "@/views/AdminDashboardView.vue"; import AdminDashboardView from "@/views/AdminDashboardView.vue";
import AdminNodesView from "@/views/AdminNodesView.vue"; import AdminNodesView from "@/views/AdminNodesView.vue";
import HomeView from "@/views/HomeView.vue"; import HomeView from "@/views/HomeView.vue";
import {isNodesFilter, isNodeSortField, isSortDirection, type SearchTerm} from "@/types"; import {isNodesFilter, isNodeSortField, isSortDirection, type SearchTerm} from "@/types";
export enum RouteName {
HOME = "home",
ADMIN = "admin",
ADMIN_NODES = "admin-nodes",
}
export function route(name: RouteName, query?: LocationQueryRaw): RouteLocationRaw {
return {
name,
query,
};
}
const router = createRouter({ const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
routes: [ routes: [
{ {
path: "/", path: "/",
name: "home", name: RouteName.HOME,
component: HomeView, component: HomeView,
}, },
{ {
path: "/admin", path: "/admin",
name: "admin", name: RouteName.ADMIN,
component: AdminDashboardView, component: AdminDashboardView,
}, },
{ {
path: "/admin/nodes", path: "/admin/nodes",
name: "admin-nodes", name: RouteName.ADMIN_NODES,
component: AdminNodesView, component: AdminNodesView,
props: route => { props: route => {
let filter: any; let filter: any;

View file

@ -2,6 +2,7 @@
import StatisticsCard from "@/components/admin/StatisticsCard.vue"; import StatisticsCard from "@/components/admin/StatisticsCard.vue";
import {useStatisticsStore} from "@/stores/statistics"; import {useStatisticsStore} from "@/stores/statistics";
import {ComponentVariant, MonitoringState} from "@/types"; import {ComponentVariant, MonitoringState} from "@/types";
import {RouteName} from "@/router";
import PageContainer from "@/components/page/PageContainer.vue"; import PageContainer from "@/components/page/PageContainer.vue";
const statistics = useStatisticsStore(); const statistics = useStatisticsStore();
@ -23,14 +24,14 @@ refresh();
icon="circle-o" icon="circle-o"
:variant="ComponentVariant.INFO" :variant="ComponentVariant.INFO"
:value="statistics.getStatistics.nodes.registered" :value="statistics.getStatistics.nodes.registered"
link="/admin/nodes" :route="RouteName.ADMIN_NODES"
/> />
<StatisticsCard <StatisticsCard
title="Mit hinterlegtem fastd-Key" title="Mit hinterlegtem fastd-Key"
icon="lock" icon="lock"
:variant="ComponentVariant.WARNING" :variant="ComponentVariant.WARNING"
:value="statistics.getStatistics.nodes.withVPN" :value="statistics.getStatistics.nodes.withVPN"
link="/admin/nodes" :route="RouteName.ADMIN_NODES"
:filter="{hasKey: true}" :filter="{hasKey: true}"
/> />
<StatisticsCard <StatisticsCard
@ -38,7 +39,7 @@ refresh();
icon="map-marker" icon="map-marker"
:variant="ComponentVariant.SUCCESS" :variant="ComponentVariant.SUCCESS"
:value="statistics.getStatistics.nodes.withCoords" :value="statistics.getStatistics.nodes.withCoords"
link="/admin/nodes" :route="RouteName.ADMIN_NODES"
:filter="{hasCoords: true}" :filter="{hasCoords: true}"
/> />
<StatisticsCard <StatisticsCard
@ -46,7 +47,7 @@ refresh();
icon="heartbeat" icon="heartbeat"
:variant="ComponentVariant.SUCCESS" :variant="ComponentVariant.SUCCESS"
:value="statistics.getStatistics.nodes.monitoring.active" :value="statistics.getStatistics.nodes.monitoring.active"
link="/admin/nodes" :route="RouteName.ADMIN_NODES"
:filter="{monitoringState: MonitoringState.ACTIVE}" :filter="{monitoringState: MonitoringState.ACTIVE}"
/> />
<StatisticsCard <StatisticsCard
@ -54,7 +55,7 @@ refresh();
icon="envelope" icon="envelope"
:variant="ComponentVariant.DANGER" :variant="ComponentVariant.DANGER"
:value="statistics.getStatistics.nodes.monitoring.pending" :value="statistics.getStatistics.nodes.monitoring.pending"
link="/admin/nodes" :route="RouteName.ADMIN_NODES"
:filter="{monitoringState: MonitoringState.PENDING}" :filter="{monitoringState: MonitoringState.PENDING}"
/> />
</div> </div>

View file

@ -8,7 +8,7 @@ import ActionButton from "@/components/form/ActionButton.vue";
import LoadingContainer from "@/components/LoadingContainer.vue"; import LoadingContainer from "@/components/LoadingContainer.vue";
import NodesFilterPanel from "@/components/nodes/NodesFilterPanel.vue"; import NodesFilterPanel from "@/components/nodes/NodesFilterPanel.vue";
import {SortTH} from "@/components/table/SortTH.vue"; import {SortTH} from "@/components/table/SortTH.vue";
import router from "@/router"; import router, {route, RouteName} from "@/router";
const NODE_PER_PAGE = 50; const NODE_PER_PAGE = 50;
@ -92,15 +92,17 @@ async function updateRouterState(
sortField: NodeSortField, sortField: NodeSortField,
): Promise<void> { ): Promise<void> {
const filterStr = Object.keys(filter).length > 0 ? JSON.stringify(filter) : undefined; const filterStr = Object.keys(filter).length > 0 ? JSON.stringify(filter) : undefined;
await router.replace({ await router.replace(
path: '/admin/nodes', route(
query: { RouteName.ADMIN_NODES,
{
q: searchTerm || undefined, q: searchTerm || undefined,
filter: filterStr, filter: filterStr,
sortDir: sortDirection, sortDir: sortDirection,
sortField: sortField, sortField: sortField,
} }
}); )
);
} }
async function updateFilter(filter: NodesFilter, searchTerm: SearchTerm): Promise<void> { async function updateFilter(filter: NodesFilter, searchTerm: SearchTerm): Promise<void> {