Skip to content

Commit

Permalink
Extract UserMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Sep 13, 2023
1 parent 7d94a9a commit e85aec2
Show file tree
Hide file tree
Showing 25 changed files with 304 additions and 190 deletions.
25 changes: 25 additions & 0 deletions app/Domain/User/MenuItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Coyote\Domain\User;

class MenuItem
{
/** @var (string|int)[] */
public array $route;
public ?string $htmlIcon = null;

public function __construct(
public string $title,
public string $routeName,
array $routeArguments = [],
public ?string $htmlId = null,
public ?string $htmlClass = null,
string $htmlIcon = null
)
{
if ($htmlIcon) {
$fixedWidth = 'fa-fw';
$this->htmlIcon = "fa $fixedWidth $htmlIcon";
}
$this->route = [$routeName, ...$routeArguments];
}
}
12 changes: 12 additions & 0 deletions app/Domain/User/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Coyote\Domain\User;

class User
{
public function __construct(
public bool $loggedIn,
public ?int $id
)
{
}
}
119 changes: 119 additions & 0 deletions app/Domain/User/UserMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
namespace Coyote\Domain\User;

class UserMenu
{
/**
* @return MenuItem[]
*/
public function profileNavigation(User $user): array
{
if ($user->loggedIn) {
return [
new MenuItem('Moje konto', 'user.home'),
new MenuItem('Ustawienia', 'user.settings'),
new MenuItem('Profil', 'profile', routeArguments: [$user->id]),
];
}
return [];
}

/**
* @return MenuItem[]
*/
public function favouriteTabs(): array
{
return [
new MenuItem('Wątki na forum', 'user.favorites.forum'),
new MenuItem('Oferty pracy', 'user.favorites.job'),
new MenuItem('Mikroblogi', 'user.favorites.microblog'),
];
}

/**
* @return MenuItem[]
*/
public function settingsMenu(): array
{
return [
new MenuItem('Podstawowa konfiguracja',
'user.settings',
htmlId: 'btn-start',
htmlIcon: 'fa-cog'),
new MenuItem('Umiejętności',
'user.skills',
htmlId: 'btn-visits',
htmlIcon: 'fa-wrench'),
new MenuItem('Bezpieczeństwo',
'user.security',
htmlId: 'btn-notifies',
htmlIcon: 'fa-lock'),
new MenuItem('Zmiana hasła',
'user.password',
htmlId: 'btn-pm',
htmlIcon: 'fa-key'),
new MenuItem('Ustawienia powiadomień',
'user.notifications.settings',
htmlId: 'btn-favorites',
htmlIcon: 'fa-bell'),
new MenuItem('Zablokowani oraz obserwowani',
'user.relations',
htmlId: 'btn-favorites',
htmlIcon: 'fa-user-slash'),
new MenuItem('Tokeny API',
'user.tokens',
htmlId: 'btn-favorites',
htmlIcon: 'fa-key')
];
}

/**
* @return MenuItem[]
*/
public function accountMenu(): array
{
return [
new MenuItem(
'Start',
'user.home',
htmlId: 'btn-start',
htmlIcon: 'fa-map-marker'
),
new MenuItem(
'Powiadomienia',
'user.notifications',
htmlId: 'btn-notifies',
htmlIcon: 'fa-bell'),
new MenuItem(
'Wiadomości prywatne',
'user.pm',
htmlId: 'btn-pm',
htmlIcon: 'fa-envelope'),
new MenuItem(
'Ulubione i obserwowane strony',
'user.favorites',
htmlId: 'btn-favorites',
htmlIcon: 'fa-heart'),
new MenuItem(
'Oceny moich postów',
'user.rates',
htmlId: 'btn-rates',
htmlIcon: 'fa-star-half'),
new MenuItem(
'Statystyki moich postów',
'user.stats',
htmlId: 'btn-stats',
htmlIcon: 'fa-chart-bar'),
new MenuItem(
'Zaakceptowane odpowiedzi',
'user.accepts',
htmlId: 'btn-accepts',
htmlIcon: 'fa-check'),
new MenuItem(
'Usuń konto',
'user.delete',
htmlClass: 'text-danger',
htmlIcon: 'fa-trash-alt')
];
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/Profile/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Coyote\Http\Controllers\Profile;

use Coyote\Http\Controllers\Controller;
use Coyote\Http\Controllers\User\UserMenuTrait;
use Coyote\Http\Controllers\User\Menu\ProfileNavigation;
use Coyote\Http\Requests\SkillsRequest;
use Coyote\Http\Resources\MicroblogCollection;
use Coyote\Http\Resources\TagResource;
Expand All @@ -19,7 +19,7 @@

class HomeController extends Controller
{
use UserMenuTrait;
use ProfileNavigation;

/**
* @var UserRepository
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/User/AcceptsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Coyote\Http\Controllers\User;

use Boduch\Grid\Source\EloquentSource;
use Coyote\Http\Controllers\User\Menu\AccountMenu;
use Coyote\Http\Grids\User\AcceptsGrid;
use Coyote\Repositories\Contracts\PostRepositoryInterface;
use Illuminate\View\View;

class AcceptsController extends BaseController
{
use HomeTrait;
use AccountMenu;

public function index(PostRepositoryInterface $post): View
{
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/User/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Coyote\Http\Controllers\User;

use Coyote\Http\Controllers\Controller;
use Coyote\Http\Controllers\User\Menu\ProfileNavigation;
use Illuminate\Contracts\View\Factory;
use Illuminate\View\View;

abstract class BaseController extends Controller
{
use UserMenuTrait;
use ProfileNavigation;

public function __construct()
{
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/User/DeleteAccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Coyote\Http\Controllers\User;

use Coyote\Events\UserDeleted;
use Coyote\Http\Controllers\User\Menu\AccountMenu;
use Coyote\Rules\PasswordCheck;
use Coyote\Services\Stream\Activities\Delete;
use Coyote\Services\Stream\Objects\Person;
Expand All @@ -19,7 +20,7 @@ class DeleteAccountController extends BaseController
logout as traitLogout;
}

use HomeTrait;
use AccountMenu;

public function index(): View
{
Expand Down
19 changes: 9 additions & 10 deletions app/Http/Controllers/User/FavoritesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Coyote\Http\Controllers\User;

use Coyote\Domain\User\UserMenu;
use Coyote\Http\Controllers\User\Menu\AccountMenu;
use Coyote\Job;
use Coyote\Microblog;
use Coyote\Models\Subscription;
Expand All @@ -13,7 +15,7 @@

class FavoritesController extends BaseController
{
use HomeTrait;
use AccountMenu;

public function __construct()
{
Expand Down Expand Up @@ -66,19 +68,16 @@ protected function load(string $resource): View

protected function getTabs(): Builder
{
$menuItems = (new UserMenu())->favouriteTabs();

/** @var Menu $menu */
$menu = app(Menu::class);
return $menu->make('favorites', function (Builder $menu) {
$tabs = [
'user.favorites.forum' => 'Wątki na forum',
'user.favorites.job' => 'Oferty pracy',
'user.favorites.microblog' => 'Mikroblogi'
];
foreach ($tabs as $route => $label) {
$item = $menu->add("<span>$label</span>", ['route' => $route]);
return $menu->make('favorites', function (Builder $menu) use ($menuItems) {
foreach ($menuItems as $menuItem) {
$item = $menu->add("<span>{$menuItem->title}</span>", ['route' => $menuItem->route]);
$item->link->attr(['class' => 'nav-item']);

if ($route === request()->route()->getName()) {
if ($menuItem->routeName === request()->route()->getName()) {
$item->link->active();
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/User/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Coyote\Http\Controllers\User;

use Coyote\Events\UserSaved;
use Coyote\Http\Controllers\User\Menu\AccountMenu;
use Coyote\Http\Factories\MediaFactory;
use Coyote\Repositories\Contracts\UserRepositoryInterface;
use Illuminate\Http\JsonResponse;
Expand All @@ -11,7 +12,7 @@

class HomeController extends BaseController
{
use HomeTrait, MediaFactory;
use AccountMenu, MediaFactory;

public function index(UserRepositoryInterface $user): View
{
Expand Down
71 changes: 0 additions & 71 deletions app/Http/Controllers/User/HomeTrait.php

This file was deleted.

35 changes: 35 additions & 0 deletions app/Http/Controllers/User/Menu/AccountMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Coyote\Http\Controllers\User\Menu;

use Coyote\Domain\User\UserMenu;
use Lavary\Menu\Builder;
use Lavary\Menu\Menu;

trait AccountMenu
{
public function getSideMenu(): Builder
{
$menuItems = (new UserMenu())->accountMenu();

/** @var Menu $menu */
$menu = app(Menu::class);

return $menu->make('user.home', function (Builder $menu) use ($menuItems) {
foreach ($menuItems as $menuItem) {
$menu
->add($menuItem->title, [
'id' => $menuItem->htmlId ?? '',
'class' => $menuItem->htmlClass ?? '',
'route' => $menuItem->route
])
->prepend("<i class=\"$menuItem->htmlIcon\"></i>");
}

$user = auth()->user();
$menu
->find('btn-pm')
->append(' <small>(' . $user->pm_unread . '/' . $user->pm . ')</small>');
});
}
}
Loading

0 comments on commit e85aec2

Please sign in to comment.