WIP: cleaning up extraneous gunk and trying to standardize on one single Livewire Select2 implementation

This commit is contained in:
Brady Wetherington 2022-12-20 12:33:18 -08:00
parent fb201e61f3
commit 53db266b69
2 changed files with 166 additions and 174 deletions

View file

@ -8,76 +8,6 @@ use App\Models\CustomField;
use Log;
global $general, $accessories, $assets, $consumables, $licenses, $users;
$general = [
'category' => 'Category',
'company' => 'Company',
'email' => 'Email',
'item_name' => 'Item Name',
'location' => 'Location',
'maintained' => 'Maintained',
'manufacturer' => 'Manufacturer',
'notes' => 'Notes',
'order_number' => 'Order Number',
'purchase_cost' => 'Purchase Cost',
'purchase_date' => 'Purchase Date',
'quantity' => 'Quantity',
'requestable' => 'Requestable',
'serial' => 'Serial Number',
'supplier' => 'Supplier',
'username' => 'Username',
'department' => 'Department',
];
$accessories = [
'model_number' => 'Model Number',
];
$assets = [
'asset_tag' => 'Asset Tag',
'asset_model' => 'Model Name',
'checkout_class' => 'Checkout Type',
'checkout_location' => 'Checkout Location',
'image' => 'Image Filename',
'model_number' => 'Model Number',
'full_name' => 'Full Name',
'status' => 'Status',
'warranty_months' => 'Warranty Months',
];
$consumables = [
'item_no' => "Item Number",
'model_number' => "Model Number",
'min_amt' => "Minimum Quantity",
];
$licenses = [
'asset_tag' => 'Assigned To Asset',
'expiration_date' => 'Expiration Date',
'full_name' => 'Full Name',
'license_email' => 'Licensed To Email',
'license_name' => 'Licensed To Name',
'purchase_order' => 'Purchase Order',
'reassignable' => 'Reassignable',
'seats' => 'Seats',
];
$users = [
'employee_num' => 'Employee Number',
'first_name' => 'First Name',
'jobtitle' => 'Job Title',
'last_name' => 'Last Name',
'phone_number' => 'Phone Number',
'manager_first_name' => 'Manager First Name',
'manager_last_name' => 'Manager Last Name',
'activated' => 'Activated',
'address' => 'Address',
'city' => 'City',
'state' => 'State',
'country' => 'Country',
];
class ImporterFile extends Component
{
public $activeFile; //this gets automatically populated on instantiation
@ -109,10 +39,76 @@ class ImporterFile extends Component
return json_encode($tmp);
}
static $general = [
'category' => 'Category',
'company' => 'Company',
'email' => 'Email',
'item_name' => 'Item Name',
'location' => 'Location',
'maintained' => 'Maintained',
'manufacturer' => 'Manufacturer',
'notes' => 'Notes',
'order_number' => 'Order Number',
'purchase_cost' => 'Purchase Cost',
'purchase_date' => 'Purchase Date',
'quantity' => 'Quantity',
'requestable' => 'Requestable',
'serial' => 'Serial Number',
'supplier' => 'Supplier',
'username' => 'Username',
'department' => 'Department',
];
static $accessories = [
'model_number' => 'Model Number',
];
static $assets = [
'asset_tag' => 'Asset Tag',
'asset_model' => 'Model Name',
'checkout_class' => 'Checkout Type',
'checkout_location' => 'Checkout Location',
'image' => 'Image Filename',
'model_number' => 'Model Number',
'full_name' => 'Full Name',
'status' => 'Status',
'warranty_months' => 'Warranty Months',
];
static $consumables = [
'item_no' => "Item Number",
'model_number' => "Model Number",
'min_amt' => "Minimum Quantity",
];
static $licenses = [
'asset_tag' => 'Assigned To Asset',
'expiration_date' => 'Expiration Date',
'full_name' => 'Full Name',
'license_email' => 'Licensed To Email',
'license_name' => 'Licensed To Name',
'purchase_order' => 'Purchase Order',
'reassignable' => 'Reassignable',
'seats' => 'Seats',
];
static $users = [
'employee_num' => 'Employee Number',
'first_name' => 'First Name',
'jobtitle' => 'Job Title',
'last_name' => 'Last Name',
'phone_number' => 'Phone Number',
'manager_first_name' => 'Manager First Name',
'manager_last_name' => 'Manager Last Name',
'activated' => 'Activated',
'address' => 'Address',
'city' => 'City',
'state' => 'State',
'country' => 'Country',
];
private function getColumns($type) //maybe static?
{
global $general, $accessories, $assets, $consumables, $licenses, $users; // TODO - why is this global?
$customFields = [];
foreach($this->customFields AS $field) {
$customFields[$field->id] = $field->name;
@ -120,22 +116,22 @@ class ImporterFile extends Component
switch($type) {
case 'asset':
$results = $general + $assets + $customFields;
$results = self::$general + self::$assets + $customFields;
break;
case 'accessory':
$results = $general + $accessories;
$results = self::$general + self::$accessories;
break;
case 'consumable':
$results = $general + $consumables;
$results = self::$general + self::$consumables;
break;
case 'license':
$results = $general + $licenses;
$results = self::$general + self::$licenses;
break;
case 'user':
$results = $general + $users;
$results = self::$general + self::$users;
break;
default:
$results = $general;
$results = self::$general;
}
asort($results); // FIXME - this isn't sorting right yet.
return $results;
@ -166,6 +162,87 @@ class ImporterFile extends Component
public function postSave()
{
if (!$this->activeFile->import_type) {
Log::info("didn't find an import type :(");
$this->statusType ='error';
$this->statusText = "An import type is required... "; // TODO - translate me!
return false;
}
$this->statusType = 'pending';
$this->statusText = "Processing...";
axios.post('{{ route('api.imports.importFile', $activeFile->id) }}', {
'import-update': !!@this.update,
'send-welcome': !!@this.send_welcome,
'import-type': @this.activeFile.import_type,
'run-backup': !!@this.run_backup,
'column-mappings': mappings // FIXME - terrible name
}).then( (body) => {
Log::warn("success!!!")
// Success
$this->statusType = "success";
$this->statusText = "Success... Redirecting.";
// FIXME - can we 'flash' an update here?
window.location.href = body.data.messages.redirect_url; // definite fixme here!
}, (body) => {
// Failure
console.warn("failure!!!!")
if(body.response.data.status == 'import-errors') {
//window.eventHub.$emit('importErrors', body.messages);
console.warn("import error")
console.dir(body)
@this.set('statusType','error');
@this.emit('importError', body.response.data.messages)
//@this.set('statusText', "Error: "+body.response.data.messages.join("<br>"));
} else {
console.warn("not import-errors, just regular errors")
console.dir(body)
@this.set('statusType','error');
@this.emit('importError',body.response.data.messages ? body.response.data.messages : {'import-type': ['Unknown error']})
@this.set('statusText',body.response.data.messages ? body.response.data.messages : 'Unknown error');
}
// @this.emit('hideDetails');
});
}
$(function () {
$('#import').on('click',function () {
console.warn("okay, click handler firing!!!")
postSave()
})
console.warn("JS click handler loaded!")
})
window.setTimeout(function() {
var what = @this.dinglefarts
console.warn("What is this: ",what)
},1000)
$class = title_case($this->option('item-type'));
$classString = "App\\Importer\\{$class}Importer";
$importer = new $classString($filename);
$importer->setCallbacks([$this, 'log'], [$this, 'progress'], [$this, 'errorCallback'])
->setUserId($this->option('user_id'))
->setUpdating($this->option('update'))
->setShouldNotify($this->option('send-welcome'))
->setUsernameFormat($this->option('username_format'));
// This $logFile/useFiles() bit is currently broken, so commenting it out for now
// $logFile = $this->option('logfile');
// \Log::useFiles($logFile);
$this->comment('======= Importing Items from '.$filename.' =========');
$importer->import();
$this->bar = null;
if (! empty($this->errors)) {
$this->comment('The following Errors were encountered.');
foreach ($this->errors as $asset => $error) {
$this->comment('Error: Item: '.$asset.' failed validation: '.json_encode($error));
}
} else {
$this->comment('All Items imported successfully!');
}
$this->comment('');
}
public function changeTypes() // UNUSED?

View file

@ -1,6 +1,6 @@
{{-- <template> --}}
<tr v-show="processDetail">
<tr>
<td colspan="5">
<div class="col-md-12">
@ -14,9 +14,6 @@
<span wire:ignore>
{{ Form::select('import_type', $importTypes, $activeFile->import_type, ['id' => 'import_type', 'class' => 'livewire-select2', 'placeholder' => '', 'data-livewire-model' => 'activeFile.import_type']) }}
</span>
{{-- <select2 :options="options.importTypes" v-model="options.importType" required> --}}
{{-- <option disabled value="0"></option> --}}
{{-- </select2> --}}
</div>
</div><!-- /dynamic-form-row -->
@ -48,8 +45,7 @@
</div><!-- /dynamic-form-row -->
@if($statusText)
<div class="alert col-md-8 col-md-offset-2 {{ $statusType == 'success' ? 'alert-success' : ($statusType == 'error' ? 'alert-danger' : 'alert-info') }}" style="text-align:left"
>
<div class="alert col-md-8 col-md-offset-2 {{ $statusType == 'success' ? 'alert-success' : ($statusType == 'error' ? 'alert-danger' : 'alert-info') }}" style="text-align:left">
{{ $statusText }}
</div><!-- /alert -->
@endif
@ -66,30 +62,22 @@
{{-- <template v-for="(header, index) in file.header_row"> --}}
@if($activeFile->header_row)
@foreach($activeFile->header_row AS $index => $header)
<div class="row" wire:key="fake_key-{{ base64_encode($header) }}-{{ $increment }}">
<div class="row" wire:key="header-row-{{ $increment }}">
<div class="col-md-12">
<div class="col-md-4 text-right">
<label for="field_map.{{ $index }}" class="control-label">{{ $header }}</label>
</div>
<div class="col-md-4 form-group">
<div required data-force-refresh="{{ $increment }}">
{{-- <select2 :options="columns" v-model="columnMappings[header]">
<option value="0">Do Not Import</option>
</select2> --}}
{{-- <span wire:ignore>--}}
{{ Form::select('field_map.'.$index, $columnOptions[$activeFile->import_type], @$activeFile->field_map[$header],
[
{{-- this, along with the JS glue below, is quite possibly near to the new Universal LW2 stuff? --}}
{{ Form::select('field_map.'.$index, $columnOptions[$activeFile->import_type], @$activeFile->field_map[$header],
[
'class' => 'mappings livewire-select2',
'wire:model' => 'field_map.'.$index, // I think it just can't read this :/
'data-livewire-mapping' => $header, // do we still need this?
'data-livewire-model' => 'field_map.'.$index, // start of a 'universal' way to do this?
'placeholder' => 'Do Not Import'
])
])
}}
{{-- /* 'wire:model' => 'activeFile.field_map.'.$header, doesn't work */
/*'class' => 'livewire-select2 mappings', */' --}}
{{-- </span>--}}
}}
</div>
</div>
<div class="col-md-4">
@ -106,7 +94,7 @@
<div class="row">
<div class="col-md-6 col-md-offset-2 text-right" style="padding-top: 20px;">
<button type="button" class="btn btn-sm btn-default" wire:click="$emit('hideDetails')">Cancel</button>
<button type="submit" class="btn btn-sm btn-primary" id="import">Import</button>
<button type="submit" class="btn btn-sm btn-primary" id="import" wire:click="postSave">Import</button>
<br><br>
</div>
</div><!-- /div row -->
@ -123,80 +111,6 @@
</div><!-- /div v-show -->
</td>
<script>
function postSave() {
// FIXME - this is just awful.
console.warn("Saving import!");
if (!@this['activeFile'].import_type) {
console.warn("didn't find an import type :(");
@this.set('statusType','error');
@this.set('statusText', "An import type is required... "); // TODO - translate me!
return false;
}
@this.set('statusType','pending');
@this.set('statusText',"Processing...");
// FIXME - switch this to a boring regular jquery post, or figure out how to use the baked-in copy of axios?
var mappings = {};
for(var i in @this.field_map) {
console.warn("I is: "+i)
console.warn("Field map for i is: "+@this.field_map[i])
console.dir(@this.activeFile)
console.dir(@this.activeFile.header_row)
console.warn("field value for is is: "+@this.activeFile.header_row[i])
mappings[@this.activeFile.header_row[i]] = @this.field_map[i]
}
axios.defaults.headers.common["X-CSRF-TOKEN"] = $('meta[name="csrf-token"]').attr('content')
axios.post('{{ route('api.imports.importFile', $activeFile->id) }}', {
'import-update': !!@this.update,
'send-welcome': !!@this.send_welcome,
'import-type': @this.activeFile.import_type,
'run-backup': !!@this.run_backup,
'column-mappings': mappings // FIXME - terrible name
}).then( (body) => {
console.warn("success!!!")
// Success
@this.set('statusType',"success");
@this.set('statusText', "Success... Redirecting.");
console.warn("Here is the body object: ")
console.dir(body);
// FIXME - can we 'flash' an update here?
window.location.href = body.data.messages.redirect_url;
}, (body) => {
// Failure
console.warn("failure!!!!")
if(body.response.data.status == 'import-errors') {
//window.eventHub.$emit('importErrors', body.messages);
console.warn("import error")
console.dir(body)
@this.set('statusType','error');
@this.emit('importError', body.response.data.messages)
//@this.set('statusText', "Error: "+body.response.data.messages.join("<br>"));
} else {
console.warn("not import-errors, just regular errors")
console.dir(body)
@this.set('statusType','error');
@this.emit('importError',body.response.data.messages ? body.response.data.messages : {'import-type': ['Unknown error']})
@this.set('statusText',body.response.data.messages ? body.response.data.messages : 'Unknown error');
}
// @this.emit('hideDetails');
});
}
$(function () {
$('#import').on('click',function () {
console.warn("okay, click handler firing!!!")
postSave()
})
console.warn("JS click handler loaded!")
})
window.setTimeout(function() {
var what = @this.dinglefarts
console.warn("What is this: ",what)
},1000)
</script>
</tr>
{{-- </template> --}}
<script>
@ -209,7 +123,8 @@ $(function () {
})
})
$('.livewire-select2').select2();
$('.livewire-select2').select2(); // TODO/FIXME (pick one) possibly embedded into the Universal Livewire Implementation
$('#import_type').on('select2:select', function (event) {
console.log("import_type select2 selected!!!!!!!!!!!")
//console.dir(event.params.data)