Skip to content

Commit

Permalink
Merge pull request #30 from HE-Arc/29-timeline-deletion
Browse files Browse the repository at this point in the history
Added deletion and removed redundant function in formfunction.js
  • Loading branch information
katsulon authored Dec 17, 2024
2 parents 3f8b618 + 0b07203 commit 91398c1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
23 changes: 18 additions & 5 deletions timeliner/app/Http/Controllers/TimelineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]);
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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,
]);
}
Expand Down Expand Up @@ -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."]);
}
}
8 changes: 0 additions & 8 deletions timeliner/resources/js/formfunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
});
});
4 changes: 2 additions & 2 deletions timeliner/resources/views/timeline/partials/comment.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<button type="submit" class="btn btn-success bi bi-check2"> Confirm Edit</button>
</form>

<form action="{{ route('comment.destroy', $comment->id) }}" method="POST">
<form action="{{ route('comment.destroy', $comment->id) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete this comment?');">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger bi bi-trash delete-button"> Delete</button>
<button type="submit" class="btn btn-danger bi bi-trash"> Delete</button>
</form>
@endif
@endauth
Expand Down
23 changes: 18 additions & 5 deletions timeliner/resources/views/timeline/partials/timeline.blade.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
@php
use App\Models\Ownership;
@endphp
<div class="timeline-for-list-template">
<div class="row">
<div class="col">
<div class="row align-items-stretch d-flex">
<div class="col-11 p-0">
<div class="card mb-4">
<div class="card-body">
@if(! $timeline->private) <!-- if public, show title with a globe icon -->
<h5 class="card-title bi bi-globe"> {{ $timeline->name}}</h5>
<h5 class="card-title bi bi-globe"> {{ $timeline->name }}</h5>
@else
<h5 class="card-title bi bi-lock-fill"> {{ $timeline->name }}</h5>
@endif
<p class="card-text">{{$timeline->description}}</p>
<a href="timeline/{{ $timeline->id }}" class="btn btn-primary stretched-link bi bi-eye"> View</a>
<p class="card-text">{{ $timeline->description }}</p>
<a href="{{ route("timeline.show", $timeline->id) }}" class="btn btn-primary stretched-link bi bi-eye"> View</a>
</div>
</div>
</div>

@auth
@if (Ownership::find($timeline->id . Auth::user()->id))
<form class="d-flex align-items-stretch mb-4" action="{{ route('timeline.destroy', $timeline->id) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete this timeline?');">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger bi bi-trash d-flex align-items-center justify-content-center"></button>
</form>
@endif
@endauth
</div>
</div>
2 changes: 1 addition & 1 deletion timeliner/resources/views/timeline/timeline.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="p-6 text-gray-900 dark:text-gray-100">

@auth
@if ($isOwner) <a href="{{ $timeline->id }}/edit" class="btn btn-primary bi bi-pencil"> Edit</a> @endif
@if ($isOwner) <a href="{{ route("timeline.edit", $timeline->id) }}" class="btn btn-primary bi bi-pencil"> Edit</a> @endif
@endauth

<!-- Timeline show section -->
Expand Down

0 comments on commit 91398c1

Please sign in to comment.