Skip to content

Commit

Permalink
Add <aside> to /Microblogs/Views/
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Dec 5, 2023
1 parent 25c4796 commit 1d1533a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 139 deletions.
105 changes: 32 additions & 73 deletions app/Http/Controllers/Microblog/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,126 +7,85 @@
use Coyote\Http\Resources\MicroblogCollection;
use Coyote\Http\Resources\MicroblogResource;
use Coyote\Http\Resources\UserResource;
use Coyote\Repositories\Contracts\MicroblogRepositoryInterface as MicroblogRepository;
use Coyote\Repositories\Eloquent\MicroblogRepository;
use Coyote\Services\Microblogs\Builder;
use Coyote\Tag;
use Illuminate\Http\Request;
use Illuminate\View\View;

class HomeController extends BaseController
{
use CacheFactory;

/**
* @var MicroblogRepository
*/
private MicroblogRepository $microblog;

/**
* @var Builder
*/
private Builder $builder;

/**
* @param MicroblogRepository $microblog
* @param Builder $builder
*/
public function __construct(MicroblogRepository $microblog)
public function __construct(private MicroblogRepository $microblog)
{
parent::__construct();

$this->microblog = $microblog;
$this->breadcrumb->push('Mikroblog', route('microblog.home'));

$this->middleware(function (Request $request, $next) {
$this->builder = resolve(Builder::class);

return $next($request);
});
}

/**
* @return \Illuminate\View\View
*/
public function index(RenderParams $renderParams = null)
public function index(RenderParams $renderParams = null): View
{
$paginator = $this->builder->orderById()->paginate();

// let's cache microblog tags. we don't need to run this query every time
$tags = $this->getCacheFactory()->remember('microblog:tags', 30 * 60, function () {
return $this->microblog->getTags();
});

[$tech, $others] = $tags->partition(function (Tag $tag) {
return $tag->category_id === Tag\Category::LANGUAGE;
});

return $this->view('microblog.home', [
'flags' => $this->flags(),
'count' => $this->microblog->count(),
'count_user' => $this->microblog->countForUser($this->userId),
'pagination' => new MicroblogCollection($paginator),
'route' => request()->route()->getName(),
'popular_tags' => $this->microblog->popularTags($this->userId),
'recommended_users' => UserResource::collection($this->microblog->recommendedUsers($this->userId)),
'tags' => [
'tech' => $tech,
'others' => $others->splice(0, 10)
],
'render_params' => $renderParams,
'flags' => $this->flags(),
'count' => $this->microblog->count(),
'count_user' => $this->microblog->countForUser($this->userId),
'pagination' => new MicroblogCollection($this->builder->orderById()->paginate()),
'route' => request()->route()->getName(),
'popular_tags' => $this->microblog->popularTags($this->userId),
'recommended_users' => UserResource::collection($this->microblog->recommendedUsers($this->userId)),
'tags' => $this->tags(),
'render_params' => $renderParams,
]);
}

/**
* @param string $tag
* @return \Illuminate\View\View
*/
public function tag(string $tag)
public function tag(string $tag): View
{
$this->breadcrumb->push('Wpisy z tagiem: ' . $tag, route('microblog.tag', [$tag]));

$this->builder->withTag($tag);

$renderParams = new RenderParams();
$renderParams->tagName = $tag;

return $this->index($renderParams);
}

/**
* @return \Illuminate\View\View
*/
public function mine()
public function mine(): View
{
$this->breadcrumb->push('Moje wpisy', route('microblog.mine'));

$this->builder->onlyUsers($this->auth);

return $this->index();
}

/**
* @param $id
* @return \Illuminate\View\View
*/
public function show($id)
public function show(int $id): View
{
$microblog = $this->builder->one($id);

abort_if(!is_null($microblog->parent_id), 404);

$excerpt = excerpt($microblog->html);

$this->breadcrumb->push($excerpt, route('microblog.view', [$microblog->id]));

MicroblogResource::withoutWrapping();

$resource = new MicroblogResource($microblog);
$resource->preserverKeys();

return $this->view('microblog.view')->with([
'flags' => $this->flags(),
'microblog' => $resource,
'excerpt' => $excerpt
'flags' => $this->flags(),
'microblog' => $resource,
'excerpt' => $excerpt,
'popular_tags' => $this->microblog->popularTags($this->userId),
'recommended_users' => UserResource::collection($this->microblog->recommendedUsers($this->userId)),
'tags' => $this->tags(),
]);
}

private function tags(): array
{
$tags = $this->getCacheFactory()->remember('microblog:tags', 30 * 60, fn() => $this->microblog->getTags());
[$tech, $others] = $tags->partition(fn(Tag $tag) => $tag->category_id === Tag\Category::LANGUAGE);
return [
'tech' => $tech,
'others' => $others->splice(0, 10),
];
}
}
62 changes: 62 additions & 0 deletions resources/views/microblog/aside.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<aside class="col-md-3">
<div class="box recommended-users">
<h4>
<i class="fas fa-tag fa-fw"></i>
Zacznij obserwować
</h4>

<div id="js-skeleton" class="card card-body mt-2" style="min-height: 300px">
</div>

<div v-cloak class="card card-body mt-2">
<div v-for="user in recommendedUsers" class="media">
<vue-avatar :id="user.id" :photo="user.photo" :name="user.name" class="d-none d-xl-block i-45 mr-2">
</vue-avatar>

<div class="media-body">
<span class="d-block mb-1">
<vue-username :user="user" class="text-body"></vue-username>
</span>
<vue-follow-button :user-id="user.id" class="btn-sm"></vue-follow-button>
</div>
</div>
</div>
</div>

<div class="box mt-2">
<h4 class="border-bottom">
<i class="fas fa-tag fa-fw"></i>
Popularne tematy
</h4>
<div class="pt-2 pb-2">
<ul class="tag-clouds">
{% for tag in tags.others %}
<li class="d-block mb-1">
<a href="{{ route('microblog.tag', [tag.name]) }}">{{ tag.name }}</a>
<small>&times; {{ tag.count }}</small>
</li>
{% endfor %}
</ul>
</div>
</div>

<div class="box mt-2">
<h4 class="border-bottom">
<i class="fas fa-wrench fa-fw"></i>
Technologie
</h4>
<ul class="tag-clouds tag-clouds-md">
{% for tag in tags.tech %}
<li class="d-block mb-1">
<a href="{{ route('microblog.tag', [tag.name]) }}">
{% if tag.logo.filename %}
<img alt="{{ tag.name }}" src="{{ logo(tag.logo) }}"/>
{% endif %}
{{ tag.name }}
</a>
<small>&times; {{ tag.count }}</small>
</li>
{% endfor %}
</ul>
</div>
</aside>
64 changes: 1 addition & 63 deletions resources/views/microblog/home.twig
Original file line number Diff line number Diff line change
Expand Up @@ -67,69 +67,7 @@
<vue-pagination :current-page="currentPage" :total-pages="totalPages" @change="changePage"></vue-pagination>
</div>
</div>

<aside class="col-md-3">
<div class="box recommended-users">
<h4>
<i class="fas fa-tag fa-fw"></i>
Zacznij obserwować
</h4>

<div id="js-skeleton" class="card card-body mt-2" style="min-height: 300px">
</div>

<div v-cloak class="card card-body mt-2">
<div v-for="user in recommendedUsers" class="media">
<vue-avatar :id="user.id" :photo="user.photo" :name="user.name" class="d-none d-xl-block i-45 mr-2">
</vue-avatar>

<div class="media-body">
<span class="d-block mb-1">
<vue-username :user="user" class="text-body"></vue-username>
</span>
<vue-follow-button :user-id="user.id" class="btn-sm"></vue-follow-button>
</div>
</div>
</div>
</div>

<div class="box mt-2">
<h4 class="border-bottom">
<i class="fas fa-tag fa-fw"></i>
Popularne tematy
</h4>
<div class="pt-2 pb-2">
<ul class="tag-clouds">
{% for tag in tags.others %}
<li class="d-block mb-1">
<a href="{{ route('microblog.tag', [tag.name]) }}">{{ tag.name }}</a>
<small>&times; {{ tag.count }}</small>
</li>
{% endfor %}
</ul>
</div>
</div>

<div class="box mt-2">
<h4 class="border-bottom">
<i class="fas fa-wrench fa-fw"></i>
Technologie
</h4>
<ul class="tag-clouds tag-clouds-md">
{% for tag in tags.tech %}
<li class="d-block mb-1">
<a href="{{ route('microblog.tag', [tag.name]) }}">
{% if tag.logo.filename %}
<img alt="{{ tag.name }}" src="{{ logo(tag.logo) }}"/>
{% endif %}
{{ tag.name }}
</a>
<small>&times; {{ tag.count }}</small>
</li>
{% endfor %}
</ul>
</div>
</aside>
{% include 'microblog.aside' %}
<vue-notifications position="bottom right"></vue-notifications>
</div>

Expand Down
7 changes: 4 additions & 3 deletions resources/views/microblog/view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@

{% block container %}
<div id="js-microblog" class="row">
<div class="col-12">
<div class="col-md-9">
<vue-microblog :microblog="microblog" :wrap="false"></vue-microblog>
</div>

{% include 'microblog.aside' %}
<vue-notifications position="bottom right" />
</div>

<script>
var microblog = {{ microblog|json_encode|raw }};
var flags = {{ flags|json_encode|raw }};
var popularTags = {{ popular_tags|json_encode|raw }};
var recommendedUsers = {{ recommended_users|json_encode|raw }};
</script>
{% endblock %}

0 comments on commit 1d1533a

Please sign in to comment.