Skip to content

Commit

Permalink
Add ability to edit news
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpar06 committed May 27, 2024
1 parent 84ecc61 commit 9cc4167
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public function news(Request $request): View
$attrs['user_id'] = Auth::user()->id;

$this->newsSvc->addNews($attrs);
} elseif ($request->isMethod('patch')) {
$attrs = $request->post();
$attrs['user_id'] = Auth::user()->id;

$this->newsSvc->updateNews($attrs);
} elseif ($request->isMethod('delete')) {
$id = $request->input('news_id');
$this->newsSvc->deleteNews($id);
Expand Down
1 change: 1 addition & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ private function mapAdminRoutes()

Route::match([
'get',
'patch',
'post',
'delete',
], 'dashboard/news', ['uses' => 'DashboardController@news'])
Expand Down
21 changes: 21 additions & 0 deletions app/Services/NewsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Contracts\Service;
use App\Events\NewsAdded;
use App\Models\News;
use App\Repositories\NewsRepository;
use Prettus\Validator\Exceptions\ValidatorException;

class NewsService extends Service
{
Expand Down Expand Up @@ -32,6 +34,25 @@ public function addNews(array $attrs)
return $news;
}

/**
* Update a news
*
* @param array $attrs
* @return ?News
*
* @throws ValidatorException
*/
public function updateNews(array $attrs): ?News
{
$news = $this->newsRepo->find($attrs['id']);

if (!$news) {
return null;
}

return $this->newsRepo->update($attrs, $attrs['id']);
}

/**
* Delete something from the news items
*
Expand Down
46 changes: 44 additions & 2 deletions resources/views/admin/dashboard/news.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="pjax_news_wrapper">
<div class="card border-blue-bottom">
<div class="card border-blue-bottom" id="add_news">
<div class="content">
<div class="header">
<h4 class="title">Add News</h4>
Expand All @@ -22,6 +22,31 @@
{{ Form::close() }}
</div>
</div>
<div class="card border-blue-bottom" id="edit_news" style="display:none;">
<div class="content">
<div class="header">
<h4 class="title" id="edit_title">Edit News</h4>
</div>
{{ Form::open(['route' => 'admin.dashboard.news', 'method' => 'patch', 'class' => 'pjax_news_form']) }}
{{ Form::hidden('id', '', ['id' => 'edit_id']) }}
<table class="table">
<tr>
<td>{{ Form::label('subject', 'Subject:') }}</td>
<td>{{ Form::text('subject', '', ['id' => 'edit_subject', 'class' => 'form-control']) }}</td>
</tr>
<tr>
<td>{{ Form::label('body', 'Body:') }}</td>
<td>{!! Form::textarea('body', '', ['id' => 'edit_body', 'class' => 'editor']) !!}</td>
</tr>
<tr>
<td colspan="2" class="text-right">
<button type="button" class="btn btn-warning btn-s" onclick="closeEdit()">Cancel</button>
{{ Form::button('<i class="fas fa-pencil-alt"></i>&nbsp;edit', ['type' => 'submit', 'class' => 'btn btn-success btn-s']) }}
</td>
</table>
{{ Form::close() }}
</div>
</div>
<div class="card border-blue-bottom">
<div class="content">
@if($news->count() === 0)
Expand All @@ -43,7 +68,8 @@
<td>{!! $item->body!!}</td>
<td>{{ optional($item->user)->name_private }}</td>
<td>{{ $item->created_at->format('d.M.y') }}</td>
<td>
<td style="display: flex;gap: .5rem;">
<button class="btn btn-primary btn-xs text-small" onclick="editNews({{ $item->toJson() }})">Edit</button>
{{ Form::open(['route' => 'admin.dashboard.news', 'method' => 'delete', 'class' => 'pjax_news_form']) }}
{{ Form::hidden('news_id', $item->id) }}
{{ Form::button('Delete', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs text-small', 'onclick' => "return confirm('Are you sure?')"]) }}
Expand All @@ -61,5 +87,21 @@
<script src="{{ public_asset('assets/vendor/ckeditor4/ckeditor.js') }}"></script>
<script>
$(document).ready(function () { CKEDITOR.replace('news_editor'); });
function editNews(news) {
CKEDITOR.replace('edit_body')
$('#edit_title').html('Edit News: ' + news.subject);
$('#edit_subject').val(news.subject)
CKEDITOR.instances.edit_body.setData(news.body)
$('#edit_id').val(news.id)
$('#add_news').hide();
$('#edit_news').show();
}
function closeEdit() {
$('#edit_news').hide();
$('#add_news').show()
}
</script>
@endsection

0 comments on commit 9cc4167

Please sign in to comment.