Skip to content

Commit

Permalink
Merge pull request #40 from grozwalker/profile-chapters
Browse files Browse the repository at this point in the history
feat(profile): add user chapters tree
  • Loading branch information
grozwalker committed Sep 14, 2019
2 parents 7e612f9 + 7863502 commit c0c9bf2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/Chapter.php
Expand Up @@ -3,7 +3,17 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class Chapter extends Model
{
public function user()
{
return $this->belongsToMany(User::class, 'read_chapters');
}

public function getIsReadAttribute()
{
return $this->user->where('id', Auth::user()->id)->count() > 0;
}
}
17 changes: 16 additions & 1 deletion app/Http/Controllers/UserController.php
Expand Up @@ -2,14 +2,29 @@

namespace App\Http\Controllers;

use App\Chapter;
use App\User;

class UserController extends Controller
{
public function show(string $name)
{
$user = User::where('name', $name)->firstOrFail();

$allChapters = Chapter::all();
$userReadChapters = $user->readChapters;

$chaptersTree = $allChapters->map(function ($chapter) use ($userReadChapters) {
return [
'id' => $chapter->id,
'path' => $chapter->path,
'is_read' => $userReadChapters->contains('id', $chapter->id),
];
});

return view('user.index', [
'user' => User::where('name', $name)->firstOrFail(),
'user' => $user,
'chaptersTree' => $chaptersTree
]);
}
}
3 changes: 3 additions & 0 deletions resources/sass/_profile.scss
@@ -0,0 +1,3 @@
.chapters-tree {
list-style: none;
}
2 changes: 2 additions & 0 deletions resources/sass/app.scss
Expand Up @@ -6,3 +6,5 @@

// Bootstrap
@import '~bootstrap/scss/bootstrap';

@import 'profile';
16 changes: 15 additions & 1 deletion resources/views/user/index.blade.php
Expand Up @@ -10,7 +10,21 @@
</div>
</div>
<div class="col-12 col-md-9 my-4 d-flex flex-column">
Main content
<ul class="list-group">
@foreach($chaptersTree as $chapter)

<li class="list-group-item">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="{{ $chapter['id'] }}" value="{{ $chapter['id'] }}" {{ $chapter['is_read'] ? 'checked' : '' }}>

<label for="{{ $chapter['id'] }}" class="form-check-label">
{{ $chapter['path'] }}
</label>
</div>
</li>

@endforeach
</ul>
</div>
</div>
@endsection

0 comments on commit c0c9bf2

Please sign in to comment.