Skip to content

Commit

Permalink
Complete essentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jan 11, 2024
1 parent 32ddf92 commit fb31c5e
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/Http/Livewire/AdminLessons.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Livewire;

use App\Models\Course;
use App\Models\Lesson;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -23,7 +24,7 @@ class AdminLessons extends Component
public function mount(): void
{
$this->lessons = Lesson::all();
$this->courses = Lesson::all();
$this->courses = Course::all();
}

public function editLesson($lessonId): void
Expand Down
77 changes: 76 additions & 1 deletion app/Http/Livewire/AdminResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,92 @@

namespace App\Http\Livewire;

use App\Models\Lesson;
use App\Models\Resource;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Component;

class AdminResources extends Component
{
public $resources;
public Collection $resources;
public Collection $lessons;
public Resource $editingResource;
public int $lesson_id;
public string $name = '';
public string $url = '';
public string $type = '';
public string $description = '';

public bool $showModal = false;

public function mount(): void
{
$this->resources = Resource::all();
$this->lessons = Lesson::all();
}

public function editResource($resourceId): void
{
$this->editingResource = Resource::find($resourceId);
$this->lesson_id = $this->editingResource->lesson_id;
$this->name = $this->editingResource->name;
$this->url = $this->editingResource->url;
$this->type = $this->editingResource->type;
$this->description = $this->editingResource->description;
$this->showModal = true;
}

public function deleteResource($resourceId): void
{
$resource = Resource::find($resourceId);
$resource->delete();
$this->resources = Resource::all();
}

public function createNewResource(): void
{
$this->resetInputFields();
$this->showModal = true;
}

public function saveResource(): void
{
if (isset($this->editingResource->id)) {
// Update existing resource
$this->editingResource->update([
'lesson_id' => $this->lesson_id,
'name' => $this->name,
'url' => $this->url,
'type' => $this->type,
'description' => $this->description,
]);
} else {
// Create new resource
Resource::create([
'lesson_id' => $this->lesson_id,
'name' => $this->name,
'url' => $this->url,
'type' => $this->type,
'description' => $this->description,
]);
}

$this->showModal = false;
$this->resources = Resource::all();
$this->resetInputFields();
}

private function resetInputFields(): void
{
$this->editingResource = new Resource();
$this->lesson_id = 0;
$this->name = '';
$this->url = '';
$this->type = '';
$this->description = '';
}

public function render(): View
{
return view('admin.resources');
Expand Down
2 changes: 1 addition & 1 deletion resources/views/admin/lessons.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class="btn btn-sm btn-circle ring-1 ring-inset ring-offset-1 ring-error btn-erro
<label for="course_id">Course</label>
<select id="course_id" wire:model="course_id" class="input input-info text-xs text-gray-100">
@foreach($courses as $course)
<option value="{{ $course->id }}">{{ $course->title }}</option>
<option value="{{ $course->id }}">{{ $course->title }} {{ $course->id }}</option>
@endforeach
</select>

Expand Down
51 changes: 49 additions & 2 deletions resources/views/admin/resources.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<div>
<!-- Resources Table -->
<h2 class="text-center text-xl font-semibold">Resources</h2>
<div class="flex justify-end">
<button class="btn btn-sm btn-circle ring-1 ring-inset ring-offset-1"
wire:click="createNewResource">
<i class="fa-solid fa-plus"></i>
</button>
</div>
<table class="table mb-5 table-zebra ring-1 text-xs rounded-md overflow-hidden mt-3">
<thead>
<tr>
Expand All @@ -19,10 +25,11 @@
<td>{{ $resource->type }}</td>
<td>{{ $resource->description }}</td>
<td class="flex gap-2" colspan="4">
<button class="btn btn-sm btn-circle ring-1 ring-inset ring-offset-1">
<button wire:click="editResource({{ $resource->id }})"
class="btn btn-sm btn-circle ring-1 ring-inset ring-offset-1">
<i class="fa-solid fa-gear"></i>
</button>
<button
<button wire:click="deleteResource({{ $resource->id }})"
class="btn btn-sm btn-circle ring-1 ring-inset ring-offset-1 ring-error btn-error">
<i class="fa-solid fa-trash-can"></i>
</button>
Expand All @@ -31,4 +38,44 @@ class="btn btn-sm btn-circle ring-1 ring-inset ring-offset-1 ring-error btn-erro
@endforeach
</tbody>
</table>

<!--Modal-->
<dialog id="resource_modal" class="modal bg-gray-100 bg-opacity-60 z-50" {{$showModal ? 'open' : ''}}>
<div class="modal-box">
<h3 class="font-bold text-lg">Add Resource</h3>
<form class="flex flex-col" wire:submit.prevent="saveResource">
<div class="py-4 text-gray-100 flex flex-col">
<label for="lesson_id">Lesson</label>
<select id="lesson_id" wire:model="lesson_id" class="input input-info text-xs text-gray-100">
@foreach($lessons as $lesson)
<option value="{{ $lesson->id }}">{{ $lesson->title }}</option>
@endforeach
</select>

<label for="name">Name</label>
<input type="text" id="name" wire:model="name" class="input text-xs input-info text-gray-100">

<label for="url">URL</label>
<input type="text" id="url" wire:model="url" class="input input-info text-xs text-gray-100">

<label for="type">Type</label>
<input type="text" id="type" wire:model="type" class="input input-info text-xs text-gray-100">

<label for="description">Description</label>
<textarea id="description" wire:model="description" class="textarea textarea-info text-xs text-gray-100"></textarea>
</div>
<div class="modal-action py-4 gap-2 flex w-full">
<button type="submit" class="btn w-1/2 btn-sm ring-1 ring-inset ring-offset-1 ring-offset-gray-100">
Save
<i class="fa-solid fa-check"></i>
</button>
<button type="button" class="btn w-1/2 btn-sm ring-1 ring-inset ring-offset-1 ring-offset-error" onclick="let resource_modal = document.getElementById('resource_modal');
resource_modal.close()">
Cancel
<i class="fa-solid fa-xmark"></i>
</button>
</div>
</form>
</div>
</dialog>
</div>

0 comments on commit fb31c5e

Please sign in to comment.