From e004bbfca1cc3bc73a2a0edb9054f1214dbbcaef Mon Sep 17 00:00:00 2001
From: philipp lang <philipp@aweos.de>
Date: Fri, 19 Nov 2021 00:39:34 +0100
Subject: [PATCH] Hide Fee button when no bill mode enabled

---
 app/Http/Middleware/InertiaShareMiddleware.php           | 7 ++++++-
 app/Setting/Controllers/SettingController.php            | 4 ++--
 app/Setting/GeneralSettings.php                          | 6 ++++--
 .../2021_11_18_230152_create_general_settings.php        | 9 ++++++---
 resources/js/app.js                                      | 3 +++
 resources/js/layouts/App.vue                             | 2 +-
 resources/js/mixins/hasModule.js                         | 7 +++++++
 tests/Feature/Settings/GlobalSettingTest.php             | 6 +++---
 8 files changed, 32 insertions(+), 12 deletions(-)
 create mode 100644 resources/js/mixins/hasModule.js

diff --git a/app/Http/Middleware/InertiaShareMiddleware.php b/app/Http/Middleware/InertiaShareMiddleware.php
index 4150c034..05eefee5 100644
--- a/app/Http/Middleware/InertiaShareMiddleware.php
+++ b/app/Http/Middleware/InertiaShareMiddleware.php
@@ -3,6 +3,7 @@
 namespace App\Http\Middleware;
 
 use App\Http\Resources\UserResource;
+use App\Setting\GeneralSettings;
 use Closure;
 use Session;
 
@@ -14,6 +15,7 @@ class InertiaShareMiddleware
      *
      * @param  \Illuminate\Http\Request  $request
      * @param  \Closure  $next
+     * @param GeneralSettings $settings
      * @return mixed
      */
     public function handle($request, Closure $next)
@@ -32,7 +34,10 @@ class InertiaShareMiddleware
             },
             'title' => function() {
                 return session()->get('title', '');
-            }
+            },
+            'settings' => [
+                'modules' => app(GeneralSettings::class)->modules,
+            ]
         ]);
 
         $response = $next($request);
diff --git a/app/Setting/Controllers/SettingController.php b/app/Setting/Controllers/SettingController.php
index ffa33bfb..928d07c2 100644
--- a/app/Setting/Controllers/SettingController.php
+++ b/app/Setting/Controllers/SettingController.php
@@ -18,10 +18,10 @@ class SettingController extends Controller
     {
         return Inertia::render('setting/Index', [
             'options' => [
-                'modes' => $generalSettings->modeOptions()
+                'modules' => $generalSettings->moduleOptions()
             ],
             'general' => [
-                'modes' => $generalSettings->modes
+                'modules' => $generalSettings->modules
             ]
         ]);
     }
diff --git a/app/Setting/GeneralSettings.php b/app/Setting/GeneralSettings.php
index d0869346..a53c68c2 100644
--- a/app/Setting/GeneralSettings.php
+++ b/app/Setting/GeneralSettings.php
@@ -8,12 +8,14 @@ class GeneralSettings extends Settings
 {
 
     /** @var array<int, string> */
-    public array $modes;
+    public array $modules;
+
+    public bool $single_view;
 
     /**
      * @return array<int, string>
      */
-    public function modeOptions(): array
+    public function moduleOptions(): array
     {
         return [
             'bill',
diff --git a/database/settings/2021_11_18_230152_create_general_settings.php b/database/settings/2021_11_18_230152_create_general_settings.php
index da38a2f4..06258cbc 100644
--- a/database/settings/2021_11_18_230152_create_general_settings.php
+++ b/database/settings/2021_11_18_230152_create_general_settings.php
@@ -13,10 +13,12 @@ class CreateGeneralSettings extends SettingsMigration
     {
         $defaults = [
             'diözese' => [
-                'modes' => []
+                'modules' => [],
+                'single_view' => false,
             ],
             'stamm' => [
-                'modes' => ['bill']
+                'modules' => ['bill'],
+                'single_view' => true,
             ]
         ];
 
@@ -26,6 +28,7 @@ class CreateGeneralSettings extends SettingsMigration
     public function up(): void
     {
         $defaults = $this->defaults(config('app.mode'));
-        $this->migrator->add('general.modes', $defaults['modes']);
+        $this->migrator->add('general.modules', $defaults['modules']);
+        $this->migrator->add('general.single_view', $defaults['single_view']);
     }
 }
diff --git a/resources/js/app.js b/resources/js/app.js
index c178a1bd..6e1bec96 100644
--- a/resources/js/app.js
+++ b/resources/js/app.js
@@ -12,6 +12,7 @@ import Pages from './components/Pages.vue';
 import VBool from './components/VBool.vue';
 import App from './layouts/App.vue';
 import VTooltip from 'v-tooltip'
+import hasModule from './mixins/hasModule.js';
 
 Vue.use(plugin)
 Vue.use(VTooltip);
@@ -25,6 +26,8 @@ Vue.component('v-bool', VBool);
 
 const el = document.getElementById('app')
 
+Vue.mixin(hasModule);
+
 new Vue({
     render: h => h(InertiaApp, {
         props: {
diff --git a/resources/js/layouts/App.vue b/resources/js/layouts/App.vue
index 324d1cb7..560f7ed9 100644
--- a/resources/js/layouts/App.vue
+++ b/resources/js/layouts/App.vue
@@ -8,7 +8,7 @@
             <div class="grid gap-2">
                 <v-link href="/" menu="dashboard" icon="loss">Dashboard</v-link>
                 <v-link href="/member" menu="member" icon="user">Mitglieder</v-link>
-                <v-link href="/subscription" menu="subscription" icon="money">Beiträge</v-link>
+                <v-link href="/subscription" v-show="hasModule('bill')" menu="subscription" icon="money">Beiträge</v-link>
             </div>
         </div>
 
diff --git a/resources/js/mixins/hasModule.js b/resources/js/mixins/hasModule.js
new file mode 100644
index 00000000..c4b94520
--- /dev/null
+++ b/resources/js/mixins/hasModule.js
@@ -0,0 +1,7 @@
+export default {
+    methods: {
+        hasModule(module) {
+            return this.$page.props.settings.modules.indexOf(module) !== -1;
+        }
+    }
+};
diff --git a/tests/Feature/Settings/GlobalSettingTest.php b/tests/Feature/Settings/GlobalSettingTest.php
index 2bc0898d..67bdec36 100644
--- a/tests/Feature/Settings/GlobalSettingTest.php
+++ b/tests/Feature/Settings/GlobalSettingTest.php
@@ -15,7 +15,7 @@ class GlobalSettingTest extends TestCase
     public function testItLoadsGeneralSettings(): void
     {
         GeneralSettings::fake([
-            'modes' => ['bill']
+            'modules' => ['bill']
         ]);
         $this->withoutExceptionHandling();
         $this->login()->init();
@@ -23,7 +23,7 @@ class GlobalSettingTest extends TestCase
         $response = $this->get('/setting');
                          
         $response->assertInertiaComponent('setting/Index');
-        $this->assertEquals(['bill'], $response->inertia('general.modes'));
+        $this->assertEquals(['bill'], $response->inertia('general.modules'));
     }
 
     public function testItGetsOptionsForModels(): void
@@ -33,7 +33,7 @@ class GlobalSettingTest extends TestCase
 
         $response = $this->get('/setting');
                          
-        $this->assertContains('bill', $response->inertia('options.modes'));
+        $this->assertContains('bill', $response->inertia('options.modules'));
     }
 
 }