diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-10-11 23:30:56 +0200 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-10-11 23:30:56 +0200 |
commit | 80a6150610182eefa0deb1f0932d3b780456ca09 (patch) | |
tree | 4471a8ffecfc527d6b9c2a5c48e445e7a4d6a74f /frontend/app/Page/EditValue.hs | |
parent | 2e0cf98254976e443ea7f693961fc105ed6cf563 (diff) |
use backend REST library for frontend
Diffstat (limited to 'frontend/app/Page/EditValue.hs')
-rw-r--r-- | frontend/app/Page/EditValue.hs | 40 |
1 files changed, 20 insertions, 20 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)) ] |