Skip to content

Commit cb0eae2

Browse files
add user management
1 parent 6370222 commit cb0eae2

File tree

10 files changed

+575
-2
lines changed

10 files changed

+575
-2
lines changed

app/Livewire/BaseComponent.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Livewire;
44

5+
use App\Traits\InteractWithModal;
56
use App\Traits\Livewire\WithChangeOrder;
67
use App\Traits\WithGetFilterData;
78
use Livewire\WithoutUrlPagination;
@@ -12,7 +13,8 @@ class BaseComponent extends Component {
1213
use WithPagination,
1314
WithoutUrlPagination,
1415
WithGetFilterData,
15-
WithChangeOrder;
16+
WithChangeOrder,
17+
InteractWithModal;
1618

1719
public int $imageIttr = 0;
1820

app/Traits/InteractWithModal.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
trait InteractWithModal {
6+
public $modals = [
7+
'acc-modal' => false,
8+
];
9+
10+
public function addModal(string $modal) {
11+
$this->modals[$modal] = false;
12+
}
13+
14+
public function openModal(string $modal = 'acc-modal') {
15+
$this->modals[$modal] = true;
16+
}
17+
18+
public function closeModal(string $modal = 'acc-modal') {
19+
$this->modals[$modal] = false;
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@props([
2+
'title' => 'Title',
3+
'modal' => 'acc-modal',
4+
'size' => 'md',
5+
])
6+
7+
<div x-data="{ open: $wire.entangle('modals.{{ $modal }}') }" style="display: none" x-show.important="open" x-on:keydown.escape.window="open = false">
8+
<div class="modal fade show mt-5" id="{{ $modal }}" tabindex="-1" aria-labelledby="{{ $modal }}-label" aria-hidden="true" wire:ignore.self style="display: block">
9+
<div class="modal-dialog modal-{{ $size }}">
10+
<div class="modal-content">
11+
<div class="modal-header">
12+
<h5 class="modal-title" id="{{ $modal }}-label">{{ $title }}</h5>
13+
<button type="button" class="btn-close text-dark" data-bs-dismiss="modal" aria-label="Close" x-on:click="open = false">
14+
<i class="fas fa-times"></i>
15+
</button>
16+
</div>
17+
<div class="modal-body">
18+
{{ $slot }}
19+
</div>
20+
@if (isset($actions))
21+
<div class="modal-footer">
22+
{{-- <button type="button" class="btn btn-secondary" data-bs-dismiss="acc-modal">Close</button>
23+
<button type="button" class="btn btn-primary">Save</button> --}}
24+
<div class="text-center">
25+
{{ $actions }}
26+
</div>
27+
</div>
28+
@endif
29+
</div>
30+
</div>
31+
</div>
32+
<div class="modal-backdrop fade show"></div>
33+
</div>

resources/views/components/cms/navigation.blade.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function showDropdown($activeRoute = []) {
3535
'label' => 'Managements',
3636
'icon' => 'fa fa-cogs',
3737
'url' => '#',
38-
'active' => ['cms.management.role', 'cms.management.permission'],
38+
'active' => ['cms.management'],
3939
'children' => [
4040
[
4141
'label' => 'Permission',
@@ -49,6 +49,12 @@ function showDropdown($activeRoute = []) {
4949
'url' => route('cms.management.role'),
5050
'active' => ['cms.management.role'],
5151
],
52+
[
53+
'label' => 'User',
54+
'icon' => null,
55+
'url' => route('cms.management.user'),
56+
'active' => ['cms.management.user'],
57+
]
5258
],
5359
],
5460
];
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
use App\Livewire\BaseComponent;
4+
use App\Models\Spatie\Role;
5+
use App\Models\User;
6+
7+
new class extends BaseComponent {
8+
public string $title = 'Create User';
9+
public string $description = 'Create a new user for the system.';
10+
public string $model = User::class;
11+
12+
public function mount() {
13+
$this->canDo('create.' . $this->model);
14+
15+
$this->roles = Role::all();
16+
}
17+
18+
// Define roles for the user creation
19+
public $roles = [];
20+
21+
// Properties for user creation
22+
public string $role;
23+
public string $name;
24+
public string $email;
25+
public string $password;
26+
27+
public function save() {
28+
$this->validate([
29+
'role' => 'required|exists:roles,name',
30+
'name' => 'required|string|max:255',
31+
'email' => 'required|email|max:255|unique:users,email',
32+
'password' => 'required|string|min:8',
33+
]);
34+
35+
// Create a new user with the validated name
36+
$model = User::create($this->all());
37+
$model->syncRoles($this->role);
38+
39+
// Redirect to the user index page after creation
40+
to_route('cms.management.user')->with('success', 'User created successfully.');
41+
}
42+
}; ?>
43+
44+
<div>
45+
<x-acc-back url="{{ route('cms.management.user') }}" />
46+
<div class="row">
47+
<div class="col-12">
48+
<div class="card my-3">
49+
<div class="card-header">
50+
<div class="d-lg-flex">
51+
<div>
52+
<h5 class="mb-0">
53+
{{ $title }}
54+
</h5>
55+
<p class="text-sm mb-0">
56+
{{ $description }}
57+
</p>
58+
</div>
59+
</div>
60+
</div>
61+
<div class="card-body pb-4">
62+
<form wire:submit.prevent="save">
63+
<div class="row">
64+
<div class="col-md-12 mb-3">
65+
<x-acc-input model="name" label="Name" />
66+
</div>
67+
<div class="col-md-6 mb-3">
68+
<x-acc-input type="email" model="email" label="Email" />
69+
</div>
70+
<div class="col-md-6 mb-3">
71+
<x-acc-input type="password" model="password" label="Password" />
72+
</div>
73+
<div class="col-md-12 mb-3">
74+
<label class="form-label fw-bold">
75+
Role
76+
</label>
77+
<x-acc-input type="select" model="role">
78+
<option value="">-- Select Role --</option>
79+
@foreach($roles as $r)
80+
<option value="{{ $r->name }}">{{ $r->name }}</option>
81+
@endforeach
82+
</x-acc-input>
83+
</div>
84+
</div>
85+
<div class="float-start">
86+
<button type="submit" class="btn btn-primary btn-sm">
87+
<i class="fas fa-save me-2"></i>
88+
Save
89+
</button>
90+
<a href="{{ route('cms.management.user') }}" class="btn bg-gradient-dark btn-sm me-2">
91+
<i class="fas fa-arrow-left me-2"></i>
92+
Cancel
93+
</a>
94+
</div>
95+
</form>
96+
</div>
97+
</div>
98+
</div>
99+
</div>
100+
</div>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
use App\Livewire\BaseComponent;
4+
use App\Models\Spatie\Role;
5+
use App\Models\User;
6+
7+
new class extends BaseComponent {
8+
public string $title = 'Edit User';
9+
public string $description = 'Edit an existing user for the system.';
10+
public string $model = User::class;
11+
public $oldData;
12+
13+
public function mount($id) {
14+
$this->canDo('update.' . $this->model);
15+
16+
$this->oldData = User::find($id);
17+
if (!$this->oldData) to_route('cms.management.user')->with('error', 'User not found.');
18+
19+
// Set properties from the old data
20+
$this->name = $this->oldData->name;
21+
$this->email = $this->oldData->email;
22+
$this->role = $this->oldData->getRoleNames()[0];
23+
24+
$this->roles = Role::all();
25+
}
26+
27+
// Define roles for the user creation
28+
public $roles = [];
29+
30+
// Properties for user creation
31+
public string $role;
32+
public string $name;
33+
public string $email;
34+
35+
public function save() {
36+
$this->validate([
37+
'role' => 'required|exists:roles,name',
38+
'name' => 'required|string|max:255',
39+
'email' => 'required|email|max:255|unique:users,email,' . $this->oldData->id,
40+
]);
41+
42+
// Create a new user with the validated name
43+
$this->oldData->syncRoles([$this->role]);
44+
$this->oldData->update($this->all());
45+
46+
// Redirect to the user index page after creation
47+
to_route('cms.management.user')->with('success', 'User updated successfully.');
48+
}
49+
}; ?>
50+
51+
<div>
52+
<x-acc-back url="{{ route('cms.management.user') }}" />
53+
<div class="row">
54+
<div class="col-12">
55+
<div class="card my-3">
56+
<div class="card-header">
57+
<div class="d-lg-flex">
58+
<div>
59+
<h5 class="mb-0">
60+
{{ $title }}
61+
</h5>
62+
<p class="text-sm mb-0">
63+
{{ $description }}
64+
</p>
65+
</div>
66+
</div>
67+
</div>
68+
<div class="card-body pb-4">
69+
<form wire:submit.prevent="save">
70+
<div class="row">
71+
<div class="col-md-12 mb-3">
72+
<x-acc-input model="name" label="Name" :filled="true" />
73+
</div>
74+
<div class="col-md-12 mb-3">
75+
<x-acc-input type="email" model="email" label="Email" :filled="true" />
76+
</div>
77+
<div class="col-md-12 mb-3">
78+
<label class="form-label fw-bold">
79+
Role
80+
</label>
81+
<x-acc-input type="select" model="role">
82+
<option value="">-- Select Role --</option>
83+
@foreach($roles as $r)
84+
<option value="{{ $r->name }}">{{ $r->name }}</option>
85+
@endforeach
86+
</x-acc-input>
87+
</div>
88+
</div>
89+
<div class="float-start">
90+
<button type="submit" class="btn btn-primary btn-sm">
91+
<i class="fas fa-save me-2"></i>
92+
Save
93+
</button>
94+
<a href="{{ route('cms.management.user') }}" class="btn bg-gradient-dark btn-sm me-2">
95+
<i class="fas fa-arrow-left me-2"></i>
96+
Cancel
97+
</a>
98+
</div>
99+
</form>
100+
</div>
101+
</div>
102+
</div>
103+
</div>
104+
</div>

0 commit comments

Comments
 (0)