Skip to content

Commit f0959ed

Browse files
kresnasatyanunomadurotaylorotwell
authored
Fix Editing Chirper and Add Confirm Delete Chirper in Livewire (#54)
* fix editing chirps in livewire section * add confirmation when delete a chirp * Reverts change on method name * Reverts change on method name * Uses `disableEdit` * Uses `disableEdit` * formatting --------- Co-authored-by: Nuno Maduro <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 67eb995 commit f0959ed

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

resources/docs/livewire/deleting-chirps.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ new class extends Component
3232
}
3333

3434
#[On('chirp-created')]
35-
#[On('chirp-updated')]
3635
public function getChirps(): void
3736
{
38-
$this->editing = null;
39-
4037
$this->chirps = Chirp::with('user')
4138
->latest()
4239
->get();
@@ -45,12 +42,17 @@ new class extends Component
4542
public function edit(Chirp $chirp): void
4643
{
4744
$this->editing = $chirp;
45+
46+
$this->getChirps();
4847
}
4948

5049
#[On('chirp-edit-canceled')]
51-
public function cancelEdit(): void
50+
#[On('chirp-updated')]
51+
public function disableEditing(): void
5252
{
5353
$this->editing = null;
54+
55+
$this->getChirps();
5456
} // [tl! collapse:end]
5557
// [tl! add:start]
5658
public function delete(Chirp $chirp): void
@@ -91,7 +93,7 @@ new class extends Component
9193
<x-dropdown-link wire:click="edit({{ $chirp->id }})">
9294
{{ __('Edit') }}
9395
</x-dropdown-link>
94-
<x-dropdown-link wire:click="delete({{ $chirp->id }})"> <!-- [tl! add:start] -->
96+
<x-dropdown-link wire:click="delete({{ $chirp->id }})" wire:confirm="Are you sure to delete this chirp?"> <!-- [tl! add:start] -->
9597
{{ __('Delete') }}
9698
</x-dropdown-link> <!-- [tl! add:end] -->
9799
</x-slot>
@@ -116,21 +118,27 @@ new class extends Component
116118
use App\Models\Chirp;
117119
use function Livewire\Volt\{on, state};
118120

119-
$getChirps = function () {
121+
$getChirps = fn () => $this->chirps = Chirp::with('user')->latest()->get();
122+
123+
$disableEditing = function () {
120124
$this->editing = null;
121125

122-
return $this->chirps = Chirp::with('user')->latest()->get();
126+
return $this->getChirps();
123127
};
124128

125129
state(['chirps' => $getChirps, 'editing' => null]);
126130

127131
on([
128132
'chirp-created' => $getChirps,
129-
'chirp-updated' => $getChirps,
130-
'chirp-edit-canceled' => fn () => $this->editing = null,
133+
'chirp-updated' => $disableEditing,
134+
'chirp-edit-canceled' => $disableEditing,
131135
]);
132136

133-
$edit = fn (Chirp $chirp) => $this->editing = $chirp; // [tl! collapse:end]
137+
$edit = function (Chirp $chirp) {
138+
$this->editing = $chirp;
139+
140+
$this->getChirps();
141+
}; // [tl! collapse:end]
134142
// [tl! add:start]
135143
$delete = function (Chirp $chirp) {
136144
$this->authorize('delete', $chirp);
@@ -170,7 +178,7 @@ $delete = function (Chirp $chirp) {
170178
<x-dropdown-link wire:click="edit({{ $chirp->id }})">
171179
{{ __('Edit') }}
172180
</x-dropdown-link>
173-
<x-dropdown-link wire:click="delete({{ $chirp->id }})"> <!-- [tl! add:start] -->
181+
<x-dropdown-link wire:click="delete({{ $chirp->id }})" wire:confirm="Are you sure to delete this chirp?"> <!-- [tl! add:start] -->
174182
{{ __('Delete') }}
175183
</x-dropdown-link> <!-- [tl! add:end] -->
176184
</x-slot>

resources/docs/livewire/editing-chirps.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ new class extends Component
4242
public function edit(Chirp $chirp): void
4343
{
4444
$this->editing = $chirp;
45+
46+
$this->getChirps();
4547
} // [tl! add:end]
4648
}; ?>
4749

@@ -102,7 +104,11 @@ state(['chirps' => $getChirps, 'editing' => null]); // [tl! add]
102104

103105
on(['chirp-created' => $getChirps]);
104106
// [tl! add:start]
105-
$edit = fn (Chirp $chirp) => $this->editing = $chirp; // [tl! add:end]
107+
$edit = function (Chirp $chirp) {
108+
$this->editing = $chirp;
109+
110+
$this->getChirps();
111+
}; // [tl! add:end]
106112

107113
?>
108114

@@ -284,11 +290,8 @@ new class extends Component
284290
}
285291
// [tl! collapse:end]
286292
#[On('chirp-created')]
287-
#[On('chirp-updated')] // [tl! add]
288293
public function getChirps(): void
289294
{
290-
$this->editing = null; // [tl! add:start]
291-
// [tl! add:end]
292295
$this->chirps = Chirp::with('user')
293296
->latest()
294297
->get();
@@ -297,12 +300,17 @@ new class extends Component
297300
public function edit(Chirp $chirp): void
298301
{
299302
$this->editing = $chirp;
303+
304+
$this->getChirps();
300305
}
301306
// [tl! add:start]
302307
#[On('chirp-edit-canceled')]
303-
public function cancelEdit(): void
308+
#[On('chirp-updated')] // [tl! add]
309+
public function disableEditing(): void
304310
{
305311
$this->editing = null;
312+
313+
$this->getChirps();
306314
} // [tl! add:end]
307315
}; ?>
308316

@@ -357,23 +365,28 @@ new class extends Component
357365
use App\Models\Chirp;
358366
use function Livewire\Volt\{on, state};
359367

360-
$getChirps = fn () => $this->chirps = Chirp::with('user')->latest()->get(); // [tl! remove]
361-
$getChirps = function () { // [tl! add:start]
368+
$getChirps = fn () => $this->chirps = Chirp::with('user')->latest()->get();
369+
370+
$disableEditing = function () { // [tl! add:start]
362371
$this->editing = null;
363372

364-
return $this->chirps = Chirp::with('user')->latest()->get();
373+
return $this->getChirps();
365374
}; // [tl! add:end]
366375

367376
state(['chirps' => $getChirps, 'editing' => null]);
368377

369378
on(['chirp-created' => $getChirps]); // [tl! remove]
370379
on([ // [tl! add:start]
371380
'chirp-created' => $getChirps,
372-
'chirp-updated' => $getChirps,
373-
'chirp-edit-canceled' => fn () => $this->editing = null,
381+
'chirp-updated' => $disableEditing,
382+
'chirp-edit-canceled' => $disableEditing,
374383
]); // [tl! add:end]
375384

376-
$edit = fn (Chirp $chirp) => $this->editing = $chirp;
385+
$edit = function (Chirp $chirp) {
386+
$this->editing = $chirp;
387+
388+
$this->getChirps();
389+
};
377390

378391
?>
379392

0 commit comments

Comments
 (0)