Skip to content

Commit

Permalink
Plug the validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Oct 29, 2024
1 parent f4ba429 commit 1ed1fb8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
8 changes: 4 additions & 4 deletions _dev/src/ts/pages/UpdatePageUpdateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class UpdatePageUpdateOptions extends UpdatePage {

private initFormFields(form: HTMLFormElement): void {
Array.from(form.elements).forEach((element) => {
if (element.nodeName !== "INPUT") {
if (element.nodeName !== 'INPUT') {
return;
}

Expand All @@ -52,17 +52,17 @@ export default class UpdatePageUpdateOptions extends UpdatePage {
}

private onInputChange = (ev: Event) => {
const optionInput = (ev.target as Element);
const optionInput = ev.target as HTMLInputElement;

optionInput.setAttribute('disabled', 'true');

const data = new FormData();
data.append('name', optionInput.name);
data.append('value', optionInput.checked);
data.append('value', JSON.stringify(optionInput.checked));
api.post(this.form.dataset.routeToSave!, data);

optionInput.removeAttribute('disabled');
}
};

private onFormSubmit = (event: Event) => {
event.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Router\Routes;
use PrestaShop\Module\AutoUpgrade\Twig\PageSelectors;
use PrestaShop\Module\AutoUpgrade\Twig\UpdateSteps;
use Symfony\Component\HttpFoundation\JsonResponse;

Expand All @@ -55,24 +56,32 @@ protected function displayRouteInUrl(): ?string

public function saveOption(): JsonResponse
{
$name = $this->request->request->get('name');
if ($name === 'PS_DISABLE_OVERRIDES') {
$this->upgradeContainer->initPrestaShopCore();
UpgradeConfiguration::updatePSDisableOverrides($this->request->request->getBoolean('value'));
return new JsonResponse(['success' => true]);
}

$upgradeConfiguration = $this->upgradeContainer->getUpgradeConfiguration();
$upgradeConfigurationStorage = $this->upgradeContainer->getUpgradeConfigurationStorage();

// TODO: Call the validator
$name = $this->request->request->get('name');

$upgradeConfiguration->merge([
$config = [
$name => $this->request->request->getBoolean('value'),
]);
];

// TODO: Remove after rebase
$upgradeConfiguration->validate($config);
$error = null;
// TODO: Uncomment after rebase
// $error = $this->upgradeContainer->getConfigurationValidator()->validate($config);

if (empty($error)) {
if ($name === 'PS_DISABLE_OVERRIDES') {
$this->upgradeContainer->initPrestaShopCore();
UpgradeConfiguration::updatePSDisableOverrides($this->request->request->getBoolean('value'));
} else {
$upgradeConfiguration->merge($config);
$upgradeConfigurationStorage->save($upgradeConfiguration, UpgradeFileNames::CONFIG_FILENAME);
}
}

$success = $upgradeConfigurationStorage->save($upgradeConfiguration, UpgradeFileNames::CONFIG_FILENAME);
return new JsonResponse(['success' => $success]);
return $this->getRefreshOfForm(array_merge($this->getParams(), ['error' => $error]));
}

public function submit(): JsonResponse
Expand All @@ -97,10 +106,33 @@ protected function getParams(): array
'form_route_to_save' => Routes::UPDATE_STEP_UPDATE_OPTIONS_SAVE_OPTION,
'form_route_to_submit' => Routes::UPDATE_STEP_UPDATE_OPTIONS_SUBMIT_FORM,

'default_deactive_non_native_modules' => $upgradeConfiguration->shouldDeactivateCustomModules(),
'default_regenerate_email_templates' => $upgradeConfiguration->shouldRegenerateMailTemplates(),
'disable_all_overrides' => !$upgradeConfiguration->isOverrideAllowed(),
'form_fields' => [
'deactive_non_native_modules' => [
'field' => 'PS_AUTOUP_CUSTOM_MOD_DESACT',
'value' => $upgradeConfiguration->shouldDeactivateCustomModules(),
],
'regenerate_email_templates' => [
'field' => 'PS_AUTOUP_REGEN_EMAIL',
'value' => $upgradeConfiguration->shouldRegenerateMailTemplates(),
],
'disable_all_overrides' => [
'field' => 'PS_DISABLE_OVERRIDES',
'value' => !$upgradeConfiguration->isOverrideAllowed(),
],
],
]
);
}

private function getRefreshOfForm(array $params): JsonResponse
{
return AjaxResponseBuilder::hydrationResponse(
PageSelectors::STEP_PARENT_ID,
$this->getTwig()->render(
'@ModuleAutoUpgrade/steps/' . $this->getStepTemplate() . '.html.twig',
$params
),
$this->displayRouteInUrl()
);
}
}
21 changes: 12 additions & 9 deletions views/templates/steps/update-options.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,33 @@
name="update-options-page-form"
>
{% include "@ModuleAutoUpgrade/components/render-switch.html.twig" with {
id: "PS_AUTOUP_CUSTOM_MOD_DESACT",
name: "PS_AUTOUP_CUSTOM_MOD_DESACT",
id: form_fields.deactive_non_native_modules.field,
name: form_fields.deactive_non_native_modules.field,
title: "Deactivate non-native modules",
description: "All the modules installed after creating your store are considered non-native modules. They might be " ~
"incompatible with the new version of PrestaShop. We recommend deactivating them during the update.",
value: default_deactive_non_native_modules
value: form_fields.deactive_non_native_modules.value,
error_message: error[form_fields.deactive_non_native_modules.field],
} %}

{% include "@ModuleAutoUpgrade/components/render-switch.html.twig" with {
id: "PS_AUTOUP_REGEN_EMAIL",
name: "PS_AUTOUP_REGEN_EMAIL",
id: form_fields.regenerate_email_templates.field,
name: form_fields.regenerate_email_templates.field,
title: "Regenerate email templates",
description: "If you've customized email templates, your changes will be lost if you activate this option.",
value: default_regenerate_email_templates
value: form_fields.regenerate_email_templates.value,
error_message: error[form_fields.regenerate_email_templates.field],
} %}

{% include "@ModuleAutoUpgrade/components/render-switch.html.twig" with {
id: "PS_AUTOUP_DISABLE_OVERRIDE",
name: "PS_AUTOUP_DISABLE_OVERRIDE",
id: form_fields.disable_all_overrides.field,
name: form_fields.disable_all_overrides.field,
title: "Disable all overrides",
description: "Overriding is a way to replace business behaviors (class files and controller files) to target only " ~
"one method or as many as you need. This option disables all classes & controllers overrides, allowing " ~
"you to avoid conflicts during and after updates.",
value: disable_all_overrides
value: form_fields.disable_all_overrides.value,
error_message: error[form_fields.disable_all_overrides.field],
} %}
</form>
{% endblock %}
Expand Down

0 comments on commit 1ed1fb8

Please sign in to comment.