2024-08-07 19:00:56 -05:00
|
|
|
<?php
|
|
|
|
|
|
2024-08-07 19:02:58 -05:00
|
|
|
use Livewire\WithFileUploads;
|
2024-08-07 19:00:56 -05:00
|
|
|
use Livewire\Volt\Component;
|
|
|
|
|
use Mary\Traits\Toast;
|
2024-08-27 22:41:13 -05:00
|
|
|
use App\Imports\BackupImport;
|
2024-08-29 23:36:44 -05:00
|
|
|
use App\Exports\BackupExport;
|
2024-08-27 22:49:01 -05:00
|
|
|
use Livewire\Attributes\Rule;
|
2024-10-23 16:08:58 -05:00
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
2024-08-07 19:00:56 -05:00
|
|
|
|
|
|
|
|
new class extends Component {
|
|
|
|
|
use Toast;
|
2024-08-07 19:02:58 -05:00
|
|
|
use WithFileUploads;
|
2024-08-07 19:00:56 -05:00
|
|
|
|
|
|
|
|
// props
|
2024-10-23 16:08:58 -05:00
|
|
|
#[Rule('required|extensions:xlsx|mimes:xlsx|max:2048')]
|
2024-08-07 19:00:56 -05:00
|
|
|
public $file;
|
|
|
|
|
|
|
|
|
|
// methods
|
2024-08-27 22:41:13 -05:00
|
|
|
public function import()
|
2024-08-07 19:00:56 -05:00
|
|
|
{
|
2024-08-27 22:49:01 -05:00
|
|
|
$this->validate();
|
|
|
|
|
|
2024-09-19 21:26:29 -05:00
|
|
|
if (!RateLimiter::attempt('import:'.auth()->user()->id, $perMinute = 3, fn()=>null)) {
|
|
|
|
|
|
|
|
|
|
$this->error(__('Hang on! You\'re doing that too much.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 21:39:59 -05:00
|
|
|
try {
|
|
|
|
|
|
2024-10-23 16:08:58 -05:00
|
|
|
$import = Excel::import(
|
|
|
|
|
new BackupImport,
|
|
|
|
|
$this->file->getPathname(),
|
|
|
|
|
config('livewire.temporary_file_upload.disk', null)
|
|
|
|
|
);
|
2024-08-29 21:39:59 -05:00
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
2024-08-29 23:36:44 -05:00
|
|
|
|
2024-08-29 21:39:59 -05:00
|
|
|
return $this->error($e->getMessage());
|
|
|
|
|
}
|
2024-08-27 22:41:13 -05:00
|
|
|
|
2024-08-30 20:22:28 -05:00
|
|
|
$this->success(__('Successfully imported!'), redirectTo: route('dashboard'));
|
2024-08-29 23:36:44 -05:00
|
|
|
}
|
2024-08-27 22:41:13 -05:00
|
|
|
|
2024-08-29 23:36:44 -05:00
|
|
|
public function downloadTemplate()
|
|
|
|
|
{
|
|
|
|
|
return Excel::download(new BackupExport(empty: true), now()->format('Y_m_d') . '_investbrain_template.xlsx');
|
2024-08-07 19:00:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}; ?>
|
|
|
|
|
|
2024-08-27 22:41:13 -05:00
|
|
|
<x-forms.form-section submit="import">
|
|
|
|
|
<x-slot name="title">
|
|
|
|
|
{{ __('Import') }}
|
|
|
|
|
</x-slot>
|
|
|
|
|
|
|
|
|
|
<x-slot name="description">
|
2024-08-29 23:36:44 -05:00
|
|
|
{{ __('Upload or recover your Investbrain portfolio and holdings.') }}
|
2024-10-18 21:21:20 -05:00
|
|
|
<strong><a href="#" title="{{ __('Click to download import template.') }}" @click="$wire.downloadTemplate()"> {{ __('Download import template.') }}</a></strong>
|
2024-08-27 22:41:13 -05:00
|
|
|
</x-slot>
|
|
|
|
|
|
|
|
|
|
<x-slot name="form">
|
|
|
|
|
|
|
|
|
|
<div class="col-span-6 sm:col-span-4">
|
|
|
|
|
<x-file wire:model="file" label="{{ __('Select a file') }}" hint="" accept=".xlsx" required />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</x-slot>
|
|
|
|
|
|
|
|
|
|
<x-slot name="actions">
|
2024-08-30 20:22:28 -05:00
|
|
|
|
|
|
|
|
<x-forms.action-message class="me-3" on="saved">
|
|
|
|
|
{{ __('Saved.') }}
|
|
|
|
|
</x-forms.action-message>
|
2024-08-27 22:41:13 -05:00
|
|
|
|
2024-10-23 16:08:58 -05:00
|
|
|
<x-button type="submit" wire:loading.attr="disabled" spinner="import">
|
2024-08-27 22:41:13 -05:00
|
|
|
{{ __('Import') }}
|
|
|
|
|
</x-button>
|
|
|
|
|
</x-slot>
|
|
|
|
|
</x-forms.form-section>
|
|
|
|
|
|
|
|
|
|
|