Skip to content

Commit

Permalink
Implement buttons at the bottom of the backup page in case of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Dec 11, 2024
1 parent e8d8914 commit db88c12
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 36 deletions.
5 changes: 4 additions & 1 deletion _dev/src/ts/components/LogsViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ export default class LogsViewer extends ComponentAbstract implements Destroyable
'.logs__download-form',
'Form to request the button to download logs cannot be found'
);
await api.post(downloadlogsButtonForm.dataset.downloadLogsRoute!, new FormData(downloadlogsButtonForm));
await api.post(
downloadlogsButtonForm.dataset.downloadLogsRoute!,
new FormData(downloadlogsButtonForm)
);

this.#appendFragmentElement(fragment, this.#logsSummary);
this.#isSummaryDisplayed = true;
Expand Down
29 changes: 29 additions & 0 deletions _dev/src/ts/pages/UpdatePageBackup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import api from '../api/RequestHandler';
export default class UpdatePageBackup extends UpdatePage {
protected stepCode = 'backup';
#progressTracker: ProgressTracker = new ProgressTracker(this.#progressTrackerContainer);
#submitSkipBackupForm: null | HTMLFormElement = null;
#submitErrorReportForm: null | HTMLFormElement = null;
#submitRetryForm: null | HTMLFormElement = null;
#submitRetryAlert: null | HTMLFormElement = null;

constructor() {
super();
Expand All @@ -29,6 +33,11 @@ export default class UpdatePageBackup extends UpdatePage {

public beforeDestroy = () => {
this.#progressTracker.beforeDestroy();

this.#submitSkipBackupForm?.removeEventListener('submit', this.#handleSubmit);
this.#submitErrorReportForm?.removeEventListener('submit', this.#handleSubmit);
this.#submitRetryForm?.removeEventListener('submit', this.#handleSubmit);
this.#submitRetryAlert?.removeEventListener('submit', this.#handleSubmit);
};

get #progressTrackerContainer(): HTMLDivElement {
Expand Down Expand Up @@ -80,5 +89,25 @@ export default class UpdatePageBackup extends UpdatePage {
}

buttonsContainer.classList.remove('hidden');

this.#submitSkipBackupForm = document.forms.namedItem('submit-skip-backup');
this.#submitSkipBackupForm?.addEventListener('submit', this.#handleSubmit);

this.#submitErrorReportForm = document.forms.namedItem('submit-error-report');
this.#submitErrorReportForm?.addEventListener('submit', this.#handleSubmit);

this.#submitRetryAlert = document.forms.namedItem('retry-alert');
this.#submitRetryAlert?.addEventListener('submit', this.#handleSubmit);

this.#submitRetryForm = document.forms.namedItem('retry-button');
this.#submitRetryForm?.addEventListener('submit', this.#handleSubmit);
};

#handleSubmit = async (event: SubmitEvent) => {
event.preventDefault();

const form = event.target as HTMLFormElement;

await api.post(form.dataset.routeToSubmit!);
};
}
5 changes: 1 addition & 4 deletions _dev/src/ts/pages/UpdatePageBackupOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ export default class UpdatePageBackup extends UpdatePage {

readonly #onClick = async (ev: Event) => {
if ((ev.target as HTMLElement).id === 'update-backup-page-skip-btn') {
const formData = new FormData();
// TODO: Value currently hardcoded until management of backups is implemented
formData.append('backupDone', JSON.stringify(false));
await api.post(this.#form.dataset.routeToSubmitUpdate!, formData);
await api.post(this.#form.dataset.routeToSubmitUpdate!);
}
};

Expand Down
3 changes: 3 additions & 0 deletions _dev/tests/components/LogsViewer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ describe('LogsViewer', () => {
container = document.createElement('div');
container.innerHTML = `
<div data-component="logs-viewer" class="logs__inner">
<form class="logs__download-form" data-download-logs-route="">
<input type="hidden" name="download-logs-type" value="" />
</form>
<div data-slot-component="scroll" class="logs__scroll" tabindex="0">
<div data-slot-component="list" class="logs__list"></div>
</div>
Expand Down
8 changes: 5 additions & 3 deletions classes/Services/LogsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace PrestaShop\Module\AutoUpgrade\Services;

use Exception;
use PrestaShop\Module\AutoUpgrade\State;
use PrestaShop\Module\AutoUpgrade\Task\TaskType;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function __construct(State $state, Translator $translator, string $logsPa
public function getDownloadLogsdData(string $task): array
{
$logsPath = $this->getDownloadLogsPath($task);

return [
'button_label' => $this->getDownloadLogsLabel($task),
'download_path' => $logsPath,
Expand Down Expand Up @@ -100,12 +102,12 @@ private function getDownloadLogsLabel(string $taskType): string
{
if ($taskType === TaskType::TASK_TYPE_BACKUP) {
return $this->translator->trans('Download backup logs');
}
}
if ($taskType === TaskType::TASK_TYPE_RESTORE) {
return $this->translator->trans('Download restore logs');
}
}
if ($taskType === TaskType::TASK_TYPE_UPDATE) {
return $this->translator->trans('Download update logs');
}
}
}
}
1 change: 0 additions & 1 deletion classes/Task/Backup/BackupFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public function run(): int
$remainingFiles = $backlog->getRemainingTotal();
if ($remainingFiles) {
$this->logger->info($this->translator->trans('Backup files in progress. %d files left', [$remainingFiles]));

$this->stepDone = false;
$res = $this->container->getZipAction()->compress($backlog, $this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $backupFilesFilename);
if (!$res) {
Expand Down
1 change: 1 addition & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ public function getLogsService(): LogsService
$this->getTranslator(),
$this->getProperty(self::LOGS_PATH)
);

return $this->logsService;
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/admin/self-managed/LogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getDownloadLogsButton(): JsonResponse
private function validateLogsType(string $type): void
{
if (!in_array($type, TaskType::ALL_TASKS)) {
throw new InvalidArgumentException('Unknown log type '. $type);
throw new InvalidArgumentException('Unknown log type ' . $type);
}
}
}
4 changes: 4 additions & 0 deletions controllers/admin/self-managed/UpdatePageBackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ protected function getParams(): array
$updateSteps->getStepParams($this::CURRENT_STEP),
[
'success_route' => Routes::UPDATE_PAGE_POST_BACKUP,
'submit_skip_backup_route' => Routes::UPDATE_STEP_BACKUP_SUBMIT_UPDATE,
'download_logs_route' => Routes::DOWNLOAD_LOGS,
'download_logs_type' => TaskType::TASK_TYPE_BACKUP,
'retry_route' => Routes::UPDATE_PAGE_BACKUP_OPTIONS,
// TODO: Move in a common controller
'submit_error_report_route' => Routes::UPDATE_STEP_UPDATE_SUBMIT_ERROR_REPORT,
'initial_process_action' => TaskName::TASK_BACKUP_INITIALIZATION,
'download_logs_parent_id' => PageSelectors::DOWNLOAD_LOGS_PARENT_ID,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@

namespace PrestaShop\Module\AutoUpgrade\Controller;

use PrestaShop\Module\AutoUpgrade\AjaxResponseBuilder;
use PrestaShop\Module\AutoUpgrade\Router\Routes;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\Task\TaskType;
use PrestaShop\Module\AutoUpgrade\Traits\DisplayErrorReportDialogTrait;
use PrestaShop\Module\AutoUpgrade\Twig\PageSelectors;
use PrestaShop\Module\AutoUpgrade\Twig\UpdateSteps;
use Symfony\Component\HttpFoundation\JsonResponse;

class UpdatePageUpdateController extends AbstractPageWithStepController
{
Expand Down Expand Up @@ -74,6 +72,7 @@ protected function getParams(): array
'download_logs_route' => Routes::DOWNLOAD_LOGS,
'download_logs_type' => TaskType::TASK_TYPE_UPDATE,
'restore_route' => Routes::RESTORE_PAGE_BACKUP_SELECTION,
// TODO: Move in a common controller
'submit_error_report_route' => Routes::UPDATE_STEP_UPDATE_SUBMIT_ERROR_REPORT,
'initial_process_action' => TaskName::TASK_UPDATE_INITIALIZATION,
'backup_available' => !empty($backupFinder->getAvailableBackups()),
Expand Down
51 changes: 30 additions & 21 deletions views/templates/steps/backup.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
{% include "@ModuleAutoUpgrade/components/alert.html.twig" with {
id: 'error-alert',
hidden: true,
alertStatus: backup_available ? 'warning' : 'danger',
title: 'Update failed'|trans({}),
message: backup_available
? 'Your store may not work properly anymore. Select the backup you want to use and restore it to avoid any data loss.'|trans({})
: 'You didn’t back up your store with the Update assistant module but you can restore using your own manual backup.'|trans({}),
buttonLabel: backup_available ? 'Restore'|trans({}) : null,
formRoute: backup_available ? restore_route : null,
formName: 'restore-alert',
alertStatus: 'warning',
title: 'Backup failed'|trans({}),
message:
'Please try again or perform a manual backup. In any case, ensure you have a backup before starting the update to avoid any data loss.'|trans({}),
buttonLabel: 'Retry backup'|trans({}),
formRoute: retry_route,
formName: 'retry-alert',
} %}
{% include "@ModuleAutoUpgrade/components/progress-tracker.html.twig" with {
successRoute: success_route,
Expand All @@ -34,23 +33,33 @@

{% block buttons %}
<div class="page__buttons hidden" id="error-buttons">
<form>
<form
id="submit-skip-backup"
name="submit-skip-backup"
data-route-to-submit="{{ submit_skip_backup_route }}"
>
<button class="btn btn-lg btn-link" type="submit">
{{ 'Update without backup'|trans({}) }}
</button>
</form>
<form
id="submit-error-report"
name="submit-error-report"
data-route-to-submit="{{ submit_error_report_route }}"
>
<button class="btn btn-lg btn-default" type="submit">
<i class="material-icons">send</i>
{{ 'Send error report'|trans({}) }}
</button>
</form>
{% if backup_available %}
<form
id="restore-button"
name="restore-button"
data-route-to-submit="{{ restore_route }}"
>
<button class="btn btn-lg btn-primary" type="submit">
<i class="material-icons">settings_backup_restore</i>
{{ 'Restore'|trans({}) }}
</button>
</form>
{% endif %}
<form
id="retry-button"
name="retry-button"
data-route-to-submit="{{ retry_route }}"
>
<button class="btn btn-lg btn-primary" type="submit">
{{ 'Retry backup'|trans({}) }}
</button>
</form>
</div>
{% endblock %}
6 changes: 3 additions & 3 deletions views/templates/steps/update.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
{% block buttons %}
<div class="page__buttons hidden" id="error-buttons">
<form
id="submit-error-report"
name="submit-error-report"
data-route-to-submit="{{ submit_error_report_route }}"
id="submit-error-report"
name="submit-error-report"
data-route-to-submit="{{ submit_error_report_route }}"
>
<button class="btn btn-lg btn-default" type="submit">
<i class="material-icons">send</i>
Expand Down

0 comments on commit db88c12

Please sign in to comment.