Route search results, displaying only heading though

This commit is contained in:
towards-a-new-leftypol 2024-03-01 22:02:13 -05:00
parent 6d17cefabe
commit b78b2af1ab
4 changed files with 30 additions and 11 deletions

View File

@ -37,9 +37,9 @@ 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 search_query) model = model { searchTerm = search_query } <# do
consoleLog $ "Submit! " <> searchTerm model consoleLog $ "Submit! " <> search_query
Client.search (clientModel model) (searchTerm model) (clientIface iface) Client.search (clientModel model) search_query (clientIface iface)
update iface (SearchResult result) model = model <# do update iface (SearchResult result) model = model <# do
consoleLog $ "Received search results!" consoleLog $ "Received search results!"
@ -62,12 +62,12 @@ update iface (PassPostsToSelf search_results) model = model { displayResults = s
update _ NoAction m = noEff m update _ NoAction m = noEff m
view :: Interface a -> View a view :: Interface a -> Model -> View a
view iface = form_ view iface m = form_
[ class_ "search_form" [ class_ "search_form"
, action_ "/search" , action_ "/search"
, method_ "GET" , method_ "GET"
, onSubmit $ pass OnSubmit , onSubmit $ pass $ OnSubmit $ searchTerm m
] ]
[ input_ [ input_
[ type_ "submit" [ type_ "submit"

View File

@ -8,7 +8,7 @@ import Network.CatalogPostType (CatalogPost)
data Action data Action
= SearchChange JSString = SearchChange JSString
| OnSubmit | OnSubmit JSString
| SearchResult (HttpResult [ CatalogPost ]) | SearchResult (HttpResult [ CatalogPost ])
| PassPostsToSelf [ CatalogPost ] -- I think I don't understand something about the update type but I had to add this... | PassPostsToSelf [ CatalogPost ] -- I think I don't understand something about the update type but I had to add this...
| NoAction | NoAction

View File

@ -9,7 +9,7 @@ import Data.Proxy
import Data.Maybe (maybe, fromJust) import Data.Maybe (maybe, fromJust)
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import Network.URI (uriPath, uriQuery, escapeURIString, isAllowedInURI) import Network.URI (uriPath, uriQuery, escapeURIString, unEscapeString, isAllowedInURI)
import System.FilePath ((</>)) import System.FilePath ((</>))
import Data.Time.Clock (UTCTime, getCurrentTime) import Data.Time.Clock (UTCTime, getCurrentTime)
@ -51,6 +51,7 @@ import qualified Component.CatalogGrid as Grid
import qualified Component.ThreadView as Thread import qualified Component.ThreadView as Thread
import qualified Component.TimeControl as TC import qualified Component.TimeControl as TC
import qualified Component.Search as Search import qualified Component.Search as Search
import qualified Component.Search.SearchTypes as Search
data Model = Model data Model = Model
@ -79,7 +80,7 @@ initialActionFromRoute model uri = either (const NoAction) id routing_result
where where
routing_result = runRoute (Proxy :: Proxy Route) handlers (const uri) model routing_result = runRoute (Proxy :: Proxy Route) handlers (const uri) model
handlers = h_latest :<|> h_thread handlers = h_latest :<|> h_thread :<|> h_search
h_latest :: Model -> Action h_latest :: Model -> Action
h_latest = const $ GoToTime $ current_time model h_latest = const $ GoToTime $ current_time model
@ -87,6 +88,15 @@ initialActionFromRoute model uri = either (const NoAction) id routing_result
h_thread :: Text -> Text -> BoardThreadId -> Model -> Action h_thread :: Text -> Text -> BoardThreadId -> Model -> Action
h_thread website board_pathpart board_thread_id _ = GetThread GetThreadArgs {..} h_thread website board_pathpart board_thread_id _ = GetThread GetThreadArgs {..}
h_search :: Maybe Text -> Model -> Action
h_search Nothing model = GoToTime $ current_time model
h_search (Just search_query) model
| Search.searchTerm (search_model model) == unescaped_search_query = SearchResults unescaped_search_query
| otherwise = (Search.passAction iSearch) $ Search.OnSubmit $ toJSString $ T.unpack search_query
where
unescaped_search_query = toJSString $ unEscapeString $ T.unpack search_query
initialModel initialModel
:: JSString :: JSString
@ -175,7 +185,7 @@ mainView model = view
either (const page404) id either (const page404) id
$ runRoute (Proxy :: Proxy Route) handlers current_uri model $ runRoute (Proxy :: Proxy Route) handlers current_uri model
handlers = catalog_view :<|> thread_view handlers = catalog_view :<|> thread_view :<|> search_view
catalog_view :: Model -> View Action catalog_view :: Model -> View Action
catalog_view m = div_ [] catalog_view m = div_ []
@ -185,7 +195,7 @@ mainView model = view
, time_ [] [ text $ pack $ show $ current_time model ] , time_ [] [ text $ pack $ show $ current_time model ]
] ]
, TC.view iTime (tc_model m) , TC.view iTime (tc_model m)
, Search.view iSearch , Search.view iSearch (search_model m)
, Grid.view iGrid (grid_model model) , Grid.view iGrid (grid_model model)
] ]
@ -195,6 +205,9 @@ mainView model = view
Thread.view Thread.view
(thread_model m) (thread_model m)
search_view :: Maybe Text -> Model -> View Action
search_view _ _ = div_ [] [ text "Search results" ]
page404 :: View Action page404 :: View Action
page404 = h1_ [] [ text "404 Not Found" ] page404 = h1_ [] [ text "404 Not Found" ]

View File

@ -15,6 +15,7 @@ import Action
type Route type Route
= R_Latest = R_Latest
:<|> R_Thread :<|> R_Thread
:<|> R_SearchResults
type R_Latest = View Action type R_Latest = View Action
@ -26,4 +27,9 @@ type R_Thread
:> Capture "board_thread_id" BoardThreadId :> Capture "board_thread_id" BoardThreadId
:> View Action :> View Action
type R_SearchResults
= "search"
:> QueryParam "search" Text
:> View Action
type BoardThreadId = Int64 type BoardThreadId = Int64