summaryrefslogtreecommitdiff
path: root/views.rkt
blob: 7dd6dbd2c63de3765af790520df900d6810b90f0 (plain)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#lang racket

(provide index-page
         ferti-index-page
         ferti-measurements-and-rotations-page
         ferti-recipe-page
         ferti-fertilizers-page
         ferti-crop-requirements-page
         new-measurement-page
         new-rotation-page
         new-fertilizer-page
         edit-measurement-page
         edit-fertilizer-page
         show-measurement-page
         show-rotation-page
         show-fertilizer-page
         show-crop-requirement-page
         new-crop-requirement-page
         edit-crop-requirement-page
         fallback-page)

(require gregor
         web-server/formlets
         "formlets.rkt"
         "models/user.rkt"
         "models/nutrient.rkt"
         "models/nutrient-value.rkt"
         "models/nutrient-measurement.rkt"
         "models/crop.rkt"
         "models/crop-rotation.rkt"
         "models/crop-requirement.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"])
                     "Facturation Pro"))
              (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"])
                     "Inventaire"))
              (li ((class "nav-item")) (a ((class "nav-link") [href "/contact"]) "Contact")))))))

;; Page helpers

(define (round n number)
  (~r number #:precision `(= ,n)))

(define (normal-date date-string)
  (~t (iso8601->date date-string) "dd/MM/yyyy"))

;; Ferti

(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-and-rotations"))
              "Relevés & Assolements"))
       (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/crop-requirements"))
              "Cultures"))))

(define (ferti-index-page)
  (ferti-template
   `((p "La recette Ferti© est calculée en fonction d'un relevé de nutriments et d'un assolement.")
     (div
      ((class "btn-group-vertical"))
      (a ((class "btn btn-outline-primary") [href "/ferti/measurements/new"]) "Ajouter un relevé")
      (a ((class "btn btn-outline-primary") [href "/ferti/rotations/new"]) "Ajouter un assolement")
      (a ((class "btn btn-outline-primary") [href "/ferti/fertilizers/new"]) "Ajouter un intrant")))))

(define (ferti-measurements-and-rotations-page measurements rotations)
  (define (maybe-rotation-for-measurement m)
    (findf (λ (r) (equal? (crop-rotation-date r) (nutrient-measurement-date m))) rotations))
  (define table
    `(table
      ((class "table"))
      (thead (tr (th "Date du relevé") (th "Relevé") (th "Assolement") (th "Recette")))
      (tbody
       ,@
       (for/list ([m measurements])
         (define maybe-rotation (maybe-rotation-for-measurement m))
         `(tr (td ((class "font-monospace align-middle")) ,(normal-date (nutrient-measurement-date m)))
              (td (a ((class "btn btn-outline-secondary btn-sm")
                      (href ,(format "/ferti/measurements/~a" (nutrient-measurement-id m))))
                     "Voir"))
              (td ,(if maybe-rotation
                       `(a ((class "btn btn-outline-secondary btn-sm")
                            (href ,(format "/ferti/rotations/~a" (crop-rotation-id maybe-rotation))))
                           "Voir")
                       `(a ((class "btn btn-outline-primary btn-sm")
                            (href ,(format "/ferti/rotations/new/~a" (nutrient-measurement-date m))))
                           "Ajouter")))
              (td ,(if maybe-rotation
                       `(a ((class "btn btn-outline-secondary btn-sm")
                            (href ,(format "/ferti/recipes/~a" (crop-rotation-date maybe-rotation))))
                           "Voir")
                       "—")))))))
  (ferti-template `((h2 () "Relevés et Assolements")
                    (div ((class "btn-group mb-3"))
                         (a ((class "btn btn-primary") [href "/ferti/measurements/new"])
                            "Ajouter un relevé"))
                    ,table)))

;; TODO: add bar chart for comparing to target concentrations
(define (ferti-recipe-page recipe-date fertilizer-recipe)
  (define table
    `(table ((class "table"))
            (thead (tr (th "Intrant") (th "Marque") (th ((class "text-end")) "Quantité (g)")))
            (tbody ,@(for/list ([(fertilizer quantity) (in-hash fertilizer-recipe)]
                                #:when (not (zero? quantity)))
                       (define canonical-name (fertilizer-product-name fertilizer))
                       (define brand-name (fertilizer-brand-name fertilizer))
                       `(tr (td ,canonical-name)
                            (td ,(if (non-empty-string? brand-name) brand-name "—"))
                            (td ((class "text-end font-monospace")) ,(round 2 (* 100 quantity))))))))
  (ferti-template `((h2 () ,(format "Recette du ~a" (normal-date recipe-date)))
                    (p "Quantités calculées pour 100'000 L.")
                    ,table)))

(define (ferti-fertilizers-page fertilizers)
  (define table
    `(table ((class "table table-striped"))
            (tr (th () "Nom de référence") (th () "Nom de marque"))
            ,@(for/list ([fp fertilizers])
                (define brand-name (fertilizer-brand-name fp))
                `(tr (td (a ([href ,(format "/ferti/fertilizers/~a" (fertilizer-product-id fp))])
                            ,(fertilizer-product-name fp)))
                     (td ,(if (and brand-name (non-empty-string? brand-name)) brand-name "—"))))))
  (ferti-template `((h2 () "Intrants") (a ((class "btn btn-primary mb-3") [href
                                                                           "/ferti/fertilizers/new"])
                                          "Ajouter un intrant")
                                       ,table)))

(define (ferti-crop-requirements-page crop-requirements)
  (define table
    `(table ((class "table table-striped"))
            (tr (th "Profil") (th "Culture"))
            ,@(for/list ([cr crop-requirements])
                (define crop-id (crop-requirement-crop-id cr))
                `(tr (td (a ((href ,(format "/ferti/crop-requirements/~a" (crop-requirement-id cr))))
                            ,(string-titlecase (crop-requirement-profile cr))))
                     (td ,(if crop-id
                              (string-titlecase (crop-name (get-crop #:id crop-id)))
                              "—"))))))
  (ferti-template `((h2 () "Profils de culture")
                    (a ((class "btn btn-primary mb-3") [href "/ferti/crop-requirements/new"])
                       "Ajouter un profil")
                    ,table)))

;; New

(define (form-page-template title action formlet)
  (page-template title
                 `((h1 ((class "display-1 mb-3")) ,title)
                   (div ((class "mb-3") [style "max-width: 30em"])
                        (form ([action ,action] [method "POST"]) ,@(formlet-display formlet))))))

(define (new-measurement-page)
  (form-page-template "Nouveau relevé" "/ferti/measurements/create" (measurements-formlet)))

(define (new-rotation-page #:date [date-string #f])
  (form-page-template "Nouvel assolement"
                      "/ferti/rotations/create"
                      (rotation-formlet #:date date-string)))

(define (new-fertilizer-page)
  (form-page-template "Nouvel intrant" "/ferti/fertilizers/create" (fertilizer-formlet)))

(define (new-crop-requirement-page)
  (form-page-template "Nouveau profil" "/ferti/crop-requirements/create" (crop-requirements-formlet)))

;; Edit

(define (edit-measurement-page nm)
  (form-page-template "Modifier le relevé"
                      "/ferti/measurements/update"
                      (measurements-formlet #:value nm)))

(define (edit-fertilizer-page fp)
  (form-page-template "Modifier intrant" "/ferti/fertilizers/update" (fertilizer-formlet #:value fp)))

(define (edit-crop-requirement-page cr)
  (form-page-template "Modifier profil"
                      "/ferti/crop-requirements/update"
                      (crop-requirements-formlet #:value cr)))

;; Show

(define (show-measurement-page nm)
  (define title (format "Relevé du ~a" (normal-date (nutrient-measurement-date nm))))
  (define table
    `(table ((class "table") (style "max-width: 30em"))
            (thead (tr (th "Nutriment") (th ((class "text-end")) "Concentration (mg/L)")))
            (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)))))))
  (define button-group
    `(div ((class "btn-group mb-3"))
          (a ((class "btn btn-primary")
              [href ,(format "/ferti/measurements/~a/edit" (nutrient-measurement-id nm))])
             "Modifier")
          (a ((class "btn btn-danger")
              [href ,(format "/ferti/measurements/~a/destroy" (nutrient-measurement-id nm))])
             "Supprimer")))
  (page-template title `((h1 ((class "display-1 mb-3")) ,title) ,button-group ,table)))

(define (show-rotation-page cr)
  (define id (crop-rotation-id cr))
  (define title (format "Assolement du ~a" (normal-date (crop-rotation-date cr))))
  (define table
    `(table ((class "table") (style "max-width: 30em"))
            (thead (tr (th "Type de culture") (th ((class "text-end")) "Proportion (%)")))
            (tbody ,@(for/list ([requirement (get-crop-requirements)])
                       (define requirement-proportion
                         (hash-ref (crop-rotation-requirements cr) requirement 0))
                       `(tr (td ,(crop-requirement-profile requirement))
                            (td ((class "text-end font-monospace"))
                                ,(round 2 requirement-proportion)))))))
  (define button-group
    `(div ((class "btn-group mb-3"))
          (a ((class "btn btn-primary") [href ,(format "/ferti/rotations/~a/edit" id)]) "Modifier")
          (a ((class "btn btn-danger")
              [href ,(format "/ferti/rotations/~a/destroy" (crop-rotation-id cr))])
             "Supprimer")))
  (page-template title `((h1 ((class "display-1 mb-3")) ,title) ,button-group ,table)))

(define (show-fertilizer-page fp)
  (define id (fertilizer-product-id fp))
  (define product-name (fertilizer-product-name fp))
  (define brand-name (fertilizer-brand-name fp))
  (define sorted-nutrient-values (get-sorted-nutrient-values (fertilizer-product-values fp)))
  (define table
    `(table ((class "table") (style "max-width: 30em"))
            (thead (tr (th "Nutriment") (th ((class "text-end")) "Concentration (mg/L)")))
            (tbody ,@(for/list ([nv-pair sorted-nutrient-values])
                       (match-define (cons n v) nv-pair)
                       `(tr (td ,(nutrient-french-name n))
                            (td ((class "text-end font-monospace")) ,(round 2 v)))))))
  (define button-group
    `(div ((class "btn-group mb-3"))
          (a ((class "btn btn-primary") [href ,(format "/ferti/fertilizers/~a/edit" id)]) "Modifier")
          (a ((class "btn btn-danger") [href ,(format "/ferti/fertilizers/~a/destroy" id)])
             "Supprimer")))
  (page-template product-name
                 `((h1 ((class "display-1 mb-3")) ,(or brand-name "Intrant générique"))
                   (h5 ((class "display-5 mb-3")) ,product-name)
                   ,button-group
                   ,table)))

(define (show-crop-requirement-page cr)
  (define id (crop-requirement-id cr))
  (define title (string-titlecase (crop-requirement-profile cr)))
  (define table
    `(table ((class "table") (style "max-width: 30em"))
            (thead (tr (th "Nutriment") (th ((class "text-end")) "Concentration (mg/L)")))
            (tbody ,@(for/list ([n (get-nutrients)])
                       (define nutrient-value (hash-ref (crop-requirement-values cr) n 0))
                       `(tr (td ,(nutrient-french-name n))
                            (td ((class "text-end font-monospace")) ,(round 2 nutrient-value)))))))
  (define button-group
    `(div ((class "btn-group"))
          (a ((class "btn btn-primary") [href ,(format "/ferti/crop-requirements/~a/edit" id)])
             "Modifier")
          (a ((class "btn btn-danger") [href ,(format "/ferti/crop-requirements/~a/destroy" id)])
             "Supprimer")))
  (page-template title `((h1 ((class "display-1 mb-3")) ,title) ,button-group ,table)))

(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)]))))
Copyright 2019--2026 Marius PETER