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
public $import_errors; //
public ?Import $activeFile = null;
public $activeFileId;
public $headerRow = [];
public $typeOfImport;
public $importTypes;
@ -30,6 +30,7 @@ class Importer extends Component
public $send_welcome;
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
// @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?
// 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.*.created_at' => 'required|string',
'files.*.filesize' => 'required|integer',
'activeFile' => 'Import',
'activeFile.import_type' => 'string',
'activeFile.field_map' => 'array',
'activeFile.header_row' => 'array',
'headerRow' => 'array',
'typeOfImport' => 'string',
'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
foreach($this->importTypes AS $type => $name) {
foreach ($this->importTypes as $type => $name) {
$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)
{
$this->clearMessage();
$this->activeFile = Import::find($id);
$this->activeFileId = $id;
if (!$this->activeFile) {
$this->message = trans('admin/hardware/message.import.file_missing');
@ -560,6 +554,16 @@ class Importer extends Component
return Import::orderBy('id', 'desc')->get();
}
#[Computed]
public function activeFile()
{
if ($this->activeFileId) {
return Import::find($this->activeFileId);
}
return null;
}
public function render()
{
return view('livewire.importer')

View file

@ -120,7 +120,7 @@
@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-3">{{ Helper::getFormattedDateObject($currentFile->created_at, 'datetime', false) }}</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>
<span class="sr-only">{{ trans('general.import') }}</span>
</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 }})">
<i class="fas fa-trash icon-white" aria-hidden="true"></i><span class="sr-only"></span></button>
</a>
</td>
</tr>
@if( $currentFile && $activeFile && ($currentFile->id == $activeFile->id))
@if( $currentFile && $this->activeFile && ($currentFile->id == $this->activeFile->id))
<tr class="warning">
<td colspan="4">
@ -233,9 +233,9 @@
])
}}
</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">
<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>
@else
@php
@ -251,7 +251,7 @@
<div class="form-group col-md-12">
<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 class="col-md-9">
<button type="submit" class="btn btn-primary col-md-5" id="import">Import</button>
@ -267,7 +267,7 @@
@else
<div class="form-group col-md-10">
<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>
@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;