Skip to content

Commit

Permalink
PS-11966: Sorting and changing number of products per page are not wo…
Browse files Browse the repository at this point in the history
…rking
  • Loading branch information
danil.moroz committed Jan 10, 2020
1 parent f18d35c commit 06bf73d
Showing 1 changed file with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,54 @@ export default class WindowLocationApplicator extends Component {

protected onTriggerEvent(event: Event): void {
const categoryUrl = (<HTMLButtonElement>event.currentTarget).getAttribute('data-url');
/* tslint:disable: no-unbound-method */
const isFormData = typeof FormData !== 'undefined' && typeof FormData.prototype.get !== 'undefined';
const isURLSearchParams = typeof URLSearchParams !== 'undefined' && typeof URLSearchParams.prototype.get !== 'undefined';
/* tslint:enable: no-unbound-method */

this.getQueryString(categoryUrl ? categoryUrl : '');
if (isFormData && isURLSearchParams) {
this.getQueryString(categoryUrl ? categoryUrl : '');

return;
}

this.getQueryStringAlternative(categoryUrl ? categoryUrl : '');
}

protected getQueryString(categoryUrl: string = window.location.pathname): void {
const formData = new FormData(this.form);
const data = new URLSearchParams(<URLSearchParams>formData);
let formattedData = {};

// Array.from(formData).forEach(entries => {
// const key: string = String(entries[0]);
// const value: string = String(entries[1]);
//
// if (value.length) {
// return;
// }
//
// data.delete(key);
// });

// formData.forEach((value: string, key: string) => {
// console.log(value);
// if (value.length) {
// return;
// }
//
// data.delete(key);
// });

// debugger;

for (const [key, value] of formData.entries()) {
console.log(value);
console.log(key);

formattedData[key] = value;
}

Object.keys(formattedData).forEach((key: string) => {
console.log(formattedData[key]);
if (formattedData[key].length) {
formData.forEach((value: string, key: string) => {
if (value.length) {
return;
}

data.delete(key);
});

console.log('provet');

this.setWindowLocation(categoryUrl, data.toString());
}

protected getQueryStringAlternative(categoryUrl: string = window.location.pathname): void {
const inputFields = <HTMLInputElement[]>Array.from(this.form.getElementsByTagName('input'));
const selectFields = <HTMLSelectElement[]>Array.from(this.form.getElementsByTagName('select'));

const filteredInputFields = inputFields.filter((input: HTMLInputElement) => {
return input.checked || input.type === 'number' || input.type === 'hidden';
});
const formFields = [...filteredInputFields, ...selectFields];
const data: string = formFields.reduce((accumulator: string, field: HTMLInputElement | HTMLSelectElement) => {
if (field.name && field.value) {
accumulator += `&${field.name}=${field.value}`;
}

return accumulator;
}, '').slice(1);

this.setWindowLocation(categoryUrl, encodeURI(data));
}

protected setWindowLocation(categoryUrl: string, data: string): void {
window.location.href = `${categoryUrl}?${data}`;
}
Expand Down

0 comments on commit 06bf73d

Please sign in to comment.