diff options
author | Yorhel <git@yorhel.nl> | 2020-01-05 12:47:04 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2020-01-05 12:52:12 +0100 |
commit | d1d981efa5b0910260cbd1c226e3ab683c8304ae (patch) | |
tree | 8704fd96dc8e5820dae8017e5bfaf882ed80980f /elm/Lib/Autocomplete.elm | |
parent | 0efea750ac26629f48fb50a9cdc9d71bd08257b7 (diff) |
refactor: Combine json_api() & elm_form() + generate Elm function to msg API endpoint
The new elm_api() function now creates an API endpoint (like json_api())
and generates a corresponding Elm module to interact with that API (like
elm_form()). The API endpoint URL is now derived from the name of the
Elm module, so there's no need to think of a separate URL and less prone
to making typos when using that URL from Elm.
Reduces the boilerplace a bit as well.
Diffstat (limited to 'elm/Lib/Autocomplete.elm')
-rw-r--r-- | elm/Lib/Autocomplete.elm | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/elm/Lib/Autocomplete.elm b/elm/Lib/Autocomplete.elm index 77f52f9e..a478921b 100644 --- a/elm/Lib/Autocomplete.elm +++ b/elm/Lib/Autocomplete.elm @@ -24,6 +24,7 @@ import Lib.Util exposing (..) import Lib.Api as Api import Gen.Types exposing (boardTypes) import Gen.Api as GApi +import Gen.Boards as GB type alias Config m a = @@ -37,9 +38,8 @@ type alias Config m a = type alias SourceConfig m a = - -- API path to query for completion results. - -- (The API must accept POST requests with {"search":".."} as body) - { path : String + -- API endpoint to query for completion results. + { endpoint : String -> (GApi.Response -> m) -> Cmd m -- How to decode results from the API , decode : GApi.Response -> Maybe (List a) -- How to display the decoded results @@ -54,7 +54,7 @@ type alias SourceConfig m a = boardSource : SourceConfig m GApi.ApiBoardResult boardSource = - { path = "/t/boards.json" + { endpoint = \s -> GB.send { search = s } , decode = \x -> case x of GApi.BoardResult e -> Just e _ -> Nothing @@ -161,7 +161,7 @@ update cfg msg model = if model.value == "" || model.wait /= i then mod model else ( model - , Api.post cfg.source.path (JE.object [("search", JE.string model.value)]) (cfg.wrap << Results model.value) + , cfg.source.endpoint model.value (cfg.wrap << Results model.value) , Nothing ) Results s r -> mod <| |