Skip to content

Commit

Permalink
Merge pull request livewire#1119 from acurrieclark/fix-remove-file-up…
Browse files Browse the repository at this point in the history
…load-bug

[Fix]: File Uploads - Removing first value in the array no longer res…
  • Loading branch information
calebporzio authored Jun 22, 2020
2 parents c62f200 + d70832d commit 5b3db24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/WithFileUploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ protected function dehydratePropertyFromWithFileUploads($name, $value)
{
if ($value instanceof TemporaryUploadedFile) {
return $value->serializeForLivewireResponse();
} elseif (is_array($value) && isset($value[0]) && $value[0] instanceof TemporaryUploadedFile) {
return $value[0]::serializeMultipleForLivewireResponse($value);
} elseif (is_array($value) && isset(array_values($value)[0]) && array_values($value)[0] instanceof TemporaryUploadedFile) {
return array_values($value)[0]::serializeMultipleForLivewireResponse($value);
}

return $value;
Expand Down
24 changes: 24 additions & 0 deletions tests/FileUploadsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,26 @@ public function can_preview_a_temporary_files_with_a_temporary_signed_url_from_s

$this->assertEquals($file->get(), $rawFileContents);
}

/** @test */
public function removing_first_item_from_array_of_temporary_uploaded_files_serializes_correctly()
{
$file1 = UploadedFile::fake()->image('avatar1.jpg');
$file2 = UploadedFile::fake()->image('avatar2.jpg');
$file3 = UploadedFile::fake()->image('avatar3.jpg');
$file4 = UploadedFile::fake()->image('avatar4.jpg');

$component = Livewire::test(FileUploadComponent::class)
->set('photos', [$file1, $file2, $file3, $file4]);

$this->assertStringStartsWith('livewire-files:', $component->get('photos'));

$component->call('removePhoto', 3);
$this->assertStringStartsWith('livewire-files:', $component->get('photos'));

$component->call('removePhoto', 0);
$this->assertStringStartsWith('livewire-files:', $component->get('photos'));
}
}

class DummyMiddleware
Expand Down Expand Up @@ -538,5 +558,9 @@ public function validateUploadWithDimensions()
]);
}

public function removePhoto($key) {
unset($this->photos[$key]);
}

public function render() { return app('view')->make('null-view'); }
}

0 comments on commit 5b3db24

Please sign in to comment.