I'm handling parameters that are passed from a URL. I want to keep my URLS neat, and the following (rather exhaustive code) has been able to achieve it.
There will be more added, so it would be good to keep it versatile and compact. Code below does work.
URLS patterns
a.com/
a.com/q=bar
a.com/filters=foo
a.com/q=bar&filters=foo
Current code to account for above
const isSearch = this.props.location.pathname.includes("q=")
const isFilter = this.props.location.pathname.includes("filters=")
const encodedVal = encodeURIComponent(e.target.value)
if (encodedVal && isSearch && isFilter) {
const filters = this.props.match.params.q.split("filters=")[1].split("&")[0];
this.props.history.push("/q=" + encodedVal + "&filters=" + filters );
}
else if (encodedVal && isFilter) {
const filters = this.props.match.params.q.split("filters=")[1].split("&")[0];
this.props.history.push("/q=" + encodedVal + "&filters=" + filters );
}
else if (encodedVal && isSearch) {
this.props.history.push("/q=" + encodedVal);
}
else if (isFilter && !encodedVal) {
const filters = this.props.match.params.q.split("filters=")[1].split("&")[0];
this.props.history.push("/filters=" + filters);
}
else if (encodedVal) {
this.props.history.push("/q=" + encodedVal);
}
else {
this.props.history.push("/");
}
?-query URL instead? \$\endgroup\$