aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Page
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-10-11 23:30:56 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-10-11 23:30:56 +0200
commit80a6150610182eefa0deb1f0932d3b780456ca09 (patch)
tree4471a8ffecfc527d6b9c2a5c48e445e7a4d6a74f /frontend/app/Page
parent2e0cf98254976e443ea7f693961fc105ed6cf563 (diff)
use backend REST library for frontend
Diffstat (limited to 'frontend/app/Page')
-rw-r--r--frontend/app/Page/EditValue.hs40
-rw-r--r--frontend/app/Page/ListCollection.hs18
-rw-r--r--frontend/app/Page/NewCollection.hs19
3 files changed, 40 insertions, 37 deletions
diff --git a/frontend/app/Page/EditValue.hs b/frontend/app/Page/EditValue.hs
index cdb1dd0..cf8ef50 100644
--- a/frontend/app/Page/EditValue.hs
+++ b/frontend/app/Page/EditValue.hs
@@ -7,7 +7,8 @@ module Page.EditValue
)
where
-import Api
+import ACMS.API.REST.Collection qualified as API.REST.Collection
+import Control.Monad.Catch (SomeException, try)
import Data.Aeson qualified as A
import Data.Aeson.KeyMap qualified as AM
import Data.ByteString.Lazy.UTF8 as LB
@@ -15,21 +16,22 @@ import Data.Maybe
import Effect (Eff)
import Form qualified as F
import Miso
-import Miso.String (toMisoString)
+import Miso.String (MisoString, toMisoString)
+import Safe (headMay)
import Schema
data Model = Model
- { collection :: String,
- fileName :: String,
- input :: Maybe A.Value,
+ { collection :: MisoString,
+ fileName :: MisoString,
+ input :: Maybe A.Object,
schema :: Schema
}
deriving (Show, Eq)
-initialModel :: String -> String -> JSM (Either String Model)
+initialModel :: MisoString -> MisoString -> JSM (Either SomeException Model)
initialModel collection fileName = do
- schema' <- fetchSchema
- input' <- fetchPost fileName
+ schema' <- try (API.REST.Collection.schema collection)
+ input' <- try (headMay <$> API.REST.Collection.read collection fileName)
pure do
schema <- schema'
input <- input'
@@ -37,14 +39,14 @@ initialModel collection fileName = do
newtype Action = Action (Model -> (Effect Action Model, [Eff]))
-update__formChanged :: A.Value -> Action
+update__formChanged :: A.Object -> Action
update__formChanged (Just -> input) = Action $ \m -> (noEff m {input}, [])
-update__formSubmitted :: A.Value -> Action
+update__formSubmitted :: A.Object -> Action
update__formSubmitted output = Action $ \m ->
- (m <# do update__entityWritten <$> updatePost m.fileName output, [])
+ (m <# do update__entityWritten <$> try (API.REST.Collection.update m.collection m.fileName output), [])
-update__entityWritten :: Either String () -> Action
+update__entityWritten :: Either SomeException () -> Action
update__entityWritten _ = Action $ \m -> (noEff m, [])
updateModel :: Action -> Model -> (Effect Action Model, [Eff])
@@ -52,29 +54,27 @@ updateModel (Action f) m = f m
viewModel :: Model -> View Action
viewModel m = do
- let input = (fromMaybe (A.Object AM.empty) m.input)
+ let input = (fromMaybe AM.empty m.input)
div_ [] $
[ viewForm input m.schema,
viewInput input,
viewOutput input m.schema
]
-viewForm :: A.Value -> Schema -> View Action
+viewForm :: A.Object -> Schema -> View Action
viewForm input =
fmap (either update__formChanged update__formSubmitted)
. flip F.runForm input
. schemaForm
-viewInput :: A.Value -> View Action
+viewInput :: A.Object -> View Action
viewInput input =
pre_ [] [text (toMisoString (A.encode input))]
-viewOutput :: A.Value -> Schema -> View Action
+viewOutput :: A.Object -> Schema -> View Action
viewOutput input schema =
pre_ [] $
[ text $
- toMisoString
- ( either ("Left " <>) (("Right " <>) . LB.toString) $
- (A.encode <$> ((schemaForm schema).fill input))
- )
+ either ("Left " <>) (("Right " <>)) $
+ (toMisoString . A.encode <$> ((schemaForm schema).fill input))
]
diff --git a/frontend/app/Page/ListCollection.hs b/frontend/app/Page/ListCollection.hs
index 9acca3c..47a4649 100644
--- a/frontend/app/Page/ListCollection.hs
+++ b/frontend/app/Page/ListCollection.hs
@@ -7,29 +7,31 @@ module Page.ListCollection
)
where
-import Api
+import ACMS.API.REST.Collection qualified as API.REST.Collection
+import Control.Monad.Catch (SomeException, try)
import Data.Aeson qualified as A
import Data.Aeson.KeyMap qualified as AM
import Effect (Eff)
import Miso
+import Miso.String (MisoString)
import Schema
data Model = Model
- { collection :: String,
- input :: A.Value,
+ { collection :: MisoString,
+ input :: A.Object,
schema :: Schema,
- posts :: [A.Value]
+ posts :: [A.Object]
}
deriving (Show, Eq)
-initialModel :: String -> JSM (Either String Model)
+initialModel :: MisoString -> JSM (Either SomeException Model)
initialModel collection = do
- schema' <- fetchSchema
- posts' <- fetchPosts
+ schema' <- try (API.REST.Collection.schema collection)
+ posts' <- try (API.REST.Collection.list collection)
pure do
schema <- schema'
posts <- posts'
- pure $ Model {input = A.Object AM.empty, ..}
+ pure $ Model {input = AM.empty, ..}
newtype Action = Action (Model -> (Effect Action Model, [Eff]))
diff --git a/frontend/app/Page/NewCollection.hs b/frontend/app/Page/NewCollection.hs
index 12b9cf1..a15d4a7 100644
--- a/frontend/app/Page/NewCollection.hs
+++ b/frontend/app/Page/NewCollection.hs
@@ -7,37 +7,38 @@ module Page.NewCollection
)
where
-import Api
+import ACMS.API.REST qualified as API.REST
+import Control.Monad.Catch (SomeException, try)
import Data.Aeson qualified as A
import Data.Text qualified as T
import Effect (Eff)
import Effect qualified as E
import Form qualified as F
import Miso
-import Miso.String (toMisoString)
+import Miso.String (MisoString, toMisoString)
data Model = Model
- { input :: T.Text
+ { input :: MisoString
}
deriving (Show, Eq)
-initialModel :: JSM (Either String Model)
+initialModel :: JSM (Either SomeException Model)
initialModel = do
pure (Right (Model {input = ""}))
newtype Action = Action (Model -> (Effect Action Model, [Eff]))
-update__formChanged :: T.Text -> Action
+update__formChanged :: MisoString -> Action
update__formChanged input = Action $ \m -> (noEff m {input}, [])
-update__formSubmitted :: T.Text -> Action
+update__formSubmitted :: MisoString -> Action
update__formSubmitted collection = Action $ \m ->
( m <# do
- update__collectionCreated <$> createCollection (T.unpack collection),
+ update__collectionCreated <$> try (API.REST.createCollection collection),
[]
)
-update__collectionCreated :: Either String () -> Action
+update__collectionCreated :: Either SomeException () -> Action
update__collectionCreated _ = Action $ \m -> (noEff m, [E.ReloadCollections])
updateModel :: Action -> Model -> (Effect Action Model, [Eff])
@@ -53,6 +54,6 @@ viewModel m = do
pre_ [] [text (toMisoString (A.encode (collectionForm.fill m.input)))]
]
-collectionForm :: F.Form T.Text T.Text
+collectionForm :: F.Form MisoString MisoString
collectionForm =
F.input "name"