From 0b07203e038f2de30fe2ab1befe6126e9269ce0e Mon Sep 17 00:00:00 2001 From: Elisa Goloviatinski Date: Tue, 17 Dec 2024 23:53:39 +0100 Subject: [PATCH] Added deletion and removed redundant function in formfunction.js --- .../Http/Controllers/TimelineController.php | 23 +++++++++++++++---- timeliner/resources/js/formfunctions.js | 8 ------- .../views/timeline/partials/comment.blade.php | 4 ++-- .../timeline/partials/timeline.blade.php | 23 +++++++++++++++---- .../views/timeline/timeline.blade.php | 2 +- 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/timeliner/app/Http/Controllers/TimelineController.php b/timeliner/app/Http/Controllers/TimelineController.php index 03738de..3c8e9f1 100644 --- a/timeliner/app/Http/Controllers/TimelineController.php +++ b/timeliner/app/Http/Controllers/TimelineController.php @@ -76,7 +76,7 @@ public function store(Request $request) 'nodes.*.name' => 'required|string|max:255', 'nodes.*.milestones' => 'nullable|array', 'nodes.*.milestones.*.date' => 'required|date', - 'nodes.*.milestones.*.description' => 'nullable|string|max:255', + 'nodes.*.milestones.*.description' => 'required|string|max:255', ]); $timeline = Timeline::create(['name' => $validated['name'], 'description' => $validated['description'], 'private'=> !$validated['private']]); @@ -142,7 +142,7 @@ public function update(Request $request, $id) 'nodes.*.name' => 'required|string|max:255', 'nodes.*.milestones' => 'nullable|array', 'nodes.*.milestones.*.date' => 'required|date', - 'nodes.*.milestones.*.description' => 'nullable|string|max:255', + 'nodes.*.milestones.*.description' => 'required|string|max:255', ]); // Update timeline details @@ -154,7 +154,7 @@ public function update(Request $request, $id) // Handle nodes if (!empty($validated['nodes'])) { - // First, delete any nodes that were removed from the form + // Delete nodes removed from the form $existingNodeIds = collect($validated['nodes'])->pluck('id')->filter(); Node::where('timeline', $timeline->id) ->whereNotIn('id', $existingNodeIds) @@ -167,13 +167,13 @@ public function update(Request $request, $id) $node = Node::findOrFail($nodeData['id']); $node->update([ 'name' => $nodeData['name'], - 'color' => '#FFFFFF', // Default color (you can adjust this) + 'color' => '#FFFFFF', // Default color) ]); } else { // Create new node $node = Node::create([ 'name' => $nodeData['name'], - 'color' => '#FFFFFF', // Default color (you can adjust this) + 'color' => '#FFFFFF', // Default color 'timeline' => $timeline->id, ]); } @@ -209,4 +209,17 @@ public function update(Request $request, $id) ->withErrors(["You don't have access."]); } + public function destroy($id) + { + $timeline = Timeline::findOrFail($id); + + if (($timeline != null) && (Auth::check() && Ownership::find($timeline->id . Auth::user()->id))) { + $timeline->delete(); + return redirect()->route('timeline.index') + ->with('success', 'Timeline deleted successfully.'); + } + + return redirect()->route('timeline.index') + ->withErrors(["You don't have the necessary rights."]); + } } diff --git a/timeliner/resources/js/formfunctions.js b/timeliner/resources/js/formfunctions.js index 433972b..1dfe303 100644 --- a/timeliner/resources/js/formfunctions.js +++ b/timeliner/resources/js/formfunctions.js @@ -13,12 +13,4 @@ document.addEventListener('DOMContentLoaded', () => { toggleElementById(event.target.dataset.target); }); }); - - document.querySelectorAll('.delete-button').forEach((button) => { - button.addEventListener('click', (event) => { - if (!confirmDelete()) { - event.preventDefault(); - } - }); - }); }); diff --git a/timeliner/resources/views/timeline/partials/comment.blade.php b/timeliner/resources/views/timeline/partials/comment.blade.php index 2a0be66..8d60616 100644 --- a/timeliner/resources/views/timeline/partials/comment.blade.php +++ b/timeliner/resources/views/timeline/partials/comment.blade.php @@ -19,10 +19,10 @@ -
+ @csrf @method('DELETE') - +
@endif @endauth diff --git a/timeliner/resources/views/timeline/partials/timeline.blade.php b/timeliner/resources/views/timeline/partials/timeline.blade.php index ffffd8d..238d962 100644 --- a/timeliner/resources/views/timeline/partials/timeline.blade.php +++ b/timeliner/resources/views/timeline/partials/timeline.blade.php @@ -1,17 +1,30 @@ +@php +use App\Models\Ownership; +@endphp
-
-
+
+
@if(! $timeline->private) -
{{ $timeline->name}}
+
{{ $timeline->name }}
@else
{{ $timeline->name }}
@endif -

{{$timeline->description}}

- View +

{{ $timeline->description }}

+ id) }}" class="btn btn-primary stretched-link bi bi-eye"> View
+ + @auth + @if (Ownership::find($timeline->id . Auth::user()->id)) +
+ @csrf + @method('DELETE') + +
+ @endif + @endauth
diff --git a/timeliner/resources/views/timeline/timeline.blade.php b/timeliner/resources/views/timeline/timeline.blade.php index 29c1e39..be723f0 100644 --- a/timeliner/resources/views/timeline/timeline.blade.php +++ b/timeliner/resources/views/timeline/timeline.blade.php @@ -11,7 +11,7 @@
@auth - @if ($isOwner) Edit @endif + @if ($isOwner) id) }}" class="btn btn-primary bi bi-pencil"> Edit @endif @endauth