Skip to content

Commit

Permalink
Bug fixes and bring the project up to standard
Browse files Browse the repository at this point in the history
  • Loading branch information
lachire committed May 14, 2024
1 parent fd6d9ea commit f44eff3
Show file tree
Hide file tree
Showing 27 changed files with 1,869 additions and 1,212 deletions.
10 changes: 5 additions & 5 deletions Api/SubmitProductsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

interface SubmitProductsInterface
{
const FEED_PATH = 'bloomreach_feed/';

/**
* POST
* Triggers a complete product feed submission to the bloomreach network.
*
* @api
* @param string $username
* @param string $password
*
* @return string
* @return array
*/
public function execute($username, $password);
public function execute();

/**
* GET
* Returns the status of the last or current product feed submission.
*
* @api
*
* @return string
* @return array
*/
public function getStatus();
}
33 changes: 33 additions & 0 deletions Block/Adminhtml/System/Config/AttrNameMappings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Bloomreach\Feed\Block\Adminhtml\System\Config;

/**
* Adminhtml "Attribute Name Mappings" field
*/
class AttrNameMappings extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
{
/**
* Prepare to render
*
* @return void
*/
protected function _prepareToRender()
{
$this->addColumn(
'source_attribute',
[
'label' => __('Source Attribute'),
'class' => 'required-entry admin__control-text'
]
);
$this->addColumn(
'target_attributes',
[
'label' => __('Target Attributes'),
'class' => 'required-entry admin__control-text'
]
);
$this->_addAfter = false;
}
}
183 changes: 38 additions & 145 deletions Block/Adminhtml/System/Config/SubmitProductsButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\UrlInterface;
use Magento\Backend\Model\Auth\Session;
use Magento\Integration\Model\Oauth\TokenFactory;
use Magento\Backend\Model\Auth\Session as AdminSession;

class SubmitProductsButton extends Field
{
private $urlBuilder;
private $authSession;
/**
* @var string
*/
protected $_template = 'Bloomreach_Feed::system/config/submit_products_button.phtml';

/** @var TokenFactory */
private $tokenFactory;

/** @var AdminSession */
private $adminSession;


/**
* Button constructor.
Expand All @@ -20,13 +29,26 @@ class SubmitProductsButton extends Field
*/
public function __construct(
Context $context,
UrlInterface $urlBuilder,
Session $authSession,
TokenFactory $tokenFactory,
AdminSession $adminSession,
array $data = []
) {
parent::__construct($context, $data);
$this->urlBuilder = $urlBuilder;
$this->authSession = $authSession;

$this->tokenFactory = $tokenFactory;
$this->adminSession = $adminSession;
}

/**
* Remove scope label
*
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element)
{
$element->setData('scope', null);
return parent::render($element);
}

/**
Expand All @@ -37,144 +59,15 @@ public function __construct(
*/
public function _getElementHtml(AbstractElement $element)
{
$value = $element->setData('style', 'display:none;');
$html = $element->getElementHtml();
$url = $this->urlBuilder->getUrl('/rest/V1/bloomreach/feed');
$btnId = "__br_submit_products_btn__";
$message = 'Are you sure you wish to submit your entire product catalog?\n(This operation can take a long time to complete depending on the size of your product listing)';
$authToken = $this->authSession->getUser()->getAuthToken();
$isLoggedIn = $this->authSession->isLoggedIn();
$userName = $this->authSession->getUser()->getUserName();

$html .= "
<div style='display: flex; flex-direction: column; width: 100%'>
<button type='button' id='$btnId'>
<span>Submit Products</span>
</button>
<input type='password' id='__br_submit_products_pass__' placeholder='Enter your password to confirm' />
<div id='__br_submit_products_message__' style=\"color: red; white-space: pre; max-width: 300px\"></div>
</div>
<script>
(function() {
// Get rid of the annoying jquery migration logs.
require(['jquery'], function (jQuery) {
if (jQuery) {
jQuery.migrateMute = true;
jQuery.migrateTrace = false;
}
});
const url = new URL(BASE_URL);
const baseURL = `\${url.protocol}//\${url.hostname}`;
window.__br_poll_feed_interval__ = window.__br_poll_feed_interval__ || null;
function jsonToYaml(json, message) {
try {
let obj = json;
if (typeof json === 'string') {
obj = JSON.parse(json);
}
const yamlStr = [];
const convertToYaml = (obj, indent) => {
if (typeof obj === 'string') {
obj = JSON.parse(obj);
}
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
const val = obj[prop];
if (typeof val === 'object') {
yamlStr.push(`\${' '.repeat(indent)}\${prop}:`);
convertToYaml(val, indent + 1);
} else {
yamlStr.push(`\${' '.repeat(indent)}\${prop}: \${val}`);
}
}
}
}
convertToYaml({ [message]: obj }, 0);
return yamlStr.join('\\n');
} catch (e) {
console.error(e);
}
}
// Poll the status of the async operation
window.clearInterval(window.__br_poll_feed_interval__);
window.__br_poll_feed_interval__ = window.setInterval(async () => {
const status = await fetch(`\${baseURL}/rest/V1/bloomreach/feed/status`);
const response = await status.json();
try {
if (typeof response === 'string') {
const statusResult = JSON.parse(response);
try {
const message = JSON.parse(statusResult.result_message);
document.querySelector('#__br_submit_products_message__').innerHTML = `\${jsonToYaml(message, 'Previous Run')}\nPending Submissions: \${statusResult.pending}`;
}
catch (err) {
document.querySelector('#__br_submit_products_message__').innerHTML = `\${statusResult.result_message}\nPending Submissions: \${statusResult.pending}`;
}
}
else {
document.querySelector('#__br_submit_products_message__').innerHTML = `\${response.message}`;
}
}
catch (e) {
document.querySelector('#__br_submit_products_message__').innerHTML = (
`Failed to parse response from server:\n\${JSON.stringify(response)}`
);
}
}, 1000);
document.querySelector('#$btnId').addEventListener('click', async function(e) {
e.preventDefault();
e.stopPropagation();
if (confirm('$message')) {
const formData = new FormData();
formData.append('username', '$userName');
formData.append('password', document.querySelector('#__br_submit_products_pass__').value);
// Begin an async operation for the polling service
const result = await fetch(
`\${baseURL}/rest/all/async/V1/bloomreach/feed`,
{
method: 'POST',
body: formData,
}
).then(async (response) => {
let result;
const res = await response.json();
try {
result = res;
if (result.request_items[0].status !== 'accepted') {
alert('Request for submission failed. Please try again or look for existing processes in the queue.');
}
}
catch (e) {
document.querySelector('#__br_submit_products_message__').innerHTML = (
`Failed to parse response from server:\n\${JSON.stringify(res)}`
);
}
return $this->_toHtml();
}

return result;
});
}
});
})();
</script>
";
public function getToken()
{
$token = $this->tokenFactory->create()
->createAdminToken($this->adminSession->getUser()->getId());

return $html;
return $token->getToken();
}

}
73 changes: 73 additions & 0 deletions Block/Adminhtml/System/Config/SubmitProductsHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Bloomreach\Feed\Block\Adminhtml\System\Config;

use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Integration\Model\Oauth\TokenFactory;
use Magento\Backend\Model\Auth\Session as AdminSession;

class SubmitProductsHistory extends Field
{
/**
* @var string
*/
protected $_template = 'Bloomreach_Feed::system/config/history.phtml';

/** @var TokenFactory */
private $tokenFactory;

/** @var AdminSession */
private $adminSession;


/**
* Button constructor.
* @param Context $context
* @param array $data
*/
public function __construct(
Context $context,
TokenFactory $tokenFactory,
AdminSession $adminSession,
array $data = []
) {
parent::__construct($context, $data);

$this->tokenFactory = $tokenFactory;
$this->adminSession = $adminSession;
}

/**
* Remove scope label
*
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element)
{
$element->setData('scope', null);
return parent::render($element);
}

/**
* Render button HTML
*
* @param AbstractElement $element
* @return string
*/
public function _getElementHtml(AbstractElement $element)
{
return $this->_toHtml();
}

public function getToken()
{
$token = $this->tokenFactory->create()
->createAdminToken($this->adminSession->getUser()->getId());

return $token->getToken();
}

}
42 changes: 0 additions & 42 deletions Block/ConfigurationSettingsInterface.php

This file was deleted.

Loading

0 comments on commit f44eff3

Please sign in to comment.