From 9a30d17fe0f9383568e704d5f4b342d2ae64a604 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Thu, 5 Mar 2020 15:39:35 +0100 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Permet=20l'=C3=A9dition=20et=20la?= =?UTF-8?q?=20suppression=20du=20post=20du=20jour?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #7 --- app/Http/Controllers/HomeController.php | 60 ++++++++++++++++++++ resources/js/components/Autocomplete.vue | 6 ++ resources/sass/_general.scss | 11 ++++ resources/views/home.blade.php | 70 ++++++++++++++---------- routes/web.php | 2 + 5 files changed, 121 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index b4ced9b..85e842f 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -26,6 +26,7 @@ class HomeController extends Controller * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable + * @throws \Exception */ 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) { $today = new DateTime(); diff --git a/resources/js/components/Autocomplete.vue b/resources/js/components/Autocomplete.vue index 45b32c8..7486808 100644 --- a/resources/js/components/Autocomplete.vue +++ b/resources/js/components/Autocomplete.vue @@ -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: { listToSearch() { diff --git a/resources/sass/_general.scss b/resources/sass/_general.scss index e84e0cc..3da8bb9 100644 --- a/resources/sass/_general.scss +++ b/resources/sass/_general.scss @@ -1,3 +1,14 @@ h1, h2, h3, h4 { 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; + } +} diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 80aca1f..ab38643 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -13,6 +13,16 @@ {{ session('status') }} @endif + @if (session('success')) + + @endif + @if (session('errors')) + + @endif
@@ -70,37 +80,35 @@
@if($already)
L'activité du jour a déjà été entrée.
- @else -
-
-
-
-

{{ strftime('%B %G', $today->format('U')) }}

-
-
-
- @csrf -
-
- -
-
-
- {{ $errors->first('message') }} -
-
-
-
- -
+ @endif +
+
+
+
+

{{ strftime('%B %G', $today->format('U')) }}

+
+
+ + @csrf +
+
+ +
+
+ {{ $errors->first('message') }}
- -
+
+
+ +
+
+
+
- @endif +
@foreach($posts as $post)
@@ -110,10 +118,16 @@

{{ ucfirst(\Carbon\Carbon::instance($post->date_post)->diffForHumans()) }}

- + @if($today->format('Y-m-d') === \Carbon\Carbon::instance($post->date_post)->format('Y-m-d')) + +
+ @csrf + +
+ @endif
-
+
{!! $tag_detector->linkTagFrom($post->content) !!}