From 5b7a6f7239d5f83d5381a8e3aec9a7fa82ab5eba Mon Sep 17 00:00:00 2001 From: AlexandeBulete Date: Sun, 10 Nov 2024 03:53:21 +0100 Subject: [PATCH] fix: get and save data --- Model/Data/Payload/SavePayloadValue.php | 4 +-- Model/GetProductAttributes.php | 4 +-- Model/SaveProductAttributes.php | 26 ++++++++++++------- Observer/AdminLoginCookie.php | 20 ++++++++++++-- .../catalog/product/attribute_by_stores.phtml | 2 -- .../web/js/catalog/product/attributes.js | 23 +++++++++++----- vue/adminhtml/catalog/product/Attributes.vue | 10 +++---- vue/adminhtml/stores/product.ts | 15 ++++++++--- 8 files changed, 71 insertions(+), 33 deletions(-) diff --git a/Model/Data/Payload/SavePayloadValue.php b/Model/Data/Payload/SavePayloadValue.php index c1d54d7..5bdb9f9 100644 --- a/Model/Data/Payload/SavePayloadValue.php +++ b/Model/Data/Payload/SavePayloadValue.php @@ -18,12 +18,12 @@ class SavePayloadValue implements SavePayloadValueInterface /** * @var string */ - protected string $value; + protected ?string $value = null; /** * @var string */ - protected string $storeViewId; + protected string $storeViewId = ''; /** * @inheritDoc diff --git a/Model/GetProductAttributes.php b/Model/GetProductAttributes.php index 7352f57..cc4507d 100644 --- a/Model/GetProductAttributes.php +++ b/Model/GetProductAttributes.php @@ -95,8 +95,8 @@ protected function getAttributeConfig(string $attributeCode): GetResponseConfigI function (AttributeOptionInterface $attributeOption) { /** @var GetResponseConfigOptionsInterface $option */ $option = $this->getResponseConfigOptionsInterfaceFactory->create(); - $option->setLabel($attributeOption->getLabel()); - $option->setValue($attributeOption->getValue()); + $option->setLabel((string) $attributeOption->getLabel()); + $option->setValue((string) $attributeOption->getValue()); return $option; }, $attribute->getOptions() diff --git a/Model/SaveProductAttributes.php b/Model/SaveProductAttributes.php index ca586b0..4456fe1 100644 --- a/Model/SaveProductAttributes.php +++ b/Model/SaveProductAttributes.php @@ -33,16 +33,22 @@ public function execute(SavePayloadInterface $payload): \Opengento\BetterBo\Api\ { $success = $error = []; - $dataByStoreId = array_reduce( - $payload->getValues(), - static function ($r, SavePayloadValueInterface $value) use ($payload) { - $r[$value->getStoreViewId()] = [ - $payload->getAttributeCode() => $value->getValue() - ]; - return $r; - }, - [] - ); + // $dataByStoreId = array_reduce( + // $payload->getValues(), + // static function ($r, SavePayloadValueInterface $value) use ($payload) { + // $r[$value->getStoreViewId()] = [ + // $payload->getAttributeCode() => $value->getValue() + // ]; + // return $r; + // }, + // [] + // ); + $dataByStoreId = []; + foreach ($payload->getValues() as $value) { + $dataByStoreId[$value->getStoreViewId()] = [ + $payload->getAttributeCode() => $value->getValue() + ]; + } foreach ($dataByStoreId as $storeId => $data) { try { diff --git a/Observer/AdminLoginCookie.php b/Observer/AdminLoginCookie.php index aa800f7..8c80d23 100644 --- a/Observer/AdminLoginCookie.php +++ b/Observer/AdminLoginCookie.php @@ -42,12 +42,26 @@ public function execute(Observer $observer) CustomUserContext::USER_TYPE_ADMIN ); $params = $this->tokenParametersFactory->create(); - $token = $this->tokenIssuer->create($context, $params); + + // Create token $this->createAdminCookie($token); // Add these lines to save token in extra_data - $extraData = $user->getExtra() ?: []; + $this->saveTokenInDB($user, $token); + } + + /** + * Save token in extra_data + * + * @param User $user + * @param string $token + * + * @return void + */ + protected function saveTokenInDB(User $user, string $token): void + { + $extraData = json_decode($user->getExtra(), true) ?: []; $extraData['betterbo_token'] = $token; $user->setExtra(json_encode($extraData)); $this->userResource->save($user); @@ -56,6 +70,8 @@ public function execute(Observer $observer) /** * Create admin cookie * + * @deprecated + * * @param string $token * * @return void diff --git a/view/adminhtml/templates/catalog/product/attribute_by_stores.phtml b/view/adminhtml/templates/catalog/product/attribute_by_stores.phtml index d59c4f5..a2b69ca 100644 --- a/view/adminhtml/templates/catalog/product/attribute_by_stores.phtml +++ b/view/adminhtml/templates/catalog/product/attribute_by_stores.phtml @@ -8,8 +8,6 @@ $extraData = json_decode($user->getExtra(), true) ?: []; $bearer = $extraData['betterbo_token']; - // dd($bearer); - ?>
{ console.log(currentValue) - const originalValue = state.originalValues.find((original: any) => original.storeViewId === currentValue.storeViewId); + const originalValue = state.originalValues.find((original: any) => original.store_view_id === currentValue.store_view_id); return originalValue?.value !== currentValue.value; }).map((value: any) => ({ - storeViewId: value.storeViewId, + store_view_id: value.store_view_id, value: value.value })) } @@ -67,6 +67,9 @@ export const useProduct = defineStore('product', { this.originalValues = JSON.parse(JSON.stringify(data.data.values)) this.config = data.data.config this.loading = false + }).catch((error: any) => { + _catch(error) + this.loading = false }) }, /** @@ -118,6 +121,9 @@ export const useProduct = defineStore('product', { message: `Values not saved for ${this.errorValues.length} stores` }) } + }).catch((error: any) => { + _catch(error) + this.loading = false }) }, /** @@ -144,6 +150,9 @@ export const useProduct = defineStore('product', { }).then((response: any) => { const data = response.data this.errorLoading = false + }).catch((error: any) => { + _catch(error) + this.errorLoading = false }) } }