From 6e8d8dc281888db9d8dfe57bde92e1e286c48dc1 Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Sat, 29 Nov 2025 19:26:43 +0100 Subject: Complete parent commit. --- formlets.rkt | 68 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 29 deletions(-) (limited to 'formlets.rkt') diff --git a/formlets.rkt b/formlets.rkt index 73db14b..bc31519 100644 --- a/formlets.rkt +++ b/formlets.rkt @@ -5,45 +5,50 @@ fertilizer-formlet) (require gregor - web-server/http web-server/formlets "models/nutrient.rkt" "models/crop.rkt" "models/crop-requirement.rkt") -(define date-formlet - (to-string (required (input #:type "date" - #:value (date->iso8601 (today)) - #:attributes '((class "form-control") [required "required"]))))) - -(define (nutrient-value-formlet nutrient) - (define id (number->string (nutrient-id nutrient))) - (define number-input - (input #:type "number" - #:attributes `((class "form-control") [id ,id] - [step "0.1"] - [placeholder ,(nutrient-french-name nutrient)]))) - (define input-label - `(label ((for ,id - )) - ,(nutrient-french-name nutrient))) - (formlet (#%# (div ((class "form-floating mb-3")) ,{=> number-input nutrient-value-b} ,input-label)) - (let ([nutrient-value (string->number (bytes->string/utf-8 - (binding:form-value nutrient-value-b)))]) - (and nutrient-value (cons nutrient nutrient-value))))) - (define (measurements-formlet) (formlet* (#%# `(div ((class "mb-3")) (h5 "Date du relevé") ,{=>* date-formlet measured-on*}) `(div ((class "mb-3")) (h5 "Valeurs du relevé") ,@(for/list ([nutrient (get-nutrients)]) - {=>* (nutrient-value-formlet nutrient) measurements*})) + {=>* (nutrient-value-formlet nutrient) nutrient-values*})) {=>* (submit "Enregistrer le relevé" #:attributes '((class "btn btn-primary"))) _}) (let ([measured-on (first measured-on*)] - [nutrient-values (for/hash ([nv (in-list (filter pair? measurements*))]) + [nutrient-values (for/hash ([nv nutrient-values*]) (values (car nv) (cdr nv)))]) (values measured-on nutrient-values)))) +(define (targets-formlet) + (formlet* (#%# `(div ((class "mb-3")) (h5 "Date ciblée") ,{=>* date-formlet effective-on*}) + `(div ((class "mb-3")) + (h5 "Valeurs cibles (%)") + ,@(for/list ([requirement (get-crop-requirements)]) + {=>* (crop-requirement-formlet requirement) requirements*})) + {=>* (submit "Enregistrer la cible" #:attributes '((class "btn btn-primary"))) _}) + (let ([effective-on (first effective-on*)] + [nutrient-values (average-crop-requirement-nutrient-values (filter pair? + requirements*))]) + (values effective-on nutrient-values)))) + +(define (fertilizer-formlet) + (formlet* + (#%# `(div ((class "mb-3")) (h5 "Nom de référence") ,{=>* required-string-input canonical-name*}) + `(div ((class "mb-3")) (h5 "Nom de marque") ,{=>* required-string-input brand-name*}) + `(div ((class "mb-3")) + (h5 "Valeurs de l'intrant") + ,@(for/list ([nutrient (get-nutrients)]) + {=>* (nutrient-value-formlet nutrient) nutrient-values*})) + {=>* (submit "Enregistrer l'intrant" #:attributes '((class "btn btn-primary"))) _}) + (let ([canonical-name (first canonical-name*)] + [nutrient-values (for/hash ([nv nutrient-values*]) + (values (car nv) (cdr nv)))] + [brand-name (first brand-name*)]) + (values canonical-name brand-name nutrient-values)))) + (define (crop-requirement-formlet requirement) (define id (number->string (crop-requirement-id requirement))) (define profile (crop-requirement-profile requirement)) @@ -52,11 +57,16 @@ (if maybe-crop (crop-name (get-crop #:id maybe-crop)) #f)) - (define number-input - (input - #:type "number" - #:attributes - `((class "form-control") [id ,id] [min "0"] [max "100"] [step "1"] [placeholder ,profile]))) + (define percentage-input + (to-number (to-string (required (input #:type "number" + #:attributes + `((class "form-control") [required "required"] + [id ,id] + [name ,id] + [min "0"] + [max "100"] + [step "1"] + [placeholder ,profile])))))) (define input-label `(label ((for ,id )) -- cgit v1.2.3