Restore form when form submit fails due to error

This commit is contained in:
Antoine Bartuccio 2024-12-20 19:41:38 +01:00
parent 7659c0cd8e
commit c6e95bc345
3 changed files with 42 additions and 8 deletions

View File

@ -2,12 +2,19 @@ import { exportToHtml } from "#core:utils/globals";
const quantityForTrayPrice = 6; const quantityForTrayPrice = 6;
interface InitialFormData {
/* Used to refill the form when the backend raises an error */
id?: string;
quantity?: number;
}
interface CounterConfig { interface CounterConfig {
csrfToken: string; csrfToken: string;
clickApiUrl: string; clickApiUrl: string;
customerBalance: number; customerBalance: number;
customerId: number; customerId: number;
products: Record<string, Product>; products: Record<string, Product>;
formInitial: InitialFormData[];
} }
interface Product { interface Product {
@ -48,8 +55,16 @@ exportToHtml("loadCounter", (config: CounterConfig) => {
codeField: undefined, codeField: undefined,
init() { init() {
// Fill the basket with the initial data
for (const entry of config.formInitial) {
if (entry.id !== undefined && entry.quantity !== undefined) {
this.addToBasket(entry.id, entry.quantity);
}
}
this.codeField = this.$refs.codeField; this.codeField = this.$refs.codeField;
this.codeField.widget.focus(); this.codeField.widget.focus();
// It's quite tricky to manually apply attributes to the management part // It's quite tricky to manually apply attributes to the management part
// of a formset so we dynamically apply it here // of a formset so we dynamically apply it here
this.$refs.basketManagementForm this.$refs.basketManagementForm

View File

@ -169,21 +169,36 @@
{{ super() }} {{ super() }}
<script> <script>
const products = { const products = {
{%- for p in products -%} {%- for product in products -%}
{{ p.id }}: { {{ product.id }}: {
id: "{{ p.id }}", id: "{{ product.id }}",
name: "{{ p.name }}", name: "{{ product.name }}",
price: {{ p.price }}, price: {{ product.price }},
hasTrayPrice: {{ p.tray | tojson }}, hasTrayPrice: {{ product.tray | tojson }},
}, },
{%- endfor -%} {%- endfor -%}
}; };
const formInitial = [
{%- for f in form -%}
{%- if f.cleaned_data -%}
{
{%- if f.cleaned_data["id"] -%}
id: '{{ f.cleaned_data["id"] | tojson }}',
{%- endif -%}
{%- if f.cleaned_data["quantity"] -%}
quantity: {{ f.cleaned_data["quantity"] | tojson }},
{%- endif -%}
},
{%- endif -%}
{%- endfor -%}
];
window.addEventListener("DOMContentLoaded", () => { window.addEventListener("DOMContentLoaded", () => {
loadCounter({ loadCounter({
csrfToken: "{{ csrf_token }}", csrfToken: "{{ csrf_token }}",
clickApiUrl: "{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}", customerBalance: {{ customer.amount }}, clickApiUrl: "{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}", customerBalance: {{ customer.amount }},
products: products, products: products,
customerId: {{ customer.pk }}, customerId: {{ customer.pk }},
formInitial: formInitial,
}); });
}); });
</script> </script>

View File

@ -117,12 +117,16 @@ class BaseBasketForm(BaseFormSet):
super().clean() super().clean()
if len(self) == 0: if len(self) == 0:
return return
if len(self.errors) > 0:
return
self._check_forms_have_errors()
self._check_recorded_products(self[0].customer) self._check_recorded_products(self[0].customer)
self._check_enough_money(self[0].counter, self[0].customer) self._check_enough_money(self[0].counter, self[0].customer)
def _check_forms_have_errors(self):
for form in self:
if len(form.errors):
raise ValidationError(_("Submmited basket is invalid"))
def _check_enough_money(self, counter: Counter, customer: Customer): def _check_enough_money(self, counter: Counter, customer: Customer):
self.total_price = sum([data["total_price"] for data in self.cleaned_data]) self.total_price = sum([data["total_price"] for data in self.cleaned_data])
if self.total_price > customer.amount: if self.total_price > customer.amount: