list collections

This commit is contained in:
Alexander Foremny
2024-06-06 22:52:33 +02:00
parent b1a4822d59
commit 3add980b73
3 changed files with 32 additions and 12 deletions

View File

@@ -221,6 +221,11 @@ main = do
respond $ respond $
W.responseLBS W.status200 [] $ W.responseLBS W.status200 [] $
J.encode (last repo.commits).schemaVersion J.encode (last repo.commits).schemaVersion
Right ListCollections -> do
repo <- atomically (readTMVar repoT)
respond $
W.responseLBS W.status200 [] $
J.encode (map (.path) (last repo.commits).collections)
(traceShowId -> !_) -> (traceShowId -> !_) ->
respond $ W.responseLBS W.status200 [] "not implemented" respond $ W.responseLBS W.status200 [] "not implemented"
@@ -228,13 +233,15 @@ data Route
= SchemaJson String = SchemaJson String
| Query | Query
| SchemaVersion | SchemaVersion
| ListCollections
deriving (Show) deriving (Show)
routeP :: P.Parser Route routeP :: P.Parser Route
routeP = routeP =
( P.choice ( P.choice
[ SchemaJson <$> (P.string "/" *> P.manyTill P.anyChar (P.string ".schema.json")), [ pure ListCollections <* P.string "/collections",
pure SchemaVersion <* P.string "/schemaVersion", pure SchemaVersion <* P.string "/schemaVersion",
SchemaJson <$> (P.string "/" *> P.manyTill P.anyChar (P.string ".schema.json")),
pure Query <* P.string "/" pure Query <* P.string "/"
] ]
) )

View File

@@ -1,7 +1,8 @@
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
module Api module Api
( fetchSchema, ( fetchCollections,
fetchSchema,
fetchSchemaVersion, fetchSchemaVersion,
fetchPosts, fetchPosts,
fetchPost, fetchPost,
@@ -28,14 +29,18 @@ import Safe
import Schema import Schema
import Version import Version
fetchSchema :: JSM (Either String Schema) fetchCollections :: JSM (Either String [String])
fetchSchema = fetchCollections =
A.eitherDecode <$> fetch (fromString "http://localhost:8081/posts.schema.json") A.eitherDecode <$> fetch (fromString "http://localhost:8081/collections")
fetchSchemaVersion :: JSM (Either String Version) fetchSchemaVersion :: JSM (Either String Version)
fetchSchemaVersion = fetchSchemaVersion =
A.eitherDecode <$> fetch (fromString "http://localhost:8081/schemaVersion") A.eitherDecode <$> fetch (fromString "http://localhost:8081/schemaVersion")
fetchSchema :: JSM (Either String Schema)
fetchSchema =
A.eitherDecode <$> fetch (fromString "http://localhost:8081/posts.schema.json")
fetchPosts :: JSM (Either String [A.Value]) fetchPosts :: JSM (Either String [A.Value])
fetchPosts = fetchPosts =
A.eitherDecode A.eitherDecode

View File

@@ -23,8 +23,9 @@ data Model
deriving (Show, Eq) deriving (Show, Eq)
data LoadedState = LoadedState data LoadedState = LoadedState
{ page :: Maybe (Either String Page), { collections :: [String],
schemaVersion :: Version schemaVersion :: Version,
page :: Maybe (Either String Page)
} }
deriving (Show, Eq) deriving (Show, Eq)
@@ -69,8 +70,10 @@ updateModel (Init uri) Loading =
Loading <# do Loading <# do
page <- Just <$> initialPage (parseURI uri) page <- Just <$> initialPage (parseURI uri)
schemaVersion' <- fetchSchemaVersion schemaVersion' <- fetchSchemaVersion
collections' <- fetchCollections
pure $ SetLoaded do pure $ SetLoaded do
schemaVersion <- schemaVersion' schemaVersion <- schemaVersion'
collections <- collections'
pure LoadedState {..} pure LoadedState {..}
updateModel (Init _) m = noEff m updateModel (Init _) m = noEff m
updateModel (SetLoaded (Left err)) Loading = noEff (Failed err) updateModel (SetLoaded (Left err)) Loading = noEff (Failed err)
@@ -98,7 +101,7 @@ viewModel (Loaded s) =
div_ [] $ div_ [] $
[ viewCss, [ viewCss,
viewHeader s, viewHeader s,
nav_ [] [viewCollections], nav_ [] [viewCollections s],
main_ [] $ main_ [] $
[ HandlePage <$> maybe (text "..") (either err viewPage) s.page [ HandlePage <$> maybe (text "..") (either err viewPage) s.page
] ]
@@ -208,12 +211,17 @@ viewBranch s =
select_ [] [option_ [] [text "main"]] select_ [] [option_ [] [text "main"]]
] ]
viewCollections :: View Action viewCollections :: LoadedState -> View Action
viewCollections = viewCollections s =
section_ [] $ section_ [] $
[ span_ [] [text "collections"], [ span_ [] [text "collections"],
ol_ [] $ ol_ [] $
[ li_ [] [a_ [href_ "#collection/posts"] [text "posts"]], [ li_
li_ [] [a_ [href_ "#collection/posts1"] [text "posts1"]] []
[ a_
[href_ (toMisoString ("#collection/" <> collection))]
[text (toMisoString collection)]
]
| collection <- s.collections
] ]
] ]