Skip to content

Commit 6a8e799

Browse files
authored
Restore previously uploaded files (#92)
1 parent 8d57c52 commit 6a8e799

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

resources/dist/js/filepond.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/filepond.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Alpine.data('filepond', (config) => ({
8484

8585
const field = this.$wire.fields[config.field].properties;
8686

87-
FilePond.create(this.$refs.input, {
87+
const filepond = FilePond.create(this.$refs.input, {
8888
allowMultiple: field.multiple,
8989
minFileSize: field.file_size.min ? `${field.file_size.min}KB` : null,
9090
maxFileSize: field.file_size.max ? `${field.file_size.max}KB` : null,
@@ -101,9 +101,29 @@ Alpine.data('filepond', (config) => ({
101101
revert: (filename, load) => {
102102
this.$wire.removeUpload(field.key, filename, load)
103103
},
104+
restore: (tmpFilename, load, error, progress, abort, headers) => {
105+
this.$wire.restoreUpload(tmpFilename)
106+
.then(file => fetch(file.url)
107+
.then(response => response.blob())
108+
.then(blob => new File([blob], file.filename, { type: blob.type }))
109+
)
110+
.then(file => {
111+
progress(true, 0, file.size)
112+
load(file)
113+
})
114+
},
104115
},
105116
...locales[config.locale],
106117
});
118+
119+
// Initialize previously uploaded files. This is necessary when using the form wizard and navigating to a previous step that contains a filepond field.
120+
filepond.files = this.$wire.fields[config.field].value.map(tmpFileId => ({
121+
source: tmpFileId.replace('livewire-file:',''),
122+
options: {
123+
type: 'limbo',
124+
},
125+
}));
126+
107127
},
108128

109129
reset(livewireId) {

src/Livewire/Concerns/HandlesValues.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Aerni\LivewireForms\Livewire\Concerns;
44

55
use Illuminate\Support\Collection;
6-
use Livewire\WithFileUploads;
76

87
trait HandlesValues
98
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Aerni\LivewireForms\Livewire\Concerns;
4+
5+
use Aerni\LivewireForms\Fields\Assets;
6+
use Livewire\WithFileUploads as BaseWithFileUploads;
7+
8+
trait WithFileUploads
9+
{
10+
use BaseWithFileUploads;
11+
12+
public function restoreUpload(string $tmpFilename): array
13+
{
14+
$file = $this->fields->whereInstanceOf(Assets::class)
15+
->flatMap(fn ($field) => $field->value())
16+
->first(fn ($field) => $tmpFilename === $field->getFilename());
17+
18+
return [
19+
'url' => $file->temporaryUrl(),
20+
'filename' => $file->getClientOriginalName(),
21+
];
22+
}
23+
}

0 commit comments

Comments
 (0)