Replace default cards on dashboard with widgets
MODIFY
app/Http/Controllers/DashboardController.php
app/Http/Controllers/DashboardController.php
@@ -23,7 +23,7 @@ class DashboardController extends Controller
$this->tagRepository = $tagRepository;
}
- public function __invoke()
+ public function __invoke(Request $request)
{
$space_id = session('space_id');
$currentYear = date('Y');
@@ -35,9 +35,7 @@ class DashboardController extends Controller
return view('dashboard', [
'month' => date('n'),
- 'balance' => $this->dashboardRepository->getBalance($currentYear, $currentMonth),
- 'recurrings' => $this->dashboardRepository->getRecurrings($currentYear, $currentMonth),
- 'leftToSpend' => $this->dashboardRepository->getLeftToSpend($currentYear, $currentMonth),
+ 'widgets' => $request->user()->widgets()->orderBy('sorting_index')->get(),
'totalSpent' => $this->dashboardRepository->getTotalAmountSpent($currentYear, $currentMonth),
'mostExpensiveTags' => $mostExpensiveTags,

MODIFY
resources/views/dashboard.blade.php
resources/views/dashboard.blade.php
<h2>{{ __('general.dashboard') }}</h2>
<p class="mt-1">{{ __('calendar.months.' . $month) }} {{ date('Y') }}</p>
<div class="row row--gutter row--responsive my-3">
+ @foreach ($widgets as $widget)
<div class="row__column">
- <div class="card card--blue">
- <h2 style="font-size: 20px;">{!! $currency !!} {{ \App\Helper::formatNumber($balance / 100) }}</h2>
- <div class="mt-1" style="color: #A7AEBB;">{{ __('general.balance') }}</div>
- </div>
- </div>
- <div class="row__column">
- <div class="card card--red">
- <h2 style="font-size: 20px;">{!! $currency !!} {{ \App\Helper::formatNumber($recurrings / 100) }}</h2>
- <div class="mt-1" style="color: #A7AEBB;">{{ __('general.recurrings') }}</div>
- </div>
- </div>
- <div class="row__column">
- <div class="card card--green">
- <h2 style="font-size: 20px;">{!! $currency !!} {{ \App\Helper::formatNumber($leftToSpend / 100) }}</h2>
- <div class="mt-1" style="color: #A7AEBB;">{{ __('general.left_to_spend') }}</div>
- </div>
+ {!! $widget->render() !!}
</div>
+ @endforeach
</div>
@if (count($mostExpensiveTags))
<div class="box mt-3">

DELETE
tests/Feature/DashboardTest.php
-<?php
-
-namespace Tests\Feature;
-
-use App\Models\Earning;
-use App\Models\Space;
-use App\Models\Spending;
-use App\Models\User;
-use Illuminate\Foundation\Testing\RefreshDatabase;
-use Illuminate\Foundation\Testing\WithFaker;
-use Tests\TestCase;
-
-class DashboardTest extends TestCase
-{
- public function testEmptySpace(): void
- {
- $user = User::factory()->create();
- $space = Space::factory()->create();
-
- $response = $this
- ->actingAs($user)
- ->withSession(['space_id' => $space->id])
- ->get('/dashboard');
-
- $response
- ->assertStatus(200)
- ->assertSeeTextInOrder([
- '0.00',
- 'Balance',
- '0.00',
- 'Recurrings',
- '0.00',
- 'Left to Spend'
- ]);
- }
-
- public function testUsedSpace(): void
- {
- $user = User::factory()->create();
- $space = Space::factory()->create();
-
- // Earn 10 bucks
- Earning::factory()->create([
- 'space_id' => $space->id,
- 'happened_on' => date('Y-m-d'),
- 'amount' => 1000]);
-
- // Spend 5 bucks
- Spending::factory()->create([
- 'space_id' => $space->id,
- 'happened_on' => date('Y-m-d'),
- 'amount' => 500
- ]);
-
- $response = $this
- ->actingAs($user)
- ->withSession(['space_id' => $space->id])
- ->get('/dashboard');
-
- $response
- ->assertStatus(200)
- ->assertSeeTextInOrder([
- '5.00',
- 'Balance',
- '0.00',
- 'Recurrings',
- '5.00',
- 'Left to Spend'
- ]);
- }
-}

Replace default cards on dashboard with widgets