Permet l'édition et la suppression du post du jour

Fix #7
This commit is contained in:
Clement Desmidt 2020-03-05 15:39:35 +01:00
parent 37b6bcd6e7
commit 9a30d17fe0
5 changed files with 121 additions and 28 deletions

View File

@ -26,6 +26,7 @@ class HomeController extends Controller
* Show the application dashboard. * Show the application dashboard.
* *
* @return \Illuminate\Contracts\Support\Renderable * @return \Illuminate\Contracts\Support\Renderable
* @throws \Exception
*/ */
public function index() public function index()
{ {
@ -59,6 +60,65 @@ class HomeController extends Controller
]); ]);
} }
/**
* Edit today's entry
*/
public function edit(TagDetectorService $tag_detector)
{
/** @var Post $today_post */
$today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d'))
->where('user_id', Auth::user()->getAuthIdentifier())
->firstOrFail();
$data = request()->validate([
'message' => 'required'
]);
$tags_to_delete = PostsTag::where('post_id', $today_post->id)->get();
foreach ($tags_to_delete as $tag_to_delete) {
$tag_to_delete->delete();
}
$tags = $tag_detector->detectFrom($data['message']);
$today_post->content = $data['message'];
$today_post->save();
$all_tags = Tag::all();
$all_tags_names = [];
foreach ($all_tags as $tag) {
$all_tags_names[$tag->id] = $tag->name;
}
foreach ($tags as $tag) {
if (in_array($tag, $all_tags_names)) {
PostsTag::create([
'post_id' => $today_post->id,
'tag_id' => array_search($tag, $all_tags_names),
]);
}
}
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
}
/**
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function delete()
{
try {
$today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d'))
->where('user_id', Auth::user()->getAuthIdentifier())
->firstOrFail();
$today_post->delete();
return Redirect::to('home')->withSuccess('Great! Your today\'s post is now deleted. You can make a new one!');
} catch (\ErrorException $e) {
return Redirect::to('home')->withErrors('Oh no! We could\'t find your today\'s post.');
}
}
public function store(TagDetectorService $tag_detector) public function store(TagDetectorService $tag_detector)
{ {
$today = new DateTime(); $today = new DateTime();

View File

@ -51,6 +51,12 @@
} }
} }
}); });
let already = document.getElementById("collapse").classList.contains("collapse");
if (already) {
let post = document.querySelector(".already").innerText;
document.querySelector("textarea").innerHTML = post;
this.inputValue = post;
}
}, },
computed: { computed: {
listToSearch() { listToSearch() {

View File

@ -1,3 +1,14 @@
h1, h2, h3, h4 { h1, h2, h3, h4 {
font-family: $font-family-base; font-family: $font-family-base;
} }
.card-options .btn-link {
color: #9aa0ac;
text-decoration: none;
min-width: 0;
padding: 0;
margin-left: 5px;
&:hover {
color: #6e7687;
}
}

View File

@ -13,6 +13,16 @@
{{ session('status') }} {{ session('status') }}
</div> </div>
@endif @endif
@if (session('success'))
<div class="alert alert-success" role="alert">
{{ session('success') }}
</div>
@endif
@if (session('errors'))
<div class="alert alert-danger" role="alert">
{{ session('errors') }}
</div>
@endif
</div> </div>
<div class="row flex-fill"> <div class="row flex-fill">
<div class="col-sm-6 col-lg-3"> <div class="col-sm-6 col-lg-3">
@ -70,37 +80,35 @@
<div class="col-md-12"> <div class="col-md-12">
@if($already) @if($already)
<div class="alert alert-primary">L'activité du jour a déjà été entrée.</div> <div class="alert alert-primary">L'activité du jour a déjà été entrée.</div>
@else @endif
<div class="row"> <div class="row @if($already) collapse @endif" id="collapse">
<div class="col-sm-12"> <div class="col-sm-12">
<div class="card"> <div class="card">
<div class="card-header"> <div class="card-header">
<h3 class="card-title">{{ strftime('%B %G', $today->format('U')) }}</h3> <h3 class="card-title">{{ strftime('%B %G', $today->format('U')) }}</h3>
</div> </div>
<div class="card-body"> <div class="card-body">
<form action="{{ url('store') }}" method="post" accept-charset="utf-8"> <form action="@if($already) {{ url('edit') }}@else{{ url('store') }}@endif" method="post" accept-charset="utf-8">
@csrf @csrf
<div class="contact-form"> <div class="contact-form">
<div class="form-group"> <div class="form-group">
<label class="control-label col-sm-5" for="message">Quoi de neuf aujourd'hui ?</label> <label class="control-label col-sm-5" for="message">Quoi de neuf aujourd'hui ?</label>
<div class="col-sm-10"> <div class="col-sm-10">
<div id="app"> <div id="app"></div>
</div> <span class="text-danger">{{ $errors->first('message') }}</span>
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary ml-auto">J'enregistre ma journée</button>
</div>
</div> </div>
</div> </div>
</form> <div class="form-group">
</div> <div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary ml-auto">J'enregistre ma journée</button>
</div>
</div>
</div>
</form>
</div> </div>
</div> </div>
</div> </div>
@endif </div>
</div> </div>
@foreach($posts as $post) @foreach($posts as $post)
<div class="col-md-6 col-xl-4"> <div class="col-md-6 col-xl-4">
@ -110,10 +118,16 @@
<h3 class="card-title">{{ ucfirst(\Carbon\Carbon::instance($post->date_post)->diffForHumans()) }}</h3> <h3 class="card-title">{{ ucfirst(\Carbon\Carbon::instance($post->date_post)->diffForHumans()) }}</h3>
<div class="card-options"> <div class="card-options">
<a href="#" class="card-options-collapse" data-toggle="card-collapse"><i class="fe fe-chevron-up"></i></a> <a href="#" class="card-options-collapse" data-toggle="card-collapse"><i class="fe fe-chevron-up"></i></a>
<a href="#" class="card-options-remove" data-toggle="card-remove"><i class="fe fe-x"></i></a> @if($today->format('Y-m-d') === \Carbon\Carbon::instance($post->date_post)->format('Y-m-d'))
<button class="btn btn-link" data-toggle="collapse" data-target="#collapse" aria-expanded="true" aria-controls="collapse"><i class="fe fe-edit-2"></i></button>
<form action="{{ url('delete') }}" method="post" accept-charset="utf-8">
@csrf
<button class="btn btn-link" data-toggle="card-remove"><i class="fe fe-x"></i></button>
</form>
@endif
</div> </div>
</div> </div>
<div class="card-body"> <div class="card-body @if($today->format('Y-m-d') === \Carbon\Carbon::instance($post->date_post)->format('Y-m-d')) already @endif ">
{!! $tag_detector->linkTagFrom($post->content) !!} {!! $tag_detector->linkTagFrom($post->content) !!}
</div> </div>
<div class="card-footer"> <div class="card-footer">

View File

@ -16,4 +16,6 @@ Auth::routes();
Route::get('/', 'WelcomeController@index')->name('home'); Route::get('/', 'WelcomeController@index')->name('home');
Route::get('/home', 'HomeController@index')->name('dashboard'); Route::get('/home', 'HomeController@index')->name('dashboard');
Route::post('store', 'HomeController@store')->name('store'); Route::post('store', 'HomeController@store')->name('store');
Route::post('delete', 'HomeController@delete')->name('delete');
Route::post('edit', 'HomeController@edit')->name('edit');
Route::post('stats', 'StatsController@tag')->name('stats'); // TODO make a group for stats Route::post('stats', 'StatsController@tag')->name('stats'); // TODO make a group for stats