Skip to content

Commit

Permalink
Minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zmitic committed Apr 9, 2024
1 parent a59b5cb commit 0804c0a
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 28 deletions.
28 changes: 21 additions & 7 deletions assets/controllers/reload_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,31 @@ export default class extends Controller {
if (!frame) {
return;
}

let reloadSrc = frame.getAttribute('data-reload-src');
if (reloadSrc) {
let a = document.createElement('a');
a.href = reloadSrc;
frame.appendChild(a);
a.click()
fetch(reloadSrc)
.then(response => {
if (response.ok) {
return response.text()
}
throw response.status
})
.then(html => {
Turbo.renderStreamMessage(html)

return;
})
.catch(error => console.warn(error))


// let a = document.createElement('a');
// a.style.display = 'none';
// a.href = reloadSrc;
// frame.appendChild(a);
// a.click()
//
// return;
}

frame.reload();
// frame.reload();
}
}
2 changes: 1 addition & 1 deletion config/packages/web_profiler.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
when@dev:
web_profiler:
toolbar: true
toolbar: false
intercept_redirects: false

framework:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function refreshZoho(): Response
]);

return $this->streamBuilder->createResponse(
new ReplaceStream('app-' . $company->getId(), $html)
new ReplaceStream('main', $html)
);
}
}
3 changes: 0 additions & 3 deletions src/Service/Zoho/Sync/ProductSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use App\Entity\ZohoAwareInterface;
use App\DTO\Zoho\Items as ZohoItems;
use App\Repository\Tax\TaxRepository;
use App\Entity\Product\ZohoStatusEnum;
use App\Service\Zoho\Model\SyncInterface;
use App\Message\Zoho\ZohoSyncEntityMessage;
use App\Repository\Product\ProductRepository;
Expand Down Expand Up @@ -80,8 +79,6 @@ public function onUpdate(ZohoAwareInterface $entity, array $changeSet): iterable
if (!any_key_exists(['name', 'description', 'price', 'tax'], $changeSet)) {
return;
}
$entity->setZohoStatus(ZohoStatusEnum::BUSY);

yield new ZohoSyncEntityMessage($entity, 'put');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Service/Zoho/ZohoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private function refreshToken(Company $company): string
{
$url = sprintf('%s/oauth/v2/token?refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token',
$this->baseUrl,
$company->getZohoRefreshToken() ?? throw new LogicException(),
$company->getZohoRefreshToken() ?? throw new LogicException(sprintf('Company %s does not have refresh token assigned.', $company->getId())),
$this->clientId,
$this->clientSecret,
);
Expand Down
9 changes: 9 additions & 0 deletions src/Service/ZohoImprovedManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Override;
use Generator;
use Throwable;
use Psr\Log\LoggerInterface;
use InvalidArgumentException;
use App\Entity\Company\Company;
use App\Service\Zoho\ZohoClient;
Expand Down Expand Up @@ -55,6 +56,7 @@ public function __construct(
private MessageBusInterface $messageBus,
private ZohoClient $zohoClient,
private StreamBuilder $streamBuilder,
private LoggerInterface $logger,
)
{
}
Expand Down Expand Up @@ -85,6 +87,10 @@ public function handleSyncEntityMessage(ZohoSyncEntityMessage $message): void
$this->companyRepository->flush();
$this->streamBuilder->pushToApp($company, new ReloadStream(sprintf('app-%s', $entity->getId())));
} catch (Throwable $e) {
$this->logger->critical($e->getMessage(), [
'exception' => $e,
'stack_trace' => $e->getTraceAsString(),
]);
throw new UnrecoverableMessageHandlingException(previous: $e);
} finally {
$this->syncEnabled = true;
Expand Down Expand Up @@ -162,15 +168,18 @@ private function downloadAll(Company $company): void
$sync = $this->syncs->get($serviceName);
$mapperClass = $sync->getMappingClass();
$data = $this->zohoClient->get($company, $sync->getBaseUrl(), $mapperClass);
$streams = [];
foreach ($data->getMany() as $item) {
$zohoId = $item->getId();
$entity = $sync->findEntityByZohoId($zohoId) ?? $sync->createNewEntity($company, $item);
$entity->setZohoId($item->getId());
$entity->setZohoStatus(ZohoStatusEnum::SYNCED);
$sync->map($entity, $item);
$streams[] = new ReloadStream(sprintf('app-%s', $entity->getId()));
}
// in case tagged service didn't flush its entities, do it here
$this->companyRepository->flush();
$this->streamBuilder->pushToApp($company, ...$streams);
}
} finally {
$this->syncEnabled = true;
Expand Down
8 changes: 7 additions & 1 deletion src/Turbo/Stream/Model/AbstractStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public function __toString(): string
public function generate(): string
{
$action = $this->getAction();
$targetId = ltrim($this->getTargetId(), '#');
$targetId = ltrim($this->getStreamTarget(), '#');

$attributeName = $this->attributeName;


$target = is_string($attributeName) ? sprintf('targets="[%s=\'%s\']"', $attributeName, $targetId) : sprintf('target="%s"', $targetId);

$html = (string)$this->getHtml();
Expand All @@ -55,6 +56,11 @@ public function getTargetId(): string
return $this->targetId;
}

protected function getStreamTarget(): string
{
return $this->getTargetId();
}

/**
* @return 'update'|'replace'|'remove'|'append'|'prepend'|'before'|'after'
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Turbo/Stream/ReloadStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ protected function getHtml(): ?string
HTML;
}

protected function getStreamTarget(): string
{
return 'streams';
}

#[Override]
protected function getAction(): string
{
Expand Down
4 changes: 4 additions & 0 deletions templates/app/app_all.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<link href="{{ asset('img/apple-touch-icon.png') }}" rel="apple-touch-icon">
<meta name="turbo-prefetch" content="false">
<meta name="turbo-cache-control" content="no-cache">
<meta name="turbo-refresh-method" content="morph">
{% block head %}{% endblock %}

<link href="https://fonts.gstatic.com" rel="preconnect">
Expand Down Expand Up @@ -84,6 +85,9 @@
<a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

{% endblock body %}
<div id="streams" class="alert alert-danger">
asdasd
</div>
</body>
</html>
{%- endblock all %}
24 changes: 13 additions & 11 deletions templates/app/embedded/table_card.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
<table class="table caption-top">
<caption>{{ caption|default('') }}</caption>

{# <colgroup>#}
{# <col class="col-200" span="1" />#}
{# <col class="xxx" span="1" />#}
{# <col class="col-edit" span="1" />#}
{# </colgroup>#}
{# <colgroup> #}
{# <col class="col-200" span="1" /> #}
{# <col class="xxx" span="1" /> #}
{# <col class="col-edit" span="1" /> #}
{# </colgroup> #}

<thead>
<tr>
{% for column in columns|default([]) %}
<th scope="col">{{ column }}</th>
{% endfor %}
<th scope="col" class="text-end">Actions</th>
</tr>
{% block thead %}
<tr>
{% for column in columns|default([]) %}
<th scope="col">{{ column }}</th>
{% endfor %}
<th scope="col" class="text-end">Actions</th>
</tr>
{% endblock thead %}
</thead>
<tbody>
{% block tbody -%}{%- endblock tbody %}
Expand Down
6 changes: 3 additions & 3 deletions templates/app/oauth2/zoho.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

{% block main -%}
{# @var company \App\Entity\Company\Company #}
<turbo-frame id="main" class="main" data-turbo-frame="main" data-turbo-prefetch="false" data-turbo-action="advance">
<div id="app-{{ company.id }}" data-reload-src="{{ path('zoho_connect') }}">
<turbo-frame id="main" class="main" data-turbo-frame="main" data-turbo-prefetch="false" data-turbo-action="visit">
<div id="app-{{ company.id }}" data-reload-src="{{ path('zoho_refresh') }}">

{# Avoid complex if-else statements by making smaller blocks, until Twig gets `match` equivalent #}
{{ is_connected ? block('_connected') : block('_not_connected') }}
Expand All @@ -25,6 +25,6 @@
</span>
Downloading data from Zoho
{% else %}
<a href="{{ path('zoho_download_all') }}" class="btn btn-danger" data-turbo-action="visit" data-turbo-method="put" data-turbo-confirm="Are you sure?">Download all</a>
<a href="{{ path('zoho_download_all') }}" class="btn btn-danger" data-turbo-frame="main" data-turbo-action="visit" data-turbo-method="put" xdata-turbo-confirm="Are you sure?">Download all</a>
{% endif %}
{%- endblock _connected %}

0 comments on commit 0804c0a

Please sign in to comment.