module Page.ListCollection ( Model, initialModel, Action, updateModel, viewModel, ) where import ACMS.API.REST.Collection qualified as API.REST.Collection import ACMS.API.REST.Collection.Paginated (Paginated (..)) import ACMS.API.REST.Collection.Paginated qualified as API.REST.Collection.Paginated import 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 Schema data Model = Model { collection :: Collection, input :: A.Object, schema :: Schema, posts :: Paginated A.Object } deriving (Show, Eq) initialModel :: Collection -> JSM (Either SomeException Model) initialModel collection = do schema' <- try (API.REST.Collection.schema collection) posts' <- try (API.REST.Collection.Paginated.list (API.REST.Collection.Paginated.Pagination 10 0) collection) pure do schema <- schema' posts <- posts' pure $ Model {input = AM.empty, ..} newtype Action = Action (Model -> (Effect Action Model, [Eff])) updateModel :: Action -> Model -> (Effect Action Model, [Eff]) updateModel (Action f) m = f m viewModel :: Model -> View Action viewModel m = div_ [] $ [ h3_ [] [text m.collection.name], schemaTable m.collection.name m.schema m.posts, h3_ [] [text "schema"], viewSchema m.schema ]