Browse Source

Add edit comment action for user

pull/15/head
Nafies Luthfi 7 years ago
parent
commit
55bd94dbda
  1. 31
      app/Http/Controllers/Projects/CommentsController.php
  2. 34
      resources/views/projects/comments.blade.php
  3. 2
      resources/views/projects/partials/comment-section.blade.php
  4. 1
      routes/web/projects.php
  5. 31
      tests/Feature/Projects/ProjectCommentsTest.php

31
app/Http/Controllers/Projects/CommentsController.php

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Projects;
use Illuminate\Http\Request;
use App\Entities\Projects\Comment;
use App\Entities\Projects\Project;
use App\Http\Controllers\Controller;
@ -16,9 +17,14 @@ class CommentsController extends Controller
*/
public function index(Project $project)
{
$editableComment = null;
$comments = $project->comments()->latest()->paginate();
return view('projects.comments', compact('project', 'comments'));
if (request('action') == 'comment-edit' && request('comment_id') != null) {
$editableComment = Comment::find(request('comment_id'));
}
return view('projects.comments', compact('project', 'comments', 'editableComment'));
}
/**
@ -33,9 +39,7 @@ class CommentsController extends Controller
$this->authorize('view', $project);
$newComment = $request->validate([
'body' => 'required|string|max:255',
'fu_type_id' => 'nullable|numeric',
'objection_id' => 'nullable|numeric',
'body' => 'required|string|max:255',
]);
$project->comments()->create([
@ -47,4 +51,23 @@ class CommentsController extends Controller
return back();
}
/**
* Update the specified comment.
*
* @param \Illuminate\Http\Request $request
* @param \App\Entities\Projects\Project $project
* @param \App\Entities\Projects\Comment $comment
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project, Comment $comment)
{
$commentData = $request->validate([
'body' => 'required|string|max:255',
]);
$comment->update($commentData);
flash(__('comment.updated'), 'success');
return redirect()->route('projects.comments.index', [$project] + request(['page']));
}
}

34
resources/views/projects/comments.blade.php

@ -11,4 +11,38 @@
</div>
</div>
@if (Request::get('action') == 'comment-edit' && $editableComment)
<div id="commentModal" class="modal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
{{ link_to_route('projects.comments.index', '&times;', [$project] + request(['page']), ['class' => 'close']) }}
<h4 class="modal-title">{{ __('comment.edit') }}</h4>
</div>
{!! Form::model($editableComment, ['route' => ['projects.comments.update', $project, $editableComment->id],'method' => 'patch']) !!}
<div class="modal-body">
{!! FormField::textarea('body', ['label' => __('comment.body')]) !!}
{{ Form::hidden('page', request('page')) }}
</div>
<div class="modal-footer">
{!! Form::submit(__('comment.update'), ['class' => 'btn btn-success']) !!}
{{ link_to_route('projects.comments.index', __('app.cancel'), [$project] + request(['page']), ['class' => 'btn btn-default']) }}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
@endif
@endsection
@section('script')
<script>
(function () {
$('#commentModal').modal({
show: true,
backdrop: 'static',
});
})();
</script>
@endsection

2
resources/views/projects/partials/comment-section.blade.php

@ -12,7 +12,7 @@
<span class="label label-default pull-right">{{ $comment->created_at }}</span>
<strong>{{ $comment->creator->name }}</strong>
</legend>
{{ link_to_route('projects.comments.index', __('app.edit'), [$project, 'action' => 'comment-edit', 'comment_id' => $comment->id], ['id' => 'edit-comment-'.$comment->id, 'class' => 'small pull-right', 'title' => __('comment.edit')]) }}
{!! nl2br($comment->body) !!}
</div>
@endforeach

1
routes/web/projects.php

@ -42,6 +42,7 @@ Route::group(['middleware' => ['auth'], 'namespace' => 'Projects'], function ()
*/
Route::get('projects/{project}/comments', 'CommentsController@index')->name('projects.comments.index');
Route::post('projects/{project}/comments', 'CommentsController@store')->name('projects.comments.store');
Route::patch('projects/{project}/comments/{comment}', 'CommentsController@update')->name('projects.comments.update');
/*
* Tasks Routes

31
tests/Feature/Projects/ProjectCommentsTest.php

@ -50,4 +50,35 @@ class ProjectCommentsTest extends TestCase
'creator_id' => $admin->id,
]);
}
/** @test */
public function user_can_edit_comment()
{
$this->adminUserSigningIn();
$project = factory(Project::class)->create();
$comment = factory(Comment::class)->create([
'commentable_type' => 'projects',
'commentable_id' => $project->id,
'body' => 'This is project comment.',
]);
$this->visitRoute('projects.comments.index', $project);
$this->seeElement('a', ['id' => 'edit-comment-'.$comment->id]);
$this->click('edit-comment-'.$comment->id);
$this->seeRouteIs('projects.comments.index', [$project, 'action' => 'comment-edit', 'comment_id' => $comment->id]);
$this->submitForm(__('comment.update'), [
'body' => 'Komentar pertama.',
]);
$this->seePageIs(route('projects.comments.index', $project));
$this->see(__('comment.updated'));
$this->seeInDatabase('comments', [
'id' => $comment->id,
'commentable_type' => 'projects',
'commentable_id' => $project->id,
'body' => 'Komentar pertama.',
]);
}
}
Loading…
Cancel
Save