Skip to content

Commit

Permalink
Fixed Issue #1744
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-webkul committed Dec 26, 2024
1 parent 1f90616 commit 7df0a86
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ public function massDestroy(MassDestroyRequest $massDestroyRequest): JsonRespons
]);
}

/**
* Get attribute options associated with attribute.
*
* @return \Illuminate\View\View
*/
public function getAttributeOptions(int $id)
{
$attribute = $this->attributeRepository->findOrFail($id);

return $attribute->options()->orderBy('sort_order')->get();
}

/**
* Download image or file
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- Input Form -->
<x-admin::form
:action="route('admin.settings.attributes.update', $attribute->id)"
enctype="multipart/form-data"
encType="multipart/form-data"
method="PUT"
>
<div class="flex flex-col gap-4">
Expand Down Expand Up @@ -132,7 +132,10 @@ class="secondary-button text-sm"
{!! view_render_event('admin.settings.attributes.edit.form_controls.option_type.before', ['attribute' => $attribute]) !!}
<!-- Input Option Type -->
<x-admin::form.control-group v-if="attributeType != 'lookup'" class="mb-2.5 w-1/2">
<x-admin::form.control-group
v-if="attributeType != 'lookup'"
class="mb-2.5 w-1/2"
>
<x-admin::form.control-group.label>
@lang('admin::app.settings.attributes.create.option-type')
</x-admin::form.control-group.label>
Expand All @@ -148,6 +151,7 @@ class="secondary-button text-sm"
<option value="lookup">
@lang('admin::app.settings.attributes.create.lookup')
</option>
<option value="options">
@lang('admin::app.settings.attributes.create.options')
</option>
Expand Down Expand Up @@ -613,9 +617,14 @@ class="primary-button"
isNullOptionChecked: false,
swatchValue: [],
swatchValue: [
{
image: [],
}
],
optionsData: @json($attribute->options()->orderBy('sort_order')->get()),
optionsData: [],
// optionsData: @json($attribute->options()->orderBy('sort_order')->get()),
optionIsNew: true,
Expand All @@ -627,6 +636,12 @@ class="primary-button"
}
},
created () {
// console.log(this.optionsData);
this.getAttributesOption();
},
methods: {
storeOptions(params, { resetForm, setValues }) {
if (! params.id) {
Expand All @@ -644,6 +659,12 @@ class="primary-button"
let formData = new FormData(this.$refs.editOptionsForm);
const sliderImage = formData.get("swatch_value[]");
if (sliderImage) {
params.swatch_value = sliderImage;
}
this.$refs.addOptionsRow.toggle();
if (params.swatch_value instanceof File) {
Expand Down Expand Up @@ -674,6 +695,8 @@ class="primary-button"
}
}
console.log(this.optionsData);
this.$emitter.emit('add-flash', { type: 'success', message: "@lang('admin::app.settings.attributes.edit.option-deleted')" });
}
});
Expand All @@ -685,6 +708,34 @@ class="primary-button"
}
},
getAttributesOption() {
this.$axios.get(`{{ route('admin.settings.attributes.options', $attribute->id) }}`)
.then(response => {
let options = response.data;
options.forEach((option) => {
let row = {
'id': option.id,
'name': option.name,
'sort_order': option.sort_order,
'attribute_id': option.attribute_id,
'isNew': false,
'isDelete': false,
};
if (! option.label) {
this.isNullOptionChecked = true;
row['notRequired'] = true;
} else {
row['notRequired'] = false;
}
this.optionsData.push(row);
});
});
},
setFile(file, id) {
let dataTransfer = new DataTransfer();
Expand Down
2 changes: 2 additions & 0 deletions packages/Webkul/Admin/src/Routes/Admin/settings-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@

Route::delete('{id}', 'destroy')->name('admin.settings.attributes.delete');

Route::get('{id}/options', 'getAttributeOptions')->name('admin.settings.attributes.options');

Route::post('mass-update', 'massUpdate')->name('admin.settings.attributes.mass_update');

Route::post('mass-destroy', 'massDestroy')->name('admin.settings.attributes.mass_delete');
Expand Down
39 changes: 19 additions & 20 deletions packages/Webkul/Attribute/src/Repositories/AttributeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,32 @@ public function update(array $data, $id, $attribute = 'id')

$attribute->update($data);

$previousOptionIds = $attribute->options()->pluck('id');
if (! in_array($attribute->type, ['select', 'multiselect', 'checkbox'])) {
return $attribute;
}

if (in_array($attribute->type, ['select', 'multiselect', 'checkbox']) && isset($data['options'])) {
$sortOrder = 1;
if (! isset($data['options'])) {
return $attribute;
}

foreach ($data['options'] as $optionId => $optionInputs) {
if (Str::contains($optionId, 'option_')) {
$this->attributeOptionRepository->create(array_merge([
'attribute_id' => $attribute->id,
'sort_order' => $sortOrder++,
], $optionInputs));
} else {
if (is_numeric($index = $previousOptionIds->search($optionId))) {
$previousOptionIds->forget($index);
}
foreach ($data['options'] as $optionId => $optionInputs) {
$isNew = $optionInputs['isNew'] == 'true';

$this->attributeOptionRepository->update(array_merge([
'sort_order' => $sortOrder++,
], $optionInputs), $optionId);
if ($isNew) {
$this->attributeOptionRepository->create(array_merge([
'attribute_id' => $attribute->id,
], $optionInputs));
} else {
$isDelete = $optionInputs['isDelete'] == 'true';

if ($isDelete) {
$this->attributeOptionRepository->delete($optionId);
} else {
$this->attributeOptionRepository->update($optionInputs, $optionId);
}
}
}

foreach ($previousOptionIds as $optionId) {
$this->attributeOptionRepository->delete($optionId);
}

return $attribute;
}

Expand Down

0 comments on commit 7df0a86

Please sign in to comment.