Skip to content

Commit

Permalink
Merge pull request #236 from fey/fix/update-comments
Browse files Browse the repository at this point in the history
fix comments
  • Loading branch information
fey committed May 9, 2020
2 parents ad7f47b + 76da92b commit 345d878
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
1 change: 0 additions & 1 deletion app/Helpers/CommentHelper.php
Expand Up @@ -8,7 +8,6 @@ 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;
}
}
25 changes: 17 additions & 8 deletions app/Http/Controllers/CommentController.php
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

use function getCommentLink;

class CommentController extends Controller
{
public function __construct()
Expand Down Expand Up @@ -37,10 +39,10 @@ public function store(Request $request)
activity()
->performedOn($commentable)
->causedBy($user)
->withProperties(['comment' => $comment, 'url' => $this->getCommentUrl($comment)])
->withProperties(['comment' => $comment, 'url' => getCommentLink($comment)])
->log('commented');

return redirect($this->getCommentUrl($comment));
return redirect(getCommentLink($comment));
}

public function update(Request $request, Comment $comment)
Expand All @@ -54,9 +56,20 @@ public function update(Request $request, Comment $comment)
]
);
$content = $request->get('content', $comment->content);
$comment->update(['content' => $content]);
$comment->fill(['content' => $content]);

if ($comment->save()) {
flash()->success(__('layout.flash.success'));
}

return redirect(getCommentLink($comment));
}

return redirect($this->getCommentUrl($comment));
public function show(Comment $comment)
{
return redirect(
getCommentLink($comment)
);
}

public function destroy(Comment $comment)
Expand All @@ -65,8 +78,4 @@ public function destroy(Comment $comment)

return back();
}
private function getCommentUrl(Comment $comment)
{
return url()->previous() . '#comment-' . $comment->id;
}
}
2 changes: 2 additions & 0 deletions resources/views/components/comment/_modal.blade.php
Expand Up @@ -18,6 +18,8 @@
</button>
</div>
<div class="modal-body">
{!! Form::hidden('commentable_type', $comment->commentable_type) !!}
{!! Form::hidden('commentable_id', $comment->commentable_id) !!}
{!! Form::textarea('content', __('comment.update_comment_here'), $content)->attrs(['rows' => 3])->required() !!}
</div>
<div class="modal-footer text-left">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/exercise/show.blade.php
Expand Up @@ -37,7 +37,7 @@
<p>{{ __('exercise.show.who_completed') }}</p>
<ul>
@foreach($exercise->users as $user)
<li>{{ $user->name }}</li>
<li><a href="{{ route('users.show', $user) }}">{{ $user->name }}</a></li>
@endforeach
</ul>
@endif
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Expand Up @@ -34,5 +34,5 @@ static function () {
Route::resource('chapters', 'ChapterController')->only('index', 'show');
Route::resource('exercises', 'ExerciseController')->only('index', 'show');
Route::resource('log', 'ActivitylogController')->only('index');
Route::resource('comments', 'CommentController')->only('store', 'update', 'destroy');
Route::resource('comments', 'CommentController')->only('store', 'update', 'show', 'destroy');
});

0 comments on commit 345d878

Please sign in to comment.