Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are you planning to add a non-livewire version for this? #55

Open
OzanKurt opened this issue Mar 12, 2024 · 3 comments
Open

Are you planning to add a non-livewire version for this? #55

OzanKurt opened this issue Mar 12, 2024 · 3 comments

Comments

@OzanKurt
Copy link

This would be extremely useful for me, in case you can implement it partially I could also continue.

@yajra
Copy link
Owner

yajra commented Mar 14, 2024

No plans but open for a PR if you can. Thanks!

@OzanKurt
Copy link
Author

So...

I've been playing around with the export package and actually found out that integrating it with buttons is extremely simple.

I added this custom button which sends an ajax request to the server side and it gets handled by the exportQueue method from WithExportQueue.

    DataTable.ext.buttons.exportQueue = {
        className: 'buttons-export-queue',
        text: function (dt) {
            return '<i class="far fa-fw fa-file-excel"></i>';
        },
        action: function (e, dt, button, config) {
            var url = _buildUrl(dt, 'exportQueue');

            $.ajax({
                url: url,
                type: 'GET',
                data: {},
                complete: window.ajax_complete_handler,
            });
        }
    };

There are a couple of ideas I've had:

1- We need to add a customizable callback function to the exportQueue() method so that I could return a custom JSON response instead of just the $batch->id

    public function exportQueue(): string
    {
        $job = new DataTableExportJob(
            [self::class, $this->attributes],
            request()->all(),
            Auth::id() ?? 0,
            $this->sheetName(),
        );

        $batch = Bus::batch([$job])->name('datatables-export')->dispatch();

        return $batch->id;
    }

2- I had to implement a getUser() method inside the DataTableExportJob to get the User instance from the id:

    public function getUser(): ?User
    {
        return $this->user ? User::find($this->user) : null;
    }

3- We need to be able to handle the errors that might occur during the Job:
- We can check if the excel creation failed in a try/catch block.

4- I think the mail should be separated from the Job
- Maybe add a new configuration key "mail_type" => "send" | "queue", so that the mail could also be queued independently from the job.
- This would allow us to customize the exportQueue() and add a callback before we send the email, or maybe even listen for the completion on client side and trigger download (without needing an email).

Maybe something like this:

    public function afterExport(array $paths): bool
    {
        // The `$paths` can contain multiple paths of the file like:
        // ['local' => 'localpath', 's3' => 's3path', 'otherDisk' => 'otherpath']

        // do some stuff with the exported file, maybe save it to an archive inside the media-library.
        // return `true` to continue with the email
        // return `false` to skip email, and maybe send the file to every user in a team
    }

5- I don't think we need a job_batch for this since it's only 1 job, we should be able to dispatch the single job alone. (I don't have much exp. with batches, so no judgement 😞)

6- Ability to add multiple destination disks

7- Ability to send the job to multiple users (this can be done manually if we implement "step 4")

9- Customizable mail_subject

10- A nicer Email Template from Laravels defaults 😄

Let me know what you think about these ideas.

I also believe that this package could be integrated inside the buttons package directly, only the livewire parts will only be registered if livewire is installed.

So, inside the ExportServiceProvider we can add something like this:

if (class_exists('Livewire\\Livewire')) {
    // Do livewire related inits
}

@yajra
Copy link
Owner

yajra commented Apr 17, 2024

Good points provided @OzanKurt. Will also try to implement it if I can.

5: the batch job is needed to check the status for real-time response.

10: agree, we haven't used this feature so no enhancements were done yet

The package idea came from Laravel Daily

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants