Skip to content

Commit

Permalink
Use Livewire methods to set & retreive properties
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Oct 7, 2024
1 parent 3a60d03 commit 96dfc30
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/WithFilePond.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use Livewire\LivewireManager;
use Livewire\WithFileUploads;

trait WithFilePond
Expand All @@ -14,32 +15,50 @@ trait WithFilePond
public function remove($property, $filename): void
{
$file = Str::after($filename, config('app.url'));
$this->$property = [];

app(LivewireManager::class)->updateProperty(
$this,
$property,
is_array($this->getPropertyValue($property))
? []
: null,
);

File::delete(public_path($file));
}

public function revert($property, $filename): void
{
if (! $this->$property) {
if (! $this->hasProperty($property)) {
return;
}

if (! is_array($this->$property)) {
$this->$property->delete();
$uploads = $this->getPropertyValue($property);

if (! is_array($uploads)) {
if ($uploads instanceof TemporaryUploadedFile && $uploads->getFilename() === $filename) {
$uploads->delete();
app(LivewireManager::class)->updateProperty($this, $property, null);
}

return;
}

$this->$property = array_filter($this->$property, function (TemporaryUploadedFile $file) use ($filename) {
if ($file->getFilename() !== $filename) {
return true;
}
$newFiles = collect($uploads)
->filter(function ($upload) use ($filename) {
if (! $upload instanceof TemporaryUploadedFile) {
return false;
}

if ($upload->getFilename() === $filename) {
$upload->delete();
return false;
}

$file->delete();
return true;
})->values()->toArray();

return false;
});
app(LivewireManager::class)->updateProperty($this, $property, $newFiles);
}

public function resetFilePond(string $property): void
Expand Down

0 comments on commit 96dfc30

Please sign in to comment.