1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
#lang racket
(provide index-page
ferti-index-page
ferti-measurements-page
ferti-targets-page
ferti-recipe-page
ferti-fertilizers-page
new-measurement-page
new-target-page
new-fertilizer-page
show-measurement-page
show-target-page
show-fertilizer-page
fallback-page)
(require gregor
web-server/formlets
"formlets.rkt"
"models/user.rkt"
"models/nutrient.rkt"
"models/nutrient-measurement.rkt"
"models/nutrient-target.rkt"
"models/fertilizer-product.rkt")
(define (page-template title body-xexpr)
`(html (head (meta ([charset "utf-8"]))
(meta ([name "viewport"] [content "width=device-width, initial-scale=1"]))
(title ,title)
;; Bootstrap CSS
(link ([href "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"]
[rel "stylesheet"]
[integrity
"sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"]
[crossorigin "anonymous"])))
(body ,navbar
(div ((class "container")) ,@body-xexpr)
;; Bootstrap JS bundle
(script
([src "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"]
[integrity "sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"]
[crossorigin "anonymous"])))))
;; Page components
(define navbar
'(nav
((class "navbar navbar-expand-lg navbar-light bg-light"))
(div
((class "container-fluid"))
(a ((class "navbar-brand") [href "/"]) "FAPG")
(button ((class "navbar-toggler") [type "button"]
[data-bs-toggle "collapse"]
[data-bs-target "#navbarSupportedContent"]
[aria-controls "navbarSupportedContent"]
[aria-expanded "false"]
[aria-label "Toggle navigation"])
(span ((class "navbar-toggler-icon"))))
(div ((class "collapse navbar-collapse") [id "navbarSupportedContent"])
(ul ((class "navbar-nav me-auto mb-2 mb-lg-0"))
(li ((class "nav-item"))
(a ((class "nav-link disabled") [href "#"] [tabindex "-1"] [aria-disabled "true"])
"Clients"))
(li ((class "nav-item"))
(a ((class "nav-link active") [aria-current "page"] [href "/ferti/index"]) "Ferti"))
(li ((class "nav-item"))
(a ((class "nav-link disabled") [href "#"] [tabindex "-1"] [aria-disabled "true"])
"Cultures"))
(li ((class "nav-item")) (a ((class "nav-link") [href "/contact"]) "Contact")))))))
;; Page helpers
(define (round n number)
(~r number #:precision `(= ,n)))
;; Pages
(define (ferti-template body-xexpr)
(page-template "Ferti" `((h1 ((class "display-1 mb-3")) "Ferti") ,ferti-tabs ,@body-xexpr)))
(define ferti-tabs
'(ul ((class "nav nav-tabs mb-3"))
(li ((class "nav-item"))
(a ((class "nav-link") (aria-current "page") (href "/ferti/index")) "Accueil"))
(li ((class "nav-item"))
(a ((class "nav-link") (aria-current "page") (href "/ferti/measurements")) "Relevés"))
(li ((class "nav-item"))
(a ((class "nav-link") (aria-current "page") (href "/ferti/targets")) "Cibles"))
(li ((class "nav-item"))
(a ((class "nav-link") (aria-current "page") (href "/ferti/fertilizers")) "Intrants"))
(li ((class "nav-item"))
(a ((class "nav-link") (aria-current "page") (href "/ferti/recipe")) "Recette Ferti©"))))
(define (ferti-index-page)
(ferti-template
`((p "La recette Ferti© est calculée en fonction d'un relevé de nutriments et d'une cible.")
(div ((class "btn-group-vertical"))
(a ((class "btn btn-outline-primary") [href "/ferti/measurement/new"]) "Ajouter un relevé")
(a ((class "btn btn-outline-primary") [href "/ferti/target/new"]) "Créer une cible")
(a ((class "btn btn-outline-primary") [href "/ferti/fertilizer/new"])
"Ajouter un intrant")))))
(define (ferti-measurements-page nutrients measurements)
(define table
`(table ((class "table"))
(thead (tr (th "Date")))
(tbody ,@(for/list ([m measurements])
`(tr (td (a ((href ,(format "/ferti/measurement/~a"
(nutrient-measurement-id m))))
,(nutrient-measurement-date m))))))))
(ferti-template `((h2 () "Relevés") (a ((class "btn btn-primary mb-3") [href
"/ferti/measurement/new"])
"Ajouter un relevé")
,table)))
(define (ferti-targets-page latest-measurement-hash latest-target-hash)
(define table
`(table ((class "table"))
(thead (tr (th "Nutriment")
(th ((class "text-end")) "Dernier Relevé")
(th ((class "text-end")) "Dernière Cible")))
(tbody ,@(for/list ([n (get-nutrients)])
(define latest-measurement (hash-ref latest-measurement-hash n #f))
(define latest-target (hash-ref latest-target-hash n #f))
`(tr (td ,(nutrient-french-name n))
(td ((class "text-end font-monospace"))
,(if latest-measurement
(round 2 latest-measurement)
"—"))
(td ((class "text-end font-monospace"))
,(if latest-target
(round 2 latest-target)
"—")))))))
(ferti-template `((h2 () "Dernière Cible")
(a ((class "btn btn-primary mb-3") [href "/ferti/target/new"]) "Créer une cible")
,table)))
(define (ferti-recipe-page fertilizer-recipe)
(define table
`(table ((class "table"))
(thead (tr (th "Intrant") (th ((class "text-end")) "Quantité (g/L)")))
(tbody ,@(for/list ([fertilizer-amount fertilizer-recipe]
#:when (not (zero? (cdr fertilizer-amount))))
(match-define (cons fertilizer amount) fertilizer-amount)
`(tr (td ()
,(let ([canonical-name (fertilizer-name fertilizer)]
[brand-name (fertilizer-brand-name fertilizer)])
(if brand-name
(format "~a (~a)" brand-name canonical-name)
canonical-name)))
(td ((class "text-end font-monospace")) ,(round 2 amount)))))))
(ferti-template `((h2 () "Recette")
,(if (ormap (λ (pair) (not (zero? (cdr pair)))) fertilizer-recipe)
table
`(p "La recette Ferti requiert au moins un relevé et une cible.")))))
(define (ferti-fertilizers-page fertilizers)
(define (delete-button-for fp)
`(a ((class "btn btn-outline-danger btn-sm")
[href ,(format "/ferti/fertilizer/destroy/~a" (fertilizer-product-id fp))])
"Supprimer"))
(define table
`(table ((class "table table-striped"))
(tr (th () "Nom de référence") (th () "Nom de marque") (th () "Actions"))
,@(for/list ([fertilizer fertilizers])
`(tr (td ,(fertilizer-name fertilizer))
(td ,(or (fertilizer-brand-name fertilizer) "—"))
(td ,(delete-button-for fertilizer))))))
(ferti-template `((h2 () "Intrants") (a ((class "btn btn-primary mb-3") [href
"/ferti/fertilizer/new"])
"Ajouter un intrant")
,table)))
(define (new-measurement-page)
(page-template "Nouveau relevé"
`((h1 ((class "display-1 mb-3")) "Nouveau relevé")
(div ((class "mb-3") [style "max-width: 30em"])
(form ([action "/ferti/measurement/create"] [method "POST"])
,@(formlet-display (measurements-formlet)))))))
(define (new-target-page)
(page-template "Nouvelle cible"
`((h1 ((class "display-1 mb-3")) "Nouvelle cible")
(div ((class "mb-3") [style "max-width: 30em"])
(form ([action "/ferti/target/create"] [method "POST"])
,@(formlet-display (targets-formlet)))))))
(define (new-fertilizer-page)
(page-template "Nouvel intrant"
`((h1 ((class "display-1 mb-3")) "Nouvel intrant")
(div ((class "mb-3") [style "max-width: 30em"])
(form ([action "/ferti/fertilizer/create"] [method "POST"])
,@(formlet-display (fertilizer-formlet)))))))
(define (show-measurement-page nm)
(define title (format "Relevé du ~a" (nutrient-measurement-date nm)))
(define table
`(table ((class "table"))
(thead (tr (th "Nutriment") (th ((class "text-end")) "Valeur de ce relevé")))
(tbody ,@(for/list ([n (get-nutrients)])
(define nutrient-value (hash-ref (nutrient-measurement-values nm) n 0))
`(tr (td ,(nutrient-french-name n))
(td ((class "text-end font-monospace")) ,(round 2 nutrient-value)))))))
(page-template title `((h1 ((class "display-1 mb-3")) ,title) ,table)))
(define (show-target-page nt)
(page-template (format "Cible ~a" (nutrient-target-id nt))
'((h1 ((class "display-1 mb-3")) "Cible"))))
(define (show-fertilizer-page fp)
(page-template (format "Intrant ~a" (fertilizer-product-id fp))
'((h1 ((class "display-1 mb-3")) "Intrant"))))
(define (index-page user)
(page-template
"Bienvenue à la FAPG"
`((div ((class "jumbotron"))
(h1 ((class "display-1 mb-3"))
,(string-join (list (if (<= (->hours (current-time)) 17) "Bonjour" "Bonsoir")
(if user
(user-name user)
"et bienvenue"))))
(hr ((class "my-3")))
(p ((class "lead")) ,(~t (now) "'Dernière connexion à' HH:mm, 'le' EEEE d MMMM yyyy"))
(p ((class "lead"))
(a ((class "btn btn-primary mb-3") [href "/ferti/index"]) "Accéder à Ferti"))))))
(define (fallback-page request-code)
(page-template (format "Réponse: ~a" request-code)
`((h1 ((class "display-1 text-danger")) ,(number->string request-code))
(p ,(fallback-message request-code))
(a ([href "/"]) "Revenir à la page d'accueil"))))
(define (fallback-message request-code)
(string-join
`("Bonjour, je suis votre serveur."
,(format "J'ai répondu '~a'" request-code)
"et"
,(case (string-ref (number->string request-code) 0)
[(#\4) "c'est de votre faute."]
[(#\5) "c'est de ma faute."]
[else "je ne sais pas qui est en tort."])
,(case request-code
;; Client errors
[(400) "Votre requête ne fait pas sens."]
[(401) "Vous n'avez pas vérifié votre identité."]
[(403) "Vous n'avez pas le droit de consulter cette page."]
[(404) "Vous avez demandé de consulter une page qui n'existe pas."]
;; Server errors
[(500)
"Je suis dans une situation que je ne sais pas gérer, et ne peux vous en dire davantage."]
[(502)
"Un tiers ne m'a pas transmis les informations nécessaires pour répondre à votre requête."]
[(503)
"Je ne peux pas vous aider, il se peut que je sois momentanément surchargé. Revenez plus tard."]
;; Fallback message
[else (format "Je ne sais pas encore interpréter le code ~a." request-code)]))))
|