Skip to content

Commit

Permalink
Merge pull request #233 from fey/feature/welcome_page_add_comments
Browse files Browse the repository at this point in the history
Последние комментарии на главной
  • Loading branch information
fey committed May 8, 2020
2 parents 810bab8 + 32807f2 commit ad7f47b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 26 deletions.
14 changes: 14 additions & 0 deletions app/Helpers/CommentHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use App\Comment;

if (!function_exists('getCommentLink')) {
function getCommentLink(Comment $comment): string
{
$commentableResourceName = str_plural(strtolower(class_basename($comment->commentable_type)));
$commentableUrl = route("{$commentableResourceName}.show", $comment->commentable);
$commentUrl = "{$commentableUrl}#comment-{$comment->id}";

return $commentUrl;
}
}
53 changes: 29 additions & 24 deletions app/Http/Controllers/WelcomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Activity;
use App\Comment;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Collection;
Expand All @@ -12,39 +13,43 @@ class WelcomeController extends Controller
public function index()
{
$logItems = Activity::latest()->with('causer')->limit(10)->get();
$chart = $this->getChart();
$comments = Comment::latest()->limit(10)->get();

return view(
'welcome',
[
'logItems' => $logItems,
'chart' => $chart,
'comments' => $comments
]
);
}

/**
* @return \Generator
*/
private function getChart(): \Generator
{
$countActivitiesByDays = Activity::all()
->groupBy(function (Activity $activity) {
return $activity->created_at->format('Y-m-d');
return $activity->created_at->format('Y-m-d');
})
->map(function (Collection $group) {
return $group->count();
return $group->count();
});

$chart = CarbonPeriod::create(now()->subMonths(12), '1 day', now())
->map(function (Carbon $dayDate) use ($countActivitiesByDays) {
$day = $dayDate->format('Y-m-d');
$dayActivitiesCount = $countActivitiesByDays->get($day, 0);

if ($dayActivitiesCount < 1) :
return 0;
elseif ($dayActivitiesCount < 5) :
return 1;
elseif ($dayActivitiesCount < 10) :
return 2;
elseif ($dayActivitiesCount < 15) :
return 3;
else :
return 4;
endif;
->map(function (Carbon $dayDate) use ($countActivitiesByDays): int {
$day = $dayDate->format('Y-m-d');
$dayActivitiesCount = $countActivitiesByDays->get($day, 0);
$magicNumber = 4;
$maxDayActivityLevel = 4;
$dayActivityLevel = (ceil(($dayActivitiesCount) / $magicNumber));

return min($dayActivityLevel, $maxDayActivityLevel);
});

return view(
'welcome',
[
'logItems' => $logItems,
'chart' => $chart,
]
);
return $chart;
}
}
3 changes: 3 additions & 0 deletions resources/lang/en/welcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
'coming_soon_list' => [
'Bind repository with resolved exercises',
],
'comments' => [
'latest' => 'Latest comments'
]
];
3 changes: 3 additions & 0 deletions resources/lang/ru/welcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
],
'coming_soon_list' => [
'Привязывать репозиторий с решёнными упражнениями',
],
'comments' => [
'latest' => 'Последние комментарии'
]
];
2 changes: 1 addition & 1 deletion resources/views/components/comment/_comment.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="mr-3"></div>
<div class="media-body">
<h5 class="mt-0 mb-1">
<a href="#comment-{{ $comment->id }}" class="small">#</a>
<a href="{{ getCommentLink($comment) }}" class="small">#</a>
{{ $comment->user->name }}
<small class="text-muted">- {{ $comment->created_at->diffForHumans() }}</small>
</h5>
Expand Down
21 changes: 20 additions & 1 deletion resources/views/welcome.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@extends('layouts.app')
@php
/** @var \Illuminate\Support\Collection|\App\Activity[] $logItems */
/** @var \Illuminate\Support\Collection|\App\Comment[] $comments */
@endphp
@section('content')
<h1 class="my-4">{{ __('welcome.title') }}</h1>
Expand Down Expand Up @@ -40,7 +41,7 @@
</div>

<div class="row">
<div class="col-md-8">
<div class="col-md-6">
<h3 class="my-3"><a href="{{ (route('log.index')) }}">{{ __('activitylog.title') }}</a></h3>
@foreach($logItems as $logItem)
<div class="media text-muted pt-1">
Expand Down Expand Up @@ -95,6 +96,24 @@
</div>
@endforeach
</div>
<div class="col-md-6">
<h3 class="h4 my-3">@lang('welcome.comments.latest')</h3>
@foreach($comments as $comment)
<div class="media text-muted pt-1">
<div class="media-body pb-1 mb-0 small lh-125 border-bottom border-gray">
<div class="d-flex justify-content-between align-items-center w-100">
<strong class="text-gray-dark">
@if($comment->user)
<a href="{{ route('users.show', $comment->user->id) }}">{{ $comment->user->name }}</a>
@endif
</strong>
<a href="{{ getCommentLink($comment) }}">{{ $comment->created_at }}</a>
</div>
<span>{{ str_limit($comment->content, 80) }}</span>
</div>
</div>
@endforeach
</div>
</div>

<!-- /.row -->
Expand Down

0 comments on commit ad7f47b

Please sign in to comment.