Refactor a tiny bit

This commit is contained in:
towards-a-new-leftypol 2023-11-15 16:48:43 -05:00
parent 2ac0d249b1
commit 7a09471bd7
4 changed files with 25 additions and 18 deletions

View File

@ -26,6 +26,7 @@ to generate this file without the comments in this block.
, "halogen" , "halogen"
, "js-date" , "js-date"
, "maybe" , "maybe"
, "now"
, "prelude" , "prelude"
, "undefined" , "undefined"
, "web-dom" , "web-dom"

View File

@ -6,6 +6,7 @@ import Data.Maybe (Maybe(..))
import Effect (Effect) import Effect (Effect)
import Effect.Aff (Aff, launchAff) import Effect.Aff (Aff, launchAff)
import Effect.Console (log) import Effect.Console (log)
import Effect.Now (nowDateTime)
import Halogen import Halogen
( HalogenM ( HalogenM
, ComponentHTML , ComponentHTML
@ -102,7 +103,8 @@ main = do
Just pgroot -> do Just pgroot -> do
let settings = { postgrest_api_root: pgroot } let settings = { postgrest_api_root: pgroot }
log $ show settings log $ show settings
_ <- launchAff $ Client.searchPosts settings "TRUE CHRISTIAN" now <- nowDateTime
_ <- launchAff $ Client.fetchCatalog settings now 1001
run run
where where

View File

@ -1,8 +1,7 @@
module Network.Client module Network.Client
( SearchResultJSON ( SearchResultJSON
, SearchResult
, searchPosts , searchPosts
, fetch_catalog , fetchCatalog
) )
where where
@ -28,7 +27,7 @@ import Data.Traversable (for)
import Effect (Effect) import Effect (Effect)
import Effect.Aff (Aff) import Effect.Aff (Aff)
import Halogen (liftEffect) import Halogen (liftEffect)
import Types (Settings) import Types (Settings, SearchResult)
import Undefined (undefined) import Undefined (undefined)
import Web.HTML (window) import Web.HTML (window)
import Web.HTML.Window (alert) import Web.HTML.Window (alert)
@ -44,17 +43,6 @@ type SearchResultJSON =
, thread_id :: Number , thread_id :: Number
} }
type SearchResult =
{ post_id :: Number
, board_post_id :: Number
, creation_time :: DateTime
, body :: Maybe String
, subject :: Maybe String
, name :: String
, email :: Maybe String
, thread_id :: Number
}
type CatalogResultJSON = type CatalogResultJSON =
{ post_count :: Number { post_count :: Number
, estimated_post_count :: Number , estimated_post_count :: Number
@ -63,7 +51,7 @@ type CatalogResultJSON =
, creation_time :: String , creation_time :: String
, bump_time :: String , bump_time :: String
, body :: String , body :: String
, subject :: String , subject :: Maybe String
, thread_id :: Number , thread_id :: Number
, board_thread_id :: Number , board_thread_id :: Number
, pathpart :: String , pathpart :: String
@ -75,6 +63,7 @@ type CatalogResultJSON =
id :: forall a. a -> a id :: forall a. a -> a
id x = x id x = x
-- | Check if HTTP status code is in the OK range.
ok :: Int -> Boolean ok :: Int -> Boolean
ok = between 200 299 ok = between 200 299
@ -82,6 +71,7 @@ ok = between 200 299
status :: forall a. Response a -> Int status :: forall a. Response a -> Int
status = (\(StatusCode c) -> c) <<< _.status status = (\(StatusCode c) -> c) <<< _.status
searchResultFromJson :: SearchResultJSON -> Effect SearchResult searchResultFromJson :: SearchResultJSON -> Effect SearchResult
searchResultFromJson j = do searchResultFromJson j = do
date <- getDate j.creation_time date <- getDate j.creation_time
@ -155,8 +145,8 @@ searchPosts settings search_query = do
payload = encodeJson { search_text: search_query } payload = encodeJson { search_text: search_query }
fetch_catalog :: Settings -> DateTime -> Int -> Aff (Maybe (Array CatalogResultJSON)) fetchCatalog :: Settings -> DateTime -> Int -> Aff (Maybe (Array CatalogResultJSON))
fetch_catalog settings max_time rows_to_read = do fetchCatalog settings max_time rows_to_read = do
max_time_iso_str <- liftEffect $ (toISOString <<< fromDateTime) max_time max_time_iso_str <- liftEffect $ (toISOString <<< fromDateTime) max_time
post_ settings "/rpc/fetch_catalog" (payload max_time_iso_str) post_ settings "/rpc/fetch_catalog" (payload max_time_iso_str)

View File

@ -1,8 +1,22 @@
module Types module Types
( Settings ( Settings
, SearchResult
) )
where where
import Data.DateTime (DateTime)
import Data.Maybe (Maybe)
type Settings = { postgrest_api_root :: String } type Settings = { postgrest_api_root :: String }
type SearchResult =
{ post_id :: Number
, board_post_id :: Number
, creation_time :: DateTime
, body :: Maybe String
, subject :: Maybe String
, name :: String
, email :: Maybe String
, thread_id :: Number
}