mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-18 20:53:24 +00:00
"Refactor UpdateURL function"
This commit is contained in:
@@ -43,7 +43,9 @@ const ArchivePage = createClass({
|
|||||||
componentDidMount: function () {
|
componentDidMount: function () {
|
||||||
console.log(this.props.query);
|
console.log(this.props.query);
|
||||||
console.log(this.state);
|
console.log(this.state);
|
||||||
if (this.state.title !== '') {
|
|
||||||
|
this.validateInput();
|
||||||
|
if (this.state.title) {
|
||||||
this.loadPage(this.state.page, false);
|
this.loadPage(this.state.page, false);
|
||||||
}
|
}
|
||||||
this.state.totalBrews || this.loadTotal(); // Load total if not already loaded
|
this.state.totalBrews || this.loadTotal(); // Load total if not already loaded
|
||||||
@@ -59,25 +61,14 @@ const ArchivePage = createClass({
|
|||||||
|
|
||||||
updateUrl: function (title, page, count, v3, legacy) {
|
updateUrl: function (title, page, count, v3, legacy) {
|
||||||
const url = new URL(window.location.href);
|
const url = new URL(window.location.href);
|
||||||
const urlParams = new URLSearchParams(url.search);
|
const urlParams = new URLSearchParams();
|
||||||
|
|
||||||
//clean all params
|
Object.entries({ title, page, count, v3, legacy }).forEach(([key, value]) => urlParams.set(key, value));
|
||||||
urlParams.delete('title');
|
|
||||||
urlParams.delete('page');
|
url.search = urlParams.toString();
|
||||||
urlParams.delete('count');
|
|
||||||
urlParams.delete('v3');
|
|
||||||
urlParams.delete('legacy');
|
|
||||||
|
|
||||||
urlParams.set('title', title);
|
|
||||||
urlParams.set('page', page);
|
|
||||||
urlParams.set('count', count);
|
|
||||||
urlParams.set('v3', v3);
|
|
||||||
urlParams.set('legacy', legacy);
|
|
||||||
|
|
||||||
url.search = urlParams.toString(); // Convert URLSearchParams to string
|
|
||||||
window.history.replaceState(null, null, url);
|
window.history.replaceState(null, null, url);
|
||||||
},
|
},
|
||||||
|
|
||||||
loadPage: async function (page, update) {
|
loadPage: async function (page, update) {
|
||||||
console.log('running loadPage');
|
console.log('running loadPage');
|
||||||
this.setState({ searching: true, error: null });
|
this.setState({ searching: true, error: null });
|
||||||
@@ -138,7 +129,7 @@ const ArchivePage = createClass({
|
|||||||
totalBrews: null,
|
totalBrews: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (title !== '') {
|
if (title) {
|
||||||
try {
|
try {
|
||||||
await request
|
await request
|
||||||
.get(
|
.get(
|
||||||
@@ -177,10 +168,10 @@ const ArchivePage = createClass({
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
validateInput: function (e) {
|
validateInput: function () {
|
||||||
const textInput = e.target
|
const textInput = document.getElementById('title');
|
||||||
const submitButton = document.getElementById('searchButton');
|
const submitButton = document.getElementById('searchButton');
|
||||||
if (textInput.value.length > 3) {
|
if (textInput.valid) {
|
||||||
submitButton.disabled = false;
|
submitButton.disabled = false;
|
||||||
} else {
|
} else {
|
||||||
submitButton.disabled = true;
|
submitButton.disabled = true;
|
||||||
@@ -199,9 +190,10 @@ const ArchivePage = createClass({
|
|||||||
type="text"
|
type="text"
|
||||||
name="title"
|
name="title"
|
||||||
defaultValue={this.state.title}
|
defaultValue={this.state.title}
|
||||||
onKeyUp={(e) => {
|
onKeyUp={() => {
|
||||||
this.validateInput(e);
|
this.validateInput();
|
||||||
}}
|
}}
|
||||||
|
pattern=".{2,}"
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
this.loadTotal();
|
this.loadTotal();
|
||||||
|
|||||||
Reference in New Issue
Block a user