Skip to content

Commit 1fd9d55

Browse files
Merge pull request #10 from botble/dat/add-filter
Apply more filters to import form
2 parents 5b1e5be + 10fe92d commit 1fd9d55

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

public/js/data-synchronize.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/data-synchronize.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ $(() => {
2727
const $form = $(document).find('[data-bb-toggle="import-form"]')
2828

2929
if ($form.length > 0) {
30+
const formData = new FormData($form.get(0))
3031
const $button = $form.find('button[type="submit"]')
3132
const $errors = $form.find('[data-bb-toggle="import-errors"]')
3233
const $output = $form.find('.data-synchronize-import-output')
3334
let errors = []
3435
let total = null
3536

37+
$form.on('change', (e) => {
38+
formData.set(e.target.name, e.target.value)
39+
})
40+
3641
const output = (message, type) => {
3742
if (type) {
3843
$output.append(`<p class="text-${type}">${message}</p>`)
@@ -67,14 +72,14 @@ $(() => {
6772
}
6873

6974
const importData = (fileName, offset, limit = $form.data('chunk-size'), total = 0) => {
75+
formData.set('file_name', fileName)
76+
formData.set('offset', offset)
77+
formData.set('limit', limit)
78+
formData.set('total', total)
79+
7080
$httpClient
7181
.make()
72-
.post($form.data('import-url'), {
73-
file_name: fileName,
74-
offset,
75-
limit,
76-
total,
77-
})
82+
.post($form.data('import-url'), formData)
7883
.then(({ data }) => {
7984
if (data.data.count > 0) {
8085
output(data.message)
@@ -88,13 +93,13 @@ $(() => {
8893
}
8994

9095
const validate = (fileName, offset, limit = $form.data('chunk-size')) => {
96+
formData.set('file_name', fileName)
97+
formData.set('offset', offset)
98+
formData.set('limit', limit)
99+
91100
$httpClient
92101
.make()
93-
.post($form.data('validate-url'), {
94-
file_name: fileName,
95-
offset,
96-
limit,
97-
})
102+
.post($form.data('validate-url'), formData)
98103
.then(({ data }) => {
99104
if (data.data.errors.length > 0) {
100105
errors = errors.concat(data.data.errors)

resources/views/import.blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class="data-synchronize-import-form"
5555
</x-core::alert>
5656
@endif
5757

58+
{!! apply_filters('data_synchronize_import_form_before', null, $importer) !!}
59+
5860
<div>
5961
<div class="dropzone">
6062
<div class="dz-message">
@@ -67,6 +69,8 @@ class="data-synchronize-import-form"
6769
</x-core::form.helper-text>
6870
</div>
6971

72+
{!! apply_filters('data_synchronize_import_form_after', null, $importer) !!}
73+
7074
<pre class="mt-3 data-synchronize-import-output" style="display: none"></pre>
7175
</x-core::card.body>
7276
<x-core::card.footer>

src/Http/Controllers/ImportController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Botble\DataSynchronize\Http\Requests\ImportRequest;
1010
use Botble\DataSynchronize\Importer\Importer;
1111
use Exception;
12+
use Illuminate\Http\Request;
1213

1314
abstract class ImportController extends BaseController
1415
{
@@ -31,7 +32,7 @@ public function index()
3132
public function validateData(ImportRequest $request)
3233
{
3334
try {
34-
$response = $this->getImporter()->validate(
35+
$response = $this->prepareImporter($request)->validate(
3536
$request->input('file_name'),
3637
$request->input('offset'),
3738
$request->input('limit'),
@@ -76,7 +77,7 @@ public function import(ImportRequest $request)
7677
}
7778

7879
try {
79-
$response = $this->getImporter()->import(
80+
$response = $this->prepareImporter($request)->import(
8081
$request->input('file_name'),
8182
$request->input('offset'),
8283
$request->input('limit'),
@@ -126,4 +127,9 @@ public function downloadExample(DownloadTemplateRequest $request)
126127

127128
return $this->getImporter()->downloadExample($request->input('format'));
128129
}
130+
131+
protected function prepareImporter(Request $request): Importer
132+
{
133+
return $this->getImporter();
134+
}
129135
}

0 commit comments

Comments
 (0)