Search WiP: hook Search into Client API, but don't call it yet
This commit is contained in:
parent
f04fd2e591
commit
cd72af095e
4
TODO.txt
4
TODO.txt
|
@ -1,7 +1,7 @@
|
||||||
- get embeds working
|
- get embeds working ✓
|
||||||
- need to implement search
|
- need to implement search
|
||||||
- change urls / history when time-travelling
|
- change urls / history when time-travelling
|
||||||
- remove duplicate threads from view (duplicate because the OP has multiple pictures)
|
- remove duplicate threads from view (duplicate because the OP has multiple pictures) ✓
|
||||||
- server-side rendering
|
- server-side rendering
|
||||||
- control to manually put in the datetime instead of using the slider
|
- control to manually put in the datetime instead of using the slider
|
||||||
for fine-grained control
|
for fine-grained control
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
|
|
||||||
module Component.Search
|
module Component.Search
|
||||||
( view
|
( view
|
||||||
|
@ -25,26 +26,45 @@ import Miso
|
||||||
, consoleLog
|
, consoleLog
|
||||||
, noEff
|
, noEff
|
||||||
)
|
)
|
||||||
import GHCJS.DOM.Types (JSString)
|
import Data.JSString (JSString, pack)
|
||||||
|
import qualified Network.ClientTypes as Client
|
||||||
|
import Network.CatalogPostType (CatalogPost)
|
||||||
|
import Network.Http (HttpResult (..))
|
||||||
|
|
||||||
data Action = SearchChange JSString | OnSubmit | NoAction
|
data Action
|
||||||
|
= SearchChange JSString
|
||||||
|
| OnSubmit
|
||||||
|
| SearchResult (HttpResult [ CatalogPost ])
|
||||||
|
| NoAction
|
||||||
|
|
||||||
data Model = Model
|
data Model = Model
|
||||||
{ search_term :: JSString
|
{ searchTerm :: JSString
|
||||||
|
, clientModel :: Client.Model
|
||||||
} deriving Eq
|
} deriving Eq
|
||||||
|
|
||||||
data Interface a = Interface
|
data Interface a = Interface
|
||||||
{ passAction :: Action -> a
|
{ passAction :: Action -> a
|
||||||
|
, clientIface :: Client.Interface a [ CatalogPost ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
update :: Interface a -> Action -> Model -> Effect a Model
|
update :: Interface a -> Action -> Model -> Effect a Model
|
||||||
update iface (SearchChange q) model = model { search_term = q } <# do
|
update iface (SearchChange q) model = model { searchTerm = q } <# do
|
||||||
consoleLog q
|
consoleLog q
|
||||||
return $ (passAction iface) NoAction
|
return $ (passAction iface) NoAction
|
||||||
|
|
||||||
update iface OnSubmit model = model <# do
|
update iface OnSubmit model = model <# do
|
||||||
consoleLog $ "Submit!" <> search_term model
|
consoleLog $ "Submit!" <> searchTerm model
|
||||||
|
return $ (passAction iface) NoAction
|
||||||
|
|
||||||
|
update iface (SearchResult result) model = model <# do
|
||||||
|
consoleLog $ "Search result"
|
||||||
|
case result of
|
||||||
|
Error -> consoleLog $ "Error!"
|
||||||
|
HttpResponse {..} -> do
|
||||||
|
consoleLog $ (pack $ show $ status_code) <> " " <> (pack $ status_text)
|
||||||
|
consoleLog $ (pack $ show $ body)
|
||||||
|
|
||||||
return $ (passAction iface) NoAction
|
return $ (passAction iface) NoAction
|
||||||
|
|
||||||
update _ NoAction m = noEff m
|
update _ NoAction m = noEff m
|
||||||
|
|
24
src/Main.hs
24
src/Main.hs
|
@ -88,17 +88,24 @@ initialModel
|
||||||
-> Model
|
-> Model
|
||||||
initialModel pgroot client_fetch_count media_root u t = Model
|
initialModel pgroot client_fetch_count media_root u t = Model
|
||||||
{ grid_model = Grid.initialModel media_root
|
{ grid_model = Grid.initialModel media_root
|
||||||
, client_model = Client.Model
|
, client_model = client_model_
|
||||||
{ Client.pgApiRoot = pgroot
|
|
||||||
, Client.fetchCount = client_fetch_count
|
|
||||||
}
|
|
||||||
, thread_model = Nothing
|
, thread_model = Nothing
|
||||||
, current_uri = u
|
, current_uri = u
|
||||||
, media_root_ = media_root
|
, media_root_ = media_root
|
||||||
, current_time = t
|
, current_time = t
|
||||||
, tc_model = TC.initialModel 0
|
, tc_model = TC.initialModel 0
|
||||||
, search_model = Search.Model { Search.search_term = "" }
|
, search_model = Search.Model
|
||||||
|
{ Search.searchTerm = ""
|
||||||
|
, Search.clientModel = client_model_
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
where
|
||||||
|
client_model_ = Client.Model
|
||||||
|
{ Client.pgApiRoot = pgroot
|
||||||
|
, Client.fetchCount = client_fetch_count
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
getMetadata :: String -> IO (Maybe JSString)
|
getMetadata :: String -> IO (Maybe JSString)
|
||||||
getMetadata key = do
|
getMetadata key = do
|
||||||
|
@ -112,6 +119,7 @@ getMetadata key = do
|
||||||
Nothing -> return Nothing
|
Nothing -> return Nothing
|
||||||
Just el -> getAttribute el ("content" :: JSString)
|
Just el -> getAttribute el ("content" :: JSString)
|
||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
consoleLog "Hello World!"
|
consoleLog "Hello World!"
|
||||||
|
@ -285,4 +293,8 @@ iTime = TC.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
iSearch :: Search.Interface Action
|
iSearch :: Search.Interface Action
|
||||||
iSearch = Search.Interface { passAction = SearchAction }
|
iSearch =
|
||||||
|
Search.Interface
|
||||||
|
{ passAction = SearchAction
|
||||||
|
, clientIface = iClient (SearchAction . Search.SearchResult)
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ module Network.Client
|
||||||
, getThread
|
, getThread
|
||||||
, Model (..)
|
, Model (..)
|
||||||
, update
|
, update
|
||||||
|
, search
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
|
@ -21,7 +22,6 @@ import Control.Monad (void)
|
||||||
import Control.Concurrent (forkIO)
|
import Control.Concurrent (forkIO)
|
||||||
import Control.Concurrent.MVar (takeMVar)
|
import Control.Concurrent.MVar (takeMVar)
|
||||||
import Data.Aeson (ToJSON, FromJSON)
|
import Data.Aeson (ToJSON, FromJSON)
|
||||||
import Data.Time (getCurrentTime)
|
|
||||||
import Data.Time.Clock (UTCTime)
|
import Data.Time.Clock (UTCTime)
|
||||||
|
|
||||||
import GHCJS.DOM.Types (JSString)
|
import GHCJS.DOM.Types (JSString)
|
||||||
|
@ -51,6 +51,10 @@ data FetchCatalogArgs = FetchCatalogArgs
|
||||||
} deriving (Generic, ToJSON)
|
} deriving (Generic, ToJSON)
|
||||||
|
|
||||||
|
|
||||||
|
data SearchPostsArgs = SearchPostsArgs { search_text :: JSString }
|
||||||
|
deriving (Generic, ToJSON)
|
||||||
|
|
||||||
|
|
||||||
http_
|
http_
|
||||||
:: (ToJSON c, FromJSON b)
|
:: (ToJSON c, FromJSON b)
|
||||||
=> Model
|
=> Model
|
||||||
|
@ -89,3 +93,14 @@ getThread m iface A.GetThreadArgs {..} =
|
||||||
<> "&boards.pathpart=eq." <> toMisoString board_pathpart
|
<> "&boards.pathpart=eq." <> toMisoString board_pathpart
|
||||||
<> "&boards.threads.board_thread_id=eq." <> toMisoString (show board_thread_id)
|
<> "&boards.threads.board_thread_id=eq." <> toMisoString (show board_thread_id)
|
||||||
<> "&boards.threads.posts.order=board_post_id.asc"
|
<> "&boards.threads.posts.order=board_post_id.asc"
|
||||||
|
|
||||||
|
|
||||||
|
search :: Model -> JSString -> Interface a [ CatalogPost ] -> IO a
|
||||||
|
search m query iface =
|
||||||
|
http_ m iface "/rpc/search_posts" Http.POST payload
|
||||||
|
|
||||||
|
where
|
||||||
|
payload = Just $ SearchPostsArgs
|
||||||
|
{ search_text = query
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,9 @@ mkResult xhr = do
|
||||||
Left err -> do
|
Left err -> do
|
||||||
consoleLog $ toJSString $ show err
|
consoleLog $ toJSString $ show err
|
||||||
return Error
|
return Error
|
||||||
Right x -> return HttpResponse
|
Right x -> do
|
||||||
|
consoleLog $ toJSString "Decoding Successful"
|
||||||
|
return HttpResponse
|
||||||
{ status_code = status_code_int
|
{ status_code = status_code_int
|
||||||
, status_text = st
|
, status_text = st
|
||||||
, body = Just x
|
, body = Just x
|
||||||
|
|
Loading…
Reference in New Issue