Skip to content

Commit

Permalink
Merge pull request #951 from ga-devfront/feat/continue-version-choice
Browse files Browse the repository at this point in the history
[NEW UI] finish front version choice
  • Loading branch information
ga-devfront authored Oct 25, 2024
2 parents fda0950 + 1994777 commit 96c18b0
Show file tree
Hide file tree
Showing 44 changed files with 562 additions and 305 deletions.
29 changes: 0 additions & 29 deletions .github/action_upgrade.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
#!/bin/bash

if [[ $CHANNEL == "local" ]]; then
if [[ "${FROM:0:1}" == 1 ]] && [[ "${VERSION:0:1}" == 1 ]]; then
if [[ "${FROM:2:1}" == "${VERSION:2:1}" ]]; then
UPDATE_THEME=0
else
UPDATE_THEME=1
fi
else
if [[ "${FROM:0:1}" == "${VERSION:0:1}" ]]; then
UPDATE_THEME=0
else
UPDATE_THEME=1
fi
fi

docker exec -u www-data prestashop_autoupgrade mkdir admin-dev/autoupgrade/download
docker exec -u www-data prestashop_autoupgrade curl -L $ARCHIVE_URL -o admin-dev/autoupgrade/download/prestashop.zip
docker exec -u www-data prestashop_autoupgrade curl -L $XML_URL -o modules/autoupgrade/download/prestashop.xml

FILE_COUNT=$(docker exec -u www-data prestashop_autoupgrade ls admin-dev/autoupgrade/backup/ | wc -l)
if [[ "$FILE_COUNT" == 0 ]]; then
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console backup:create admin-dev
fi

echo "{\"channel\":\"local\",\"archive_prestashop\":\"prestashop.zip\",\"archive_num\":\"${VERSION}\", \"archive_xml\":\"prestashop.xml\", \"PS_AUTOUP_CHANGE_DEFAULT_THEME\":${UPDATE_THEME}" > modules/autoupgrade/config.json
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="CompareReleases" --config-file-path="modules/autoupgrade/config.json" admin-dev
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --config-file-path="modules/autoupgrade/config.json" admin-dev
fi

docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console backup:create admin-dev
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="CompareReleases" admin-dev
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start admin-dev
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Here is an example of the different fields that can be found in it:
```json
{
"channel": "local", // see https://devdocs.prestashop-project.org/8/basics/keeping-up-to-date/upgrade-module/channels/
"archive_prestashop": "prestashop_8.0.0.zip", // Name of the zip file, specific to the archive channel, to be placed in the [your-admin-dir]/autoupgrade/download folder
"archive_zip": "prestashop_8.0.0.zip", // Name of the zip file, specific to the archive channel, to be placed in the [your-admin-dir]/autoupgrade/download folder
"archive_xml": "prestashop_8.0.0.xml", // Name of the XML file, specific to the archive channel, to be placed in the [your-admin-dir]/autoupgrade/download folder
"archive_num": "8.0.0", // Release number, specific to the archive channel
"PS_AUTOUP_CUSTOM_MOD_DESACT": 1, // Disable non-native modules
Expand Down
12 changes: 11 additions & 1 deletion _dev/src/ts/api/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ import { ApiResponse } from '../types/apiTypes';
import Hydration from '../utils/Hydration';

export class RequestHandler {
private currentRequestAbortController: AbortController | null = null;

public post(route: string, data = new FormData(), fromPopState?: boolean) {
if (this.currentRequestAbortController) {
this.currentRequestAbortController.abort();
}

this.currentRequestAbortController = new AbortController();
const { signal } = this.currentRequestAbortController;

data.append('dir', window.AutoUpgradeVariables.admin_dir);

baseApi
.post('', data, {
params: { route: route }
params: { route: route },
signal
})
.then((response) => {
const data = response.data as ApiResponse;
Expand Down
57 changes: 33 additions & 24 deletions _dev/src/ts/pages/HomePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,64 @@ import PageAbstract from './PageAbstract';
import api from '../api/RequestHandler';

export default class HomePage extends PageAbstract {
form: HTMLFormElement;
submitButton: HTMLButtonElement;

constructor() {
super();
const form = document.forms.namedItem('next_page');
if (form) {
this.form = form;
} else {
if (!this.form) {
throw new Error("The form wasn't found inside DOM. HomePage can't be initiated properly");
}
const submitButton = Array.from(this.form.elements).find(
(element) => element instanceof HTMLButtonElement && element.type === 'submit'
) as HTMLButtonElement | null;
if (submitButton) {
this.submitButton = submitButton;
} else {
if (!this.submitButton) {
throw new Error(
"The submit button wasn't found inside DOM. HomePage can't be initiated properly"
);
}
}

public mount = () => {
this.checkForm();
this.form.addEventListener('change', this.checkForm);
this.form.addEventListener('submit', this.handleSubmit);
if (this.form) {
this.checkForm();
this.form.addEventListener('change', this.checkForm);
this.form.addEventListener('submit', this.handleSubmit);
}
};

public beforeDestroy = () => {
this.form.removeEventListener('change', this.checkForm);
this.form.removeEventListener('submit', this.handleSubmit);
if (this.form) {
this.form.removeEventListener('change', this.checkForm);
this.form.removeEventListener('submit', this.handleSubmit);
}
};

private checkForm = () => {
if (this.form.checkValidity()) {
this.submitButton.removeAttribute('disabled');
if (this.formIsValid) {
this.submitButton?.removeAttribute('disabled');
} else {
this.submitButton.setAttribute('disabled', 'true');
this.submitButton?.setAttribute('disabled', 'true');
}
};

private handleSubmit = (event: Event) => {
event.preventDefault();
const route = this.form.dataset.route;
const routeToSubmit = this.form?.dataset.routeToSubmit;

if (route) {
if (routeToSubmit) {
const formData = new FormData(this.form);
api.post(route, formData);
api.post(routeToSubmit, formData);
}
};

private get form(): HTMLFormElement | null {
return document.forms.namedItem('next_page');
}

private get formIsValid(): boolean {
return this.form ? this.form.checkValidity() : false;
}

private get submitButton(): HTMLButtonElement | undefined {
return this.form
? (Array.from(this.form.elements).find(
(element) => element instanceof HTMLButtonElement && element.type === 'submit'
) as HTMLButtonElement | undefined)
: undefined;
}
}
167 changes: 166 additions & 1 deletion _dev/src/ts/pages/UpdatePageVersionChoice.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,178 @@
import UpdatePage from './UpdatePage';
import api from '../api/RequestHandler';
import Hydration from '../utils/Hydration';

export default class UpdatePageVersionChoice extends UpdatePage {
protected stepCode = 'version-choice';
private radioLoadingClass = 'radio--show-requirements-loader';

constructor() {
super();
}

public mount() {
public mount = () => {
this.initStepper();
if (!this.form) return;

this.form.addEventListener('change', this.saveForm.bind(this));
this.form.addEventListener('submit', this.handleSubmit);

this.onlineCardParent?.addEventListener(Hydration.hydrationEventName, this.handleHydrate);
this.localCardParent?.addEventListener(Hydration.hydrationEventName, this.handleHydrate);

this.toggleNextButton();
this.addListenerToCheckRequirementsAgainButtons();
};

public beforeDestroy = () => {
if (!this.form) return;
this.form.removeEventListener('change', this.saveForm);
this.form.removeEventListener('submit', this.handleSubmit);
this.onlineCardParent?.removeEventListener(Hydration.hydrationEventName, this.toggleNextButton);
this.localCardParent?.removeEventListener(Hydration.hydrationEventName, this.toggleNextButton);
this.checkRequirementsAgainButtons?.forEach((element) => {
element.removeEventListener('click', this.saveForm);
});
};

private sendForm = (routeToSend: string) => {
const formData = new FormData(this.form!);
api.post(routeToSend, formData);
};

private addListenerToCheckRequirementsAgainButtons = () => {
if (this.checkRequirementsAgainButtons?.length) {
this.checkRequirementsAgainButtons.forEach((element) => {
element.addEventListener('click', this.saveForm);
});
}
};

private handleHydrate = () => {
this.toggleNextButton();
this.addListenerToCheckRequirementsAgainButtons();
};

private toggleNextButton = () => {
if (this.currentChannelRequirementsAreOk) {
this.submitButton?.removeAttribute('disabled');
} else {
this.submitButton?.setAttribute('disabled', 'true');
}
};

private saveForm = () => {
const routeToSave = this.form!.dataset.routeToSave;

if (!routeToSave) {
throw new Error('No route to save form provided. Impossible to save form.');
}

let currentInputCheck = null;

if (this.onlineInputIsChecked) {
currentInputCheck = this.onlineInputElement!;
}

if (this.localInputIsCheckAndFullFilled) {
currentInputCheck = this.localInputElement!;
}

if (currentInputCheck) {
currentInputCheck.removeAttribute('data-requirements-are-ok');
this.toggleNextButton();
currentInputCheck.classList.add(this.radioLoadingClass);
this.sendForm(routeToSave);
}
};

private handleSubmit = (event: Event) => {
event.preventDefault();
const routeToSubmit = this.form!.dataset.routeToSubmit;

if (!routeToSubmit) {
throw new Error('No route to submit form provided. Impossible to submit form.');
}

this.sendForm(routeToSubmit);
};

// global form
private get form(): HTMLFormElement | null {
return document.forms.namedItem('version_choice');
}

private get submitButton(): HTMLButtonElement | undefined {
return this.form
? (Array.from(this.form.elements).find(
(element) => element instanceof HTMLButtonElement && element.type === 'submit'
) as HTMLButtonElement | undefined)
: undefined;
}

private get currentChannelRequirementsAreOk(): boolean {
if (this.onlineInputIsChecked) {
return this.onlineInputElement!.dataset.requirementsAreOk === '1';
}
if (this.localInputIsCheckAndFullFilled) {
return this.localInputElement!.dataset.requirementsAreOk === '1';
}
return false;
}

private get checkRequirementsAgainButtons(): HTMLButtonElement[] | undefined {
return this.form
? (Array.from(this.form.elements).filter(
(element): element is HTMLButtonElement =>
element instanceof HTMLButtonElement &&
element.dataset.action === 'check-requirements-again'
) as HTMLButtonElement[])
: undefined;
}

// online option
private get onlineCardParent(): HTMLDivElement | undefined {
return document.getElementById('radio_card_online') as HTMLDivElement | undefined;
}

private get onlineInputElement(): HTMLInputElement | undefined {
return this.form?.elements.namedItem('online') as HTMLInputElement | undefined;
}

private get onlineInputIsChecked(): boolean {
return (this.onlineInputElement && this.onlineInputElement.checked) || false;
}

// local option
private get localCardParent(): HTMLDivElement | undefined {
return document.getElementById('radio_card_archive') as HTMLDivElement | undefined;
}

private get localInputElement(): HTMLInputElement | undefined {
return this.form?.elements.namedItem('local') as HTMLInputElement | undefined;
}

private get localInputIsChecked(): boolean {
return this.localInputElement?.checked || false;
}

private get archiveZipSelectElement(): HTMLSelectElement | undefined {
return this.form?.elements.namedItem('archive_zip') as HTMLSelectElement | undefined;
}

private get archiveZipIsFilled(): boolean {
return !!this.archiveZipSelectElement?.value;
}

private get archiveXmlSelectElement(): HTMLSelectElement | undefined {
return this.form!.elements.namedItem('archive_xml') as HTMLSelectElement | undefined;
}

private get archiveXmlIsFilled(): boolean {
return (this.archiveXmlSelectElement && !!this.archiveXmlSelectElement.value) || false;
}

private get localInputIsCheckAndFullFilled(): boolean {
return this.localInputIsChecked && this.archiveZipIsFilled && this.archiveXmlIsFilled;
}
}
5 changes: 5 additions & 0 deletions _dev/src/ts/utils/Hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { ApiResponseHydration } from '../types/apiTypes';
import { routeHandler, scriptHandler } from '../autoUpgrade';

export default class Hydration {
public static hydrationEventName = 'hydrate';
public hydrationEvent = new Event(Hydration.hydrationEventName);

public hydrate(data: ApiResponseHydration, fromPopState?: boolean) {
const elementToUpdate = document.getElementById(data.parent_to_update);

Expand All @@ -15,6 +18,8 @@ export default class Hydration {
routeHandler.setNewRoute(data.new_route);
}
}

elementToUpdate.dispatchEvent(this.hydrationEvent);
}
}
}
Loading

0 comments on commit 96c18b0

Please sign in to comment.