Make $activeFile a computed property

This commit is contained in:
Marcus Moore 2024-07-16 17:03:42 -07:00
parent 7e89b58746
commit eba494ad8c
No known key found for this signature in database
2 changed files with 39 additions and 35 deletions

View file

@ -19,7 +19,7 @@ class Importer extends Component
//originally from ImporterFile //originally from ImporterFile
public $import_errors; // public $import_errors; //
public ?Import $activeFile = null; public $activeFileId;
public $headerRow = []; public $headerRow = [];
public $typeOfImport; public $typeOfImport;
public $importTypes; public $importTypes;
@ -30,6 +30,7 @@ class Importer extends Component
public $send_welcome; public $send_welcome;
public $run_backup; public $run_backup;
public $field_map; // we need a separate variable for the field-mapping, because the keys in the normal array are too complicated for Livewire to understand public $field_map; // we need a separate variable for the field-mapping, because the keys in the normal array are too complicated for Livewire to understand
// @todo: remove the need for this by using $activeFileId
public $file_id; // TODO: I can't figure out *why* we need this, but it really seems like we do. I can't seem to pull the id from the activeFile for some reason? public $file_id; // TODO: I can't figure out *why* we need this, but it really seems like we do. I can't seem to pull the id from the activeFile for some reason?
// Make these variables public - we set the properties in the constructor so we can localize them (versus the old static arrays) // Make these variables public - we set the properties in the constructor so we can localize them (versus the old static arrays)
@ -46,10 +47,6 @@ class Importer extends Component
'files.*.file_path' => 'required|string', 'files.*.file_path' => 'required|string',
'files.*.created_at' => 'required|string', 'files.*.created_at' => 'required|string',
'files.*.filesize' => 'required|integer', 'files.*.filesize' => 'required|integer',
'activeFile' => 'Import',
'activeFile.import_type' => 'string',
'activeFile.field_map' => 'array',
'activeFile.header_row' => 'array',
'headerRow' => 'array', 'headerRow' => 'array',
'typeOfImport' => 'string', 'typeOfImport' => 'string',
'field_map' => 'array' 'field_map' => 'array'
@ -489,19 +486,16 @@ class Importer extends Component
]; ];
$this->columnOptions[''] = $this->getColumns(''); //blank mode? I don't know what this is supposed to mean $this->columnOptions[''] = $this->getColumns(''); //blank mode? I don't know what this is supposed to mean
foreach($this->importTypes AS $type => $name) { foreach ($this->importTypes as $type => $name) {
$this->columnOptions[$type] = $this->getColumns($type); $this->columnOptions[$type] = $this->getColumns($type);
} }
if ($this->activeFile) {
$this->field_map = $this->activeFile->field_map ? array_values($this->activeFile->field_map) : [];
}
} }
public function selectFile($id) public function selectFile($id)
{ {
$this->clearMessage(); $this->clearMessage();
$this->activeFile = Import::find($id); $this->activeFileId = $id;
if (!$this->activeFile) { if (!$this->activeFile) {
$this->message = trans('admin/hardware/message.import.file_missing'); $this->message = trans('admin/hardware/message.import.file_missing');
@ -515,7 +509,7 @@ class Importer extends Component
$this->field_map = null; $this->field_map = null;
foreach ($this->headerRow as $element) { foreach ($this->headerRow as $element) {
if(isset($this->activeFile->field_map[$element])) { if (isset($this->activeFile->field_map[$element])) {
$this->field_map[] = $this->activeFile->field_map[$element]; $this->field_map[] = $this->activeFile->field_map[$element];
} else { } else {
$this->field_map[] = null; // re-inject the 'nulls' if a file was imported with some 'Do Not Import' settings $this->field_map[] = null; // re-inject the 'nulls' if a file was imported with some 'Do Not Import' settings
@ -530,9 +524,9 @@ class Importer extends Component
public function destroy($id) public function destroy($id)
{ {
// TODO: why don't we just do File::find($id)? This seems dumb. // TODO: why don't we just do File::find($id)? This seems dumb.
foreach($this->files as $file) { foreach ($this->files as $file) {
if ($id == $file->id) { if ($id == $file->id) {
if (Storage::delete('private_uploads/imports/'.$file->file_path)) { if (Storage::delete('private_uploads/imports/' . $file->file_path)) {
$file->delete(); $file->delete();
$this->message = trans('admin/hardware/message.import.file_delete_success'); $this->message = trans('admin/hardware/message.import.file_delete_success');
@ -560,6 +554,16 @@ class Importer extends Component
return Import::orderBy('id', 'desc')->get(); return Import::orderBy('id', 'desc')->get();
} }
#[Computed]
public function activeFile()
{
if ($this->activeFileId) {
return Import::find($this->activeFileId);
}
return null;
}
public function render() public function render()
{ {
return view('livewire.importer') return view('livewire.importer')

View file

@ -120,7 +120,7 @@
@foreach($this->files as $currentFile) @foreach($this->files as $currentFile)
<tr style="{{ ($activeFile && ($currentFile->id == $activeFile->id)) ? 'font-weight: bold' : '' }}" class="{{ ($activeFile && ($currentFile->id == $activeFile->id)) ? 'warning' : '' }}"> <tr style="{{ ($this->activeFile && ($currentFile->id == $this->activeFile->id)) ? 'font-weight: bold' : '' }}" class="{{ ($this->activeFile && ($currentFile->id == $this->activeFile->id)) ? 'warning' : '' }}">
<td class="col-md-6">{{ $currentFile->file_path }}</td> <td class="col-md-6">{{ $currentFile->file_path }}</td>
<td class="col-md-3">{{ Helper::getFormattedDateObject($currentFile->created_at, 'datetime', false) }}</td> <td class="col-md-3">{{ Helper::getFormattedDateObject($currentFile->created_at, 'datetime', false) }}</td>
<td class="col-md-1">{{ Helper::formatFilesizeUnits($currentFile->filesize) }}</td> <td class="col-md-1">{{ Helper::formatFilesizeUnits($currentFile->filesize) }}</td>
@ -129,14 +129,14 @@
<i class="fa-solid fa-list-check" aria-hidden="true"></i> <i class="fa-solid fa-list-check" aria-hidden="true"></i>
<span class="sr-only">{{ trans('general.import') }}</span> <span class="sr-only">{{ trans('general.import') }}</span>
</button> </button>
<a href="#" wire:click.prevent="$set('activeFile',null)"> <a href="#" wire:click.prevent="$set('activeFileId',null)">
<button class="btn btn-sm btn-danger" wire:click="destroy({{ $currentFile->id }})"> <button class="btn btn-sm btn-danger" wire:click="destroy({{ $currentFile->id }})">
<i class="fas fa-trash icon-white" aria-hidden="true"></i><span class="sr-only"></span></button> <i class="fas fa-trash icon-white" aria-hidden="true"></i><span class="sr-only"></span></button>
</a> </a>
</td> </td>
</tr> </tr>
@if( $currentFile && $activeFile && ($currentFile->id == $activeFile->id)) @if( $currentFile && $this->activeFile && ($currentFile->id == $this->activeFile->id))
<tr class="warning"> <tr class="warning">
<td colspan="4"> <td colspan="4">
@ -233,9 +233,9 @@
]) ])
}} }}
</div> </div>
@if (($activeFile->first_row) && (array_key_exists($index, $activeFile->first_row))) @if (($this->activeFile->first_row) && (array_key_exists($index, $this->activeFile->first_row)))
<div class="col-md-5"> <div class="col-md-5">
<p class="form-control-static">{{ str_limit($activeFile->first_row[$index], 50, '...') }}</p> <p class="form-control-static">{{ str_limit($this->activeFile->first_row[$index], 50, '...') }}</p>
</div> </div>
@else @else
@php @php
@ -251,7 +251,7 @@
<div class="form-group col-md-12"> <div class="form-group col-md-12">
<div class="col-md-3 text-left"> <div class="col-md-3 text-left">
<a href="#" wire:click.prevent="$set('activeFile',null)">{{ trans('general.cancel') }}</a> <a href="#" wire:click.prevent="$set('activeFileId',null)">{{ trans('general.cancel') }}</a>
</div> </div>
<div class="col-md-9"> <div class="col-md-9">
<button type="submit" class="btn btn-primary col-md-5" id="import">Import</button> <button type="submit" class="btn btn-primary col-md-5" id="import">Import</button>
@ -267,7 +267,7 @@
@else @else
<div class="form-group col-md-10"> <div class="form-group col-md-10">
<div class="col-md-3 text-left"> <div class="col-md-3 text-left">
<a href="#" wire:click.prevent="$set('activeFile',null)">{{ trans('general.cancel') }}</a> <a href="#" wire:click.prevent="$set('activeFileId',null)">{{ trans('general.cancel') }}</a>
</div> </div>
</div> </div>
@endif {{-- end of if ... $typeOfImport --}} @endif {{-- end of if ... $typeOfImport --}}
@ -388,7 +388,7 @@
} }
} }
$wire.$set('activeFile', null); //$wire.$set('hideDetails') $wire.$set('activeFileId', null); //$wire.$set('hideDetails')
}); });
}) })
return false; return false;