API-Route to new global variables

This commit is contained in:
2026-02-05 09:18:24 +01:00
parent e9ae850002
commit 6fc65e195c
8 changed files with 108 additions and 65 deletions

View File

@@ -2,18 +2,10 @@
import AppLayout from '../../../../resources/js/layouts/AppLayout.vue'
import ShadowedBox from "../../../Views/Components/ShadowedBox.vue";
const props = defineProps({
navbar: Object,
tenant: String,
user: Object,
currentPath: String,
})
console.log(props.user.nicename)
</script>
<template>
<AppLayout title='Dashboard' :user="props.user" :navbar="props.navbar" :tenant="props.tenant" :currentPath="props.currentPath">
<AppLayout title='Dashboard'>
<diV class="dashboard-widget-container">
<shadowed-box class="dashboard-widget-box" style="width: 60%;">
Meine Anmeldungen

View File

@@ -1,15 +1,13 @@
<?php
use App\Domains\Dashboard\Controllers\DashboardController;
use App\Domains\UserManagement\Controllers\EmailVerificationController;
use App\Domains\UserManagement\Controllers\LoginController;
use App\Domains\UserManagement\Controllers\LogOutController;
use App\Domains\UserManagement\Controllers\RegistrationController;
use App\Domains\UserManagement\Controllers\ResetPasswordController;
use App\Http\Controllers\TestRenderInertiaProvider;
use App\Middleware\IdentifyTenant;
use App\Providers\GlobalDataProvider;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
Route::middleware(IdentifyTenant::class)->group(function () {
Route::get('/register', [RegistrationController::class, 'loginForm']);
@@ -25,6 +23,11 @@ Route::middleware(IdentifyTenant::class)->group(function () {
Route::post('/logout', [LogoutController::class, 'logout']);
});
Route::prefix('api/v1/') ->group(function () {
Route::get('/retreive-global-data', GlobalDataProvider::class);
});
});

View File

@@ -38,6 +38,7 @@ class DevelopmentDataSeeder {
'email' => 'th.guenther@saale-mail.de',
'password' => bcrypt('development'),
'username' => 'development',
'active' => true,
]);
}
}

View File

@@ -101,6 +101,6 @@ class User extends Authenticatable
}
public function getNicename() : string {
return $this->nickname !== '' ? $this->nickname : $this->firstname;
return $this->nickname ?? $this->firstname;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Providers;
use App\Models\Tenant;
use App\Models\User;
use App\Repositories\UserRepository;
use App\Resources\UserResource;
class GlobalDataProvider {
private ?User $user;
public function __invoke() {
$this->user = auth()->user();
return response()->json([
'user' => null !== $this->user ? new UserResource($this->user)->toArray(request()) : [],
'navbar' => $this->generateNavbar(),
'tenant' => app('tenant'),
'availableLocalGroups' => Tenant::where(['is_active_local_group' => true])->get(),
]);
}
private function generateNavbar() : array {
$navigation = [
'personal' => [],
'common' => [],
'costunits' => [],
'events' => [],
];
$navigation['personal'][] = ['url' => '/', 'display' => 'Home'];
if (null !== $this->user) {
$navigation['personal'][] = ['url' => '/personal-data', 'display' => 'Meine Daten'];
$navigation['personal'][] = ['url' => '/messages', 'display' => 'Meine Nachrichten'];
$navigation['costunits'][] = ['url' => '/cost-unit/create', 'display' => 'Neue laufende Tätigkeit'];
}
$navigation['common'][] = ['url' => '/capture-invoice', 'display' => 'Neue Abrechnung'];
$navigation['common'][] = ['url' => '/available-events', 'display' => 'Verfügbare Veranstaltungen'];
return $navigation;
}
}

View File

@@ -22,35 +22,11 @@ final class InertiaProvider
}
public function render() : Response {
$this->props['navbar'] = $this->generateNavbar();
$this->props['tenant'] = app('tenant');
$this->props['user'] = new UserResource($this->user)->toArray(request());
$this->props['currentPath'] = request()->path();
$this->props['availableLocalGroups'] = Tenant::where(['is_active_local_group' => true])->get();
return Inertia::render(
str_replace('/', '/Views/', $this->vueFile),
$this->props
);
}
private function generateNavbar() : array {
$navigation = [
'personal' => [],
'common' => [],
'costunits' => [],
'events' => [],
];
$navigation['personal'][] = ['url' => '/', 'display' => 'Home'];
if (null !== $this->user) {
$navigation['personal'][] = ['url' => '/personal-data', 'display' => 'Meine Daten'];
$navigation['personal'][] = ['url' => '/messages', 'display' => 'Meine Nachrichten'];
}
$navigation['common'][] = ['url' => '/capture-invoice', 'display' => 'Neue Abrechnung'];
$navigation['common'][] = ['url' => '/available-events', 'display' => 'Verfügbare Veranstaltungen'];
return $navigation;
}
}

View File

@@ -1,15 +1,44 @@
<script setup>
import {reactive, onMounted} from 'vue';
import Icon from "../../../app/Views/Components/Icon.vue";
import GlobalWidgets from "../../../app/Views/Partials/GlobalWidgets/GlobalWidgets.vue";
import {toast} from "vue3-toastify";
import {useAjax} from "../components/ajaxHandler.js";
const { request } = useAjax()
const globalProps = reactive({
navbar: {
personal: [],
common: [],
costunits: [],
events: [],
},
tenant: '',
user: null,
currentPath: '/',
errors: {},
availableLocalGroups: []
});
onMounted(async () => {
const response = await fetch('/api/v1/retreive-global-data');
const data = await response.json();
Object.assign(globalProps, data); // reactive wird automatisch aktualisiert
});
const currentPath = window.location.pathname;
const props = defineProps({
title: { type: String, default: 'App' },
user: { type: Object, },
flash: { type: Object, default: () => ({}) },
navbar: { type: Object },
tenant: String,
currentPath: String,
})
flash: { type: Object, default: () => ({}) }
});
</script>
<template>
@@ -25,36 +54,36 @@ const props = defineProps({
</div>
<nav class="nav">
<ul class="nav-links" v-if="props.navbar.personal.length > 0">
<li v-for="navlink in props.navbar.personal">
<ul class="nav-links" v-if="globalProps.navbar.personal.length > 0">
<li v-for="navlink in globalProps.navbar.personal">
<a
:class="{ navlink_active: navlink.url.endsWith(props.currentPath) }"
:class="{ navlink_active: navlink.url.endsWith(currentPath) }"
:href="navlink.url"
>{{navlink.display}}</a>
</li>
</ul>
<ul class="nav-links" v-if="props.navbar.common.length > 0">
<li v-for="navlink in props.navbar.common">
<ul class="nav-links" v-if="globalProps.navbar.common.length > 0">
<li v-for="navlink in globalProps.navbar.common">
<a
:class="{ navlink_active: navlink.url.endsWith(props.currentPath) }"
:class="{ navlink_active: navlink.url.endsWith(currentPath) }"
:href="navlink.url"
>{{navlink.display}}</a>
</li>
</ul>
<ul class="nav-links" v-if="props.navbar.costunits.length > 0">
<li v-for="navlink in props.navbar.costunits">
<ul class="nav-links" v-if="globalProps.navbar.costunits.length > 0">
<li v-for="navlink in globalProps.navbar.costunits">
<a
:class="{ navlink_active: navlink.url.endsWith(props.currentPath) }"
:class="{ navlink_active: navlink.url.endsWith(currentPath) }"
:href="navlink.url"
>{{navlink.display}}</a>
</li>
</ul>
<ul class="nav-links" v-if="props.navbar.events.length > 0">
<li v-for="navlink in props.navbar.events">
<ul class="nav-links" v-if="globalProps.navbar.events.length > 0">
<li v-for="navlink in globalProps.navbar.events">
<a
:class="{ navlink_active: navlink.url.endsWith(props.currentPath) }"
:class="{ navlink_active: navlink.url.endsWith(currentPath) }"
:href="navlink.url"
>{{navlink.display}}</a>
</li>
@@ -67,10 +96,9 @@ const props = defineProps({
<div class="header">
<div class="left-side">
<h1>{{ props.title }}</h1>
<label id="show_username" v-if="props.user !== null">Willkommen, {{ props.user.nicename }}</label>
Test
<label id="show_username" v-if="globalProps.user !== null">Willkommen, {{ globalProps.user.nicename }}</label>
</div>
<div class="user-info" v-if="props.user !== null">
<div class="user-info" v-if="globalProps.user !== null">
<a href="/messages" class="header-link-anonymous" title="Meine Nachrichten">
<Icon name="envelope" />
</a>
@@ -91,7 +119,7 @@ const props = defineProps({
</div>
<global-widgets :user="props.user" :tenant="props.tenant" v-if="props.user !== null" />
<global-widgets :user="globalProps.user" :tenant="globalProps.tenant" v-if="globalProps.user !== null" />
<div class="content">
<slot />
</div>

View File

@@ -1,17 +1,14 @@
<?php
use App\Domains\Dashboard\Controllers\DashboardController;
use App\Domains\UserManagement\Controllers\EmailVerificationController;
use App\Domains\UserManagement\Controllers\LoginController;
use App\Domains\UserManagement\Controllers\LogOutController;
use App\Domains\UserManagement\Controllers\RegistrationController;
use App\Domains\UserManagement\Controllers\ResetPasswordController;
use App\Http\Controllers\TestRenderInertiaProvider;
use App\Middleware\IdentifyTenant;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
require_once __DIR__ . '/../app/Domains/UserManagement/Routes/web.php';
require_once __DIR__ . '/../app/Domains/CostUnit/Routes/web.php';
Route::middleware(IdentifyTenant::class)->group(function () {
Route::get('/', DashboardController::class);