From 0f838c2e3858e0f151f33412efc27c4701a434c5 Mon Sep 17 00:00:00 2001
From: Alexander Foremny <aforemny@posteo.de>
Date: Fri, 20 Dec 2024 17:52:43 +0100
Subject: display date-time

---
 frontend/app/Schema.hs  | 31 ++++++++++++++++++++++---------
 frontend/frontend.cabal |  1 +
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/frontend/app/Schema.hs b/frontend/app/Schema.hs
index be04906..8c02a1a 100644
--- a/frontend/app/Schema.hs
+++ b/frontend/app/Schema.hs
@@ -18,6 +18,10 @@ import Data.Map qualified as M
 import Data.Maybe
 import Data.Scientific (fromFloatDigits)
 import Data.Set qualified as S
+import Data.Text qualified as T
+import Data.Time.Clock
+import Data.Time.Format
+import Data.Time.Format.ISO8601
 import Form qualified as F
 import Miso
 import Miso.String (MisoString, fromMisoString, intercalate, toMisoString)
@@ -51,14 +55,14 @@ instance A.FromJSONKey MisoString where
 #endif
 
 data Property
-  = Type MisoString
+  = Type MisoString (Maybe MisoString)
   | Reference MisoString
   | Union [MisoString]
   deriving (Show, Eq)
 
 instance A.FromJSON Property where
   parseJSON = A.withObject "Property" $ \v ->
-    (Type <$> v A..: "type")
+    (Type <$> v A..: "type" <*> v A..:? "format")
       <|> (Reference <$> v A..: "$ref")
       <|> (fmap Union $ traverse (A..: "type") =<< v A..: "oneOf")
 
@@ -71,7 +75,9 @@ viewSchema schema =
             text ":",
             text
               ( case v of
-                  Type v -> toMisoString v
+                  Type "string" (Just "date-time") -> "datetime"
+                  Type v Nothing -> toMisoString v
+                  Type v (Just f) -> toMisoString v <> " (" <> f <> ")"
                   Reference v -> "reference to " <> toMisoString v
                   Union vs -> Miso.String.intercalate " or " vs
               ),
@@ -96,21 +102,28 @@ schemaTable collection schema paginated =
         [ tr_
             []
             [ td_ [] $
-                [ case (k, getO (AK.fromText (fromMisoString k)) value) of
-                    ("$fileName", A.String fn) ->
+                [ case (k, p, getO (AK.fromText (fromMisoString k)) value) of
+                    ("$fileName", _, A.String fn) ->
                       a_
                         [ href_
                             (routeToMisoString (EditValue collection (toMisoString fn)))
                         ]
                         [ text (toMisoString fn)
                         ]
-                    (_, v) ->
+                    (_, Type "string" (Just "date-time"), v) ->
+                      text $
+                        case v of
+                          A.String
+                            (iso8601ParseM @Maybe @UTCTime . T.unpack -> Just t) ->
+                              toMisoString (formatTime defaultTimeLocale "%c" t)
+                          _ -> toMisoString (A.encode v)
+                    (_, _, v) ->
                       text $
                         case v of
                           A.String s -> toMisoString s
                           _ -> toMisoString (A.encode v)
                 ]
-              | k <- M.keys schema.properties
+              | (k, p) <- M.assocs schema.properties
             ]
           | value <- paginated.data_
         ]
@@ -132,7 +145,7 @@ schemaForm schema =
   fmap mergeJson . sequence . catMaybes $
     ( \((AK.fromText . fromMisoString) -> k, v) ->
         case v of
-          Type "string" ->
+          Type "string" Nothing ->
             Just $
               if toMisoString (AK.toText k) `S.member` schema.required
                 then
@@ -148,7 +161,7 @@ schemaForm schema =
                               . F.mapValues fromJson toJson
                             $ F.optional (F.inputText (toMisoString (AK.toString k)))
                         )
-          Type "number" ->
+          Type "number" Nothing ->
             Just $
               if toMisoString (AK.toText k) `S.member` schema.required
                 then
diff --git a/frontend/frontend.cabal b/frontend/frontend.cabal
index baa11e4..c4affab 100644
--- a/frontend/frontend.cabal
+++ b/frontend/frontend.cabal
@@ -50,6 +50,7 @@ executable frontend
         scientific,
         split,
         text,
+        time,
         utf8-string
 
     if !arch(javascript)
-- 
cgit v1.2.3