diff --git a/assets/controllers/reload_controller.js b/assets/controllers/reload_controller.js index f1674f5..d74838d 100644 --- a/assets/controllers/reload_controller.js +++ b/assets/controllers/reload_controller.js @@ -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(); } } diff --git a/config/packages/web_profiler.yaml b/config/packages/web_profiler.yaml index b946111..d1a73dc 100644 --- a/config/packages/web_profiler.yaml +++ b/config/packages/web_profiler.yaml @@ -1,6 +1,6 @@ when@dev: web_profiler: - toolbar: true + toolbar: false intercept_redirects: false framework: diff --git a/src/Controller/Application/ZohoController/ZohoController.php b/src/Controller/Application/ZohoController/ZohoController.php index 7e01f62..e5d561c 100644 --- a/src/Controller/Application/ZohoController/ZohoController.php +++ b/src/Controller/Application/ZohoController/ZohoController.php @@ -83,7 +83,7 @@ public function refreshZoho(): Response ]); return $this->streamBuilder->createResponse( - new ReplaceStream('app-' . $company->getId(), $html) + new ReplaceStream('main', $html) ); } } diff --git a/src/Service/Zoho/Sync/ProductSync.php b/src/Service/Zoho/Sync/ProductSync.php index 8f43432..c859b50 100644 --- a/src/Service/Zoho/Sync/ProductSync.php +++ b/src/Service/Zoho/Sync/ProductSync.php @@ -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; @@ -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'); } diff --git a/src/Service/Zoho/ZohoClient.php b/src/Service/Zoho/ZohoClient.php index e7fe8bf..8175cdf 100644 --- a/src/Service/Zoho/ZohoClient.php +++ b/src/Service/Zoho/ZohoClient.php @@ -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, ); diff --git a/src/Service/ZohoImprovedManager.php b/src/Service/ZohoImprovedManager.php index 88f6fc6..4b38445 100644 --- a/src/Service/ZohoImprovedManager.php +++ b/src/Service/ZohoImprovedManager.php @@ -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; @@ -55,6 +56,7 @@ public function __construct( private MessageBusInterface $messageBus, private ZohoClient $zohoClient, private StreamBuilder $streamBuilder, + private LoggerInterface $logger, ) { } @@ -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; @@ -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; diff --git a/src/Turbo/Stream/Model/AbstractStream.php b/src/Turbo/Stream/Model/AbstractStream.php index cdfcba6..98ec421 100644 --- a/src/Turbo/Stream/Model/AbstractStream.php +++ b/src/Turbo/Stream/Model/AbstractStream.php @@ -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(); @@ -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' */ diff --git a/src/Turbo/Stream/ReloadStream.php b/src/Turbo/Stream/ReloadStream.php index 531e17c..1c04a0f 100644 --- a/src/Turbo/Stream/ReloadStream.php +++ b/src/Turbo/Stream/ReloadStream.php @@ -19,6 +19,11 @@ protected function getHtml(): ?string HTML; } + protected function getStreamTarget(): string + { + return 'streams'; + } + #[Override] protected function getAction(): string { diff --git a/templates/app/app_all.html.twig b/templates/app/app_all.html.twig index b17b128..5e8e0d1 100644 --- a/templates/app/app_all.html.twig +++ b/templates/app/app_all.html.twig @@ -8,6 +8,7 @@ + {% block head %}{% endblock %} @@ -84,6 +85,9 @@ {% endblock body %} +