Skip to content

Commit

Permalink
feat: change layout for public lists (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Feb 20, 2024
1 parent b52cd93 commit 08550b7
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/PublicListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Controllers;

use App\Http\ViewModels\User\ListViewModel;
use App\Http\ViewModels\User\PublicListViewModel;
use App\Http\ViewModels\User\UserViewModel;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
Expand All @@ -14,7 +14,7 @@ public function show(Request $request): View
{
$requestedList = $request->attributes->get('list');

$details = Cache::remember('list-details-' . $requestedList->id, 604800, fn () => ListViewModel::show($requestedList));
$details = PublicListViewModel::show($requestedList);

$favoritedNamesForLoggedUser = auth()->check()
? Cache::remember('user-favorites-' . auth()->id(), 604800, fn () => UserViewModel::favorites())
Expand Down
46 changes: 46 additions & 0 deletions app/Http/ViewModels/User/PublicListViewModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\ViewModels\User;

use App\Helpers\StringHelper;
use App\Models\Name;
use App\Models\NameList;
use Illuminate\Support\Number;

class PublicListViewModel
{
public static function show(NameList $list): array
{
$names = $list->names()
->withPivot('public_note')
->orderBy('name')->get()
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::formatNameFromDB($name->name),
'total' => Number::format($name->total),
'public_note' => $name->pivot->public_note,
'url' => [
'show' => route('name.show', [
'id' => $name->id,
'name' => StringHelper::sanitizeNameForURL($name->name),
]),
'favorite' => route('favorite.update', [
'id' => $name->id,
]),
],
]);

return [
'id' => $list->id,
'name' => $list->name,
'description' => $list->description,
'names' => $names,
'created_at' => $list->created_at->isoFormat('LL'),
'url' => [
'show' => route('list.show', [
'liste' => $list->id,
]),
],
];
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ parameters:
- app/Http/ViewModels/Names/NameViewModel.php
- app/Http/ViewModels/Search/SearchViewModel.php
- app/Http/ViewModels/User/ListViewModel.php
- app/Http/ViewModels/User/PublicListViewModel.php
#
# checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
Expand Down
47 changes: 38 additions & 9 deletions resources/views/user/lists/public/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,60 @@
</div>

<div>
<div class="mx-auto max-w-4xl sm:px-6 px-2 lg:px-8 py-2">
<div class="mx-auto max-w-6xl sm:px-6 px-2 lg:px-8 py-2">

<h1 class="text-3xl mb-12 text-center">{{ $list['name'] }}</h1>

<div class="grid list-show-grid sm:gap-5">

<!-- left -->
<div>
<p class="text-xs text-gray-700 mb-3">Liste créée le {{ $list['created_at'] }}.</p>

<div>
<div class="mb-3 text-gray-600">
{{ $list['description'] }}
</div>

<p class="text-xs text-gray-700">Liste créée le {{ $list['created_at'] }}.</p>
</div>

<!-- right -->
<div>
<div class="mb-10">
<ul class="space-y-1">
<div class="space-y-1">
@foreach ($list['names'] as $name)
<li>
<x-name-items :name="$name" favorited="{{ $favorites->contains($name['id']) }}" showOrigins="true" />
</li>
<div class="border hover:bg-blue-50 hover:border-blue-400 px-3 py-2 mb-2 rounded-md">
<div class="flex items-center justify-between">
<!-- avatar + name / public note -->
<div class="relative">
<!-- avatar -->
<div class="absolute top-1 rounded-full w-5 mr-4 ring-4 ring-violet-100">{!! \App\Helpers\NameHelper::getAvatar($name['name']) !!}</div>

<div class="ml-8">
<!-- name -->
<a href="{{ $name['url']['show'] }}" class="font-semibold mb-2 text-lg hover:underline">
{{ $name['name']}}
</a>

<!-- public note -->
<div class="mb-1">
{{ $name['public_note'] }}
</div>

<!-- stats -->
<p class="text-xs text-gray-700 flex">
<x-heroicon-c-arrow-trending-up class="w-4 h-4 mr-1 text-gray-500" />
{{ $name['total'] }} utilisations depuis 1900</p>
</div>
</div>

<!-- favorite -->
<div>
{{-- <x-heroicon-o-heart
class="w-4 h-4 ml-2 text-gray-400 hover:text-rose-400 hover:w-5 hover:h-5 transition-all cursor-pointer" /> --}}
</div>
</div>
</div>
@endforeach
</ul>
</div>
</div>
</div>
</div>
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/ViewModels/User/PublicListViewModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Unit\ViewModels\User;

use App\Http\ViewModels\User\PublicListViewModel;
use App\Models\Name;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class PublicListViewModelTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_gets_the_list_of_names_in_the_list(): void
{
$name = Name::factory()->create([
'name' => 'test',
'total' => 1000,
'origins' => 'test',
]);
$user = User::factory()->create();
$nameList = $user->lists()->create([
'is_list_of_favorites' => true,
'uuid' => '1234567890',
]);
$nameList->names()->attach($name->id);

$this->be($user);

$array = PublicListViewModel::show($nameList);

$this->assertCount(6, $array);
$this->assertArrayHasKey('id', $array);
$this->assertArrayHasKey('name', $array);
$this->assertArrayHasKey('description', $array);
$this->assertArrayHasKey('names', $array);
$this->assertArrayHasKey('url', $array);
$this->assertEquals(
[
0 => [
'id' => $name->id,
'name' => 'Test',
'total' => '1 000',
'public_note' => null,
'url' => [
'show' => env('APP_URL') . '/prenoms/' . $name->id . '/test',
'favorite' => env('APP_URL') . '/prenoms/' . $name->id . '/favorite',
],
],
],
$array['names']->toArray()
);
}
}

0 comments on commit 08550b7

Please sign in to comment.