mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Reimplement saving and restoring submitted search filters between page loads
This commit is contained in:
@@ -12,8 +12,8 @@ module Spree
|
||||
before_action :load_order, only: [:edit, :update, :fire, :resend,
|
||||
:invoice, :print, :distribution]
|
||||
before_action :load_distribution_choices, only: [:new, :edit, :update, :distribution]
|
||||
|
||||
before_action :require_distributor_abn, only: :invoice
|
||||
before_action :restore_saved_query!, only: :index
|
||||
|
||||
respond_to :html, :json
|
||||
|
||||
@@ -119,6 +119,8 @@ module Spree
|
||||
private
|
||||
|
||||
def update_search_results
|
||||
session[:admin_orders_search] = search_params
|
||||
|
||||
render cable_ready: cable_car.inner_html(
|
||||
"#orders-index",
|
||||
partial("spree/admin/orders/table", locals: { pagy: @pagy, orders: @orders })
|
||||
@@ -137,6 +139,13 @@ module Spree
|
||||
{ q: { completed_at_not_null: 1, s: "completed_at desc" } }
|
||||
end
|
||||
|
||||
def restore_saved_query!
|
||||
return unless request.format.html?
|
||||
|
||||
@_params = ActionController::Parameters.new(session[:admin_orders_search] || {})
|
||||
@stored_query = search_params.to_query
|
||||
end
|
||||
|
||||
def on_update
|
||||
@order.recreate_all_fees!
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
%div.admin-orders-index-search{ "data-controller": "search" }
|
||||
%div.admin-orders-index-search{ "data-controller": "search", "data-search-restore-value": @stored_query }
|
||||
= form_with url: spree.admin_orders_url, id: "orders_form", method: :get, data: { remote: true, "search-target": "form" } do
|
||||
= hidden_field_tag :page, 1, class: "page"
|
||||
= hidden_field_tag :per_page, 15, class: "per-page"
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Controller } from "stimulus";
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ["form"];
|
||||
static values = { restore: String }; // Query string, eg: "?color=red&size=small"
|
||||
|
||||
connect() {
|
||||
this.#setup();
|
||||
@@ -49,6 +50,7 @@ export default class extends Controller {
|
||||
if (this.hasFormTarget) {
|
||||
this.form = this.formTarget;
|
||||
this.form.controller = this;
|
||||
if (this.restoreValue) this.#restoreFormState(this.form, this.restoreValue);
|
||||
} else {
|
||||
this.form = document.querySelector("form[data-search-target=form]");
|
||||
}
|
||||
@@ -65,4 +67,23 @@ export default class extends Controller {
|
||||
select.tomselect?.clear();
|
||||
});
|
||||
}
|
||||
|
||||
#restoreFormState(form, queryString) {
|
||||
const params = new URLSearchParams(queryString);
|
||||
|
||||
// Apply non-checkbox values
|
||||
for (const [key, value] of params.entries()) {
|
||||
const input = form.elements[key];
|
||||
if (input && input.type !== "checkbox") input.value = value;
|
||||
}
|
||||
|
||||
// Deal with checkbox values
|
||||
form.querySelectorAll("[type=checkbox]").forEach((checkbox) => {
|
||||
checkbox.checked = !!params.get(checkbox.name);
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(new CustomEvent("flatpickr:change"));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user