Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ committed Nov 30, 2024
1 parent 5c64c22 commit 2f3188b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
13 changes: 7 additions & 6 deletions app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ class TaxonomyTerm extends Model
public function getFormattedMetadataAttribute()
{
$response = array();
$filteredMetadata = array_filter($this->metadata, function ($value) {
return !is_null($value['value']);
});
if (is_array($this->metadata)) {
$filteredMetadata = array_filter($this->metadata, function ($value) {
return !is_null($value['value']);
});

foreach ($filteredMetadata as $metadata) {
$response[$metadata['code']] = $metadata['value'];
foreach ($filteredMetadata as $metadata) {
$response[$metadata['code']] = $metadata['value'];
}
}

return $response;
}

Expand Down
7 changes: 4 additions & 3 deletions app/Http/Controllers/Backend/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ public function edit(Taxonomy $taxonomy)
public function update(Request $request, Taxonomy $taxonomy)
{
$data = $request->validate([
'code' => 'required',
'name' => 'required',
'code' => 'string|required',
'name' => 'string|required',
'description' => 'nullable',
'properties' => 'string'
]);

try {
$taxonomy->update($data);
$taxonomy->properties = $request->properties;
$taxonomy->properties = json_decode($request->properties);
$taxonomy->updated_by = Auth::user()->id;
$taxonomy->save();
return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy updated successfully');
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Controllers/Backend/TaxonomyTermController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function store(Request $request, Taxonomy $taxonomy, TaxonomyTerm $taxono
]);


foreach (json_decode($taxonomy->properties, true) as $property) {
foreach ($taxonomy->properties as $property) {
$metadataKey = "metadata.{$property['code']}";

switch ($property['data_type']) {
Expand Down Expand Up @@ -86,7 +86,7 @@ public function store(Request $request, Taxonomy $taxonomy, TaxonomyTerm $taxono

$metadataArray = [];

foreach (json_decode($taxonomy->properties, true) as $property) {
foreach ($taxonomy->properties as $property) {
$value = $request->input("metadata.{$property['code']}");

if ($property['data_type'] === 'boolean') {
Expand All @@ -99,7 +99,7 @@ public function store(Request $request, Taxonomy $taxonomy, TaxonomyTerm $taxono
}

$taxonomyTerm = new TaxonomyTerm($validatedData);
$taxonomyTerm->metadata = json_encode($metadataArray);
$taxonomyTerm->metadata = $metadataArray;
$taxonomyTerm->created_by = Auth::user()->id;
$taxonomyTerm->save();

Expand Down Expand Up @@ -142,7 +142,7 @@ public function update(Request $request, Taxonomy $taxonomy, TaxonomyTerm $term)
'metadata' => 'array',
]);

foreach (json_decode($taxonomy->properties, true) as $property) {
foreach ($taxonomy->properties as $property) {
$metadataKey = "metadata.{$property['code']}";

switch ($property['data_type']) {
Expand Down Expand Up @@ -179,7 +179,7 @@ public function update(Request $request, Taxonomy $taxonomy, TaxonomyTerm $term)
}

$metadataArray = [];
foreach (json_decode($taxonomy->properties, true) as $property) {
foreach ($taxonomy->properties as $property) {
$value = $request->input("metadata.{$property['code']}");

if ($property['data_type'] === 'boolean') {
Expand All @@ -193,7 +193,7 @@ public function update(Request $request, Taxonomy $taxonomy, TaxonomyTerm $term)
}

$term->update($validatedData);
$term->metadata = json_encode($metadataArray);
$term->metadata = $metadataArray;
$term->updated_by = Auth::user()->id;
$term->save();

Expand Down
2 changes: 1 addition & 1 deletion resources/views/backend/taxonomy/terms/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<strong>Metadata</strong>
</div>

@foreach (json_decode($taxonomy->properties, true) as $property)
@foreach ($taxonomy->properties as $property)
<div class="col-12 py-2">
<div class="col ps-0">
<label>{{ $property['name'] }}
Expand Down
6 changes: 3 additions & 3 deletions resources/views/backend/taxonomy/terms/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
<label for="parent_term">Parent Taxonomy Term (Optional)</label>
</div>
<select name="parent_id" class="form-select">
<option value="" selected>Select</option>
<option value="">Select</option>
@foreach ($term->taxonomy->terms as $sibling)
@if ($sibling->id != $term->id)
<option value="{{ $sibling->id }}"
{{ old('parent_id', $sibling->parent_id) == $sibling->id ? 'selected' : '' }}>
{{ old('parent_id', $term->parent_id) == $sibling->id ? 'selected' : '' }}>
{{ TaxonomyTerm::getHierarchicalPath($sibling->id) }}
</option>
@endif
Expand Down Expand Up @@ -82,7 +82,7 @@
<strong>Metadata</strong>
</div>

@foreach (json_decode($taxonomy->properties, true) as $property)
@foreach ($taxonomy->properties as $property)
<div class="col-12 py-2">
<div class="col ps-0">
<label>{{ $property['name'] }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,28 @@
data_type: this.newProperty.data_type
};
if (this.isEditing && this.editIndex !== null) {
if (this.editIndex >= 0 && this.editIndex < this.properties.length) {
this.properties[this.editIndex] = newPropertyData;
this.selectedItem = this.editIndex; // Select the edited item
if (newPropertyData.code != '' && newPropertyData.name != '' && newPropertyData.data_type !== '') {
if (this.isEditing && this.editIndex !== null) {
if (this.editIndex >= 0 && this.editIndex < this.properties.length) {
this.properties[this.editIndex] = newPropertyData;
this.selectedItem = this.editIndex; // Select the edited item
} else {
console.error('Invalid editIndex:', this.editIndex);
}
this.editIndex = null;
this.isEditing = false;
} else {
console.error('Invalid editIndex:', this.editIndex);
this.properties.push(newPropertyData);
this.selectedItem = this.properties.length - 1; // Select the newly added item
}
this.editIndex = null;
this.isEditing = false;
bootstrap.Modal.getInstance(document.getElementById('addPropertyItemModal')).hide();
{{-- nextTick ensures that the modal is hidden before resetting the form --}}
this.$nextTick(() => {
this.resetForm();
});
} else {
this.properties.push(newPropertyData);
this.selectedItem = this.properties.length - 1; // Select the newly added item
// TODO Give a warning
}
bootstrap.Modal.getInstance(document.getElementById('addPropertyItemModal')).hide();
{{-- nextTick ensures that the modal is hidden before resetting the form --}}
this.$nextTick(() => {
this.resetForm();
});
},
resetForm() {
this.newProperty = {
Expand Down Expand Up @@ -120,9 +124,9 @@
x-model="newProperty.name" />
</div>
<div class="mb-3">
<label for="propertyDatatype" class="form-label">Datatype</label>
<label for="propertyDatatype" class="form-label">Datatype*</label>
<select class="form-select" aria-label="Datatype" x-model="newProperty.data_type">
<option selected>Open this select menu</option>
<option value='' selected>Open this select menu</option>
<template x-for="(value,key) in propertyTypes" :key="key">
<option :value="key" x-text="value"></option>
</template>
Expand Down

0 comments on commit 2f3188b

Please sign in to comment.