From 10a63c8f1d0bed76a2f8e0cf98645478facb5370 Mon Sep 17 00:00:00 2001 From: Guillaume Hivert Date: Sun, 1 Dec 2024 16:15:14 +0100 Subject: [PATCH] Rename message with convention Subject-Verb-Object --- apps/frontend/src/data/msg.gleam | 29 ++++----- apps/frontend/src/frontend.gleam | 61 ++++++++----------- .../src/frontend/view/body/body.gleam | 6 +- .../src/frontend/view/body/cache.gleam | 12 ++-- .../src/frontend/view/footer/footer.gleam | 2 +- .../view/search_input/search_input.gleam | 2 +- 6 files changed, 53 insertions(+), 59 deletions(-) diff --git a/apps/frontend/src/data/msg.gleam b/apps/frontend/src/data/msg.gleam index 11142ab..3bece08 100644 --- a/apps/frontend/src/data/msg.gleam +++ b/apps/frontend/src/data/msg.gleam @@ -37,18 +37,19 @@ pub type Analytics { } pub type Msg { - None - Packages(packages: Result(List(package.Package), http.HttpError)) - OnSearchFocus(event: Dynamic) - SubmitSearch - UpdateIsMobile(is_mobile: Bool) - SearchResults(input: String, result: Result(SearchResults, http.HttpError)) - Trendings(result: Result(List(package.Package), http.HttpError)) - UpdateInput(String) - Reset - ScrollTo(String) - OnEscape - OnAnalytics(Result(Analytics, http.HttpError)) - OnRouteChange(router.Route) - OnCheckFilter(Filter, Bool) + ApiReturnedAnalytics(Result(Analytics, http.HttpError)) + ApiReturnedPackages(packages: Result(List(package.Package), http.HttpError)) + ApiReturnedSearchResults( + input: String, + result: Result(SearchResults, http.HttpError), + ) + ApiReturnedTrendings(result: Result(List(package.Package), http.HttpError)) + BrowserChangedRoute(router.Route) + BrowserResizedViewport(is_mobile: Bool) + UserClickedSidebarName(String) + UserFocusedSearch(event: Dynamic) + UserInputtedSearch(String) + UserPressedEscape + UserSubmittedSearch + UserToggledFilter(Filter, Bool) } diff --git a/apps/frontend/src/frontend.gleam b/apps/frontend/src/frontend.gleam index cef7323..0c7f66d 100644 --- a/apps/frontend/src/frontend.gleam +++ b/apps/frontend/src/frontend.gleam @@ -46,15 +46,15 @@ fn subscribe_focus() { use dispatch <- effect.from() use event <- ffi.subscribe_focus() case ffi.key(event) { - Ok("Escape") -> dispatch(msg.OnEscape) - _ -> dispatch(msg.OnSearchFocus(event)) + Ok("Escape") -> dispatch(msg.UserPressedEscape) + _ -> dispatch(msg.UserFocusedSearch(event)) } } fn subscribe_is_mobile() { use dispatch <- effect.from() use is_mobile <- ffi.suscribe_is_mobile() - dispatch(msg.UpdateIsMobile(is_mobile)) + dispatch(msg.BrowserResizedViewport(is_mobile)) } pub fn main() { @@ -94,15 +94,15 @@ fn init(_) { |> update.add_effect(subscribe_focus()) |> update.add_effect(subscribe_is_mobile()) |> update.add_effect( - http.expect_json(dynamic.list(package.decoder), msg.Trendings) + http.expect_json(dynamic.list(package.decoder), msg.ApiReturnedTrendings) |> http.get(config.api_endpoint() <> "/trendings", _), ) |> update.add_effect( - http.expect_json(dynamic.list(package.decoder), msg.Packages) + http.expect_json(dynamic.list(package.decoder), msg.ApiReturnedPackages) |> http.get(config.api_endpoint() <> "/packages", _), ) |> update.add_effect( - msg.OnAnalytics + msg.ApiReturnedAnalytics |> http.expect_json( dynamic.decode6( msg.Analytics, @@ -133,32 +133,12 @@ fn init(_) { fn on_url_change(uri: Uri) -> Msg { router.parse_uri(uri) - |> msg.OnRouteChange() + |> msg.BrowserChangedRoute } fn update(model: Model, msg: Msg) { case msg { - msg.UpdateInput(content) -> update_input(model, content) - msg.SubmitSearch -> submit_search(model) - msg.Reset -> reset(model) - msg.None -> update.none(model) - msg.Packages(Ok(packages)) -> model.Model(..model, packages:) |> update.none - msg.Packages(_) -> update.none(model) - msg.ScrollTo(id) -> scroll_to(model, id) - msg.OnRouteChange(route) -> handle_route_change(model, route) - msg.Trendings(trendings) -> handle_trendings(model, trendings) - msg.OnSearchFocus(event) -> - update.effect(model, focus(on: "search-input", event: event)) - msg.OnEscape -> update.effect(model, blur()) - msg.UpdateIsMobile(is_mobile) -> - model - |> model.update_is_mobile(is_mobile) - |> update.none - msg.SearchResults(input, search_results) -> - handle_search_results(model, input, search_results) - msg.OnCheckFilter(filter, value) -> - handle_oncheck_filter(model, filter, value) - msg.OnAnalytics(analytics) -> { + msg.ApiReturnedAnalytics(analytics) -> { case analytics { Error(_) -> #(model, effect.none()) Ok(analytics) -> @@ -167,6 +147,23 @@ fn update(model: Model, msg: Msg) { |> update.none() } } + msg.ApiReturnedPackages(Ok(packages)) -> + model.Model(..model, packages:) |> update.none + msg.ApiReturnedPackages(_) -> update.none(model) + msg.ApiReturnedSearchResults(input, search_results) -> + handle_search_results(model, input, search_results) + msg.ApiReturnedTrendings(trendings) -> handle_trendings(model, trendings) + msg.BrowserChangedRoute(route) -> handle_route_change(model, route) + msg.BrowserResizedViewport(is_mobile) -> + model |> model.update_is_mobile(is_mobile) |> update.none + msg.UserClickedSidebarName(id) -> scroll_to(model, id) + msg.UserFocusedSearch(event) -> + update.effect(model, focus(on: "search-input", event: event)) + msg.UserInputtedSearch(content) -> update_input(model, content) + msg.UserPressedEscape -> update.effect(model, blur()) + msg.UserSubmittedSearch -> submit_search(model) + msg.UserToggledFilter(filter, value) -> + handle_oncheck_filter(model, filter, value) } } @@ -191,12 +188,6 @@ fn update_input(model: Model, content: String) { |> update.none } -fn reset(model: Model) { - model - |> model.reset - |> update.none -} - fn submit_search(model: Model) { use <- bool.guard(when: model.input == "", return: #(model, effect.none())) use <- bool.guard(when: model.loading, return: #(model, effect.none())) @@ -214,7 +205,7 @@ fn submit_search(model: Model) { |> update.add_effect(blur()) } Error(_) -> - msg.SearchResults(input: model.input, result: _) + msg.ApiReturnedSearchResults(input: model.input, result: _) |> http.expect_json(search_result.decode_search_results, _) |> http.get(config.api_endpoint() <> "/search?q=" <> model.input, _) |> update.effect(model.toggle_loading(new_model), _) diff --git a/apps/frontend/src/frontend/view/body/body.gleam b/apps/frontend/src/frontend/view/body/body.gleam index 7bde2f1..bc8e4c0 100644 --- a/apps/frontend/src/frontend/view/body/body.gleam +++ b/apps/frontend/src/frontend/view/body/body.gleam @@ -28,7 +28,7 @@ fn view_search_input(model: Model) { |> string.length() |> fn(input) { input != 0 } } - h.form([a.class("search-wrapper"), e.on_submit(msg.SubmitSearch)], [ + h.form([a.class("search-wrapper"), e.on_submit(msg.UserSubmittedSearch)], [ h.div([a.class("search-title-wrapper")], [ h.div([a.class("search-title")], [ h.img([ @@ -152,7 +152,7 @@ fn sidebar(model: Model) { ]), h.form([a.class("sidebar-title-inside")], [h.text("Gloogle")]), ]), - h.form([e.on_submit(msg.SubmitSearch)], [ + h.form([e.on_submit(msg.UserSubmittedSearch)], [ search_input.view(model.loading, model.input, small: True), ]), h.div([a.class("sidebar-filter"), disabled], [el.text("Filters")]), @@ -208,7 +208,7 @@ fn checkbox(active: Bool, msg: msg.Filter, name: String) { a.class("sidebar-checkbox-2"), a.type_("checkbox"), a.checked(active), - e.on_check(msg.OnCheckFilter(msg, _)), + e.on_check(msg.UserToggledFilter(msg, _)), ]), ]), h.div([a.class("sidebar-filter-name")], [el.text(name)]), diff --git a/apps/frontend/src/frontend/view/body/cache.gleam b/apps/frontend/src/frontend/view/body/cache.gleam index 8068bf3..57074b1 100644 --- a/apps/frontend/src/frontend/view/body/cache.gleam +++ b/apps/frontend/src/frontend/view/body/cache.gleam @@ -32,11 +32,13 @@ fn sidebar( ..list.map(modules, fn(module) { let #(module, name) = module let id = package.0 <> "@" <> package.1 <> "-" <> module <> "-" <> name - h.div([a.class("sidebar-module-name"), e.on_click(msg.ScrollTo(id))], [ - t.keyword(module), - h.text("."), - t.fun(name), - ]) + h.div( + [ + a.class("sidebar-module-name"), + e.on_click(msg.UserClickedSidebarName(id)), + ], + [t.keyword(module), h.text("."), t.fun(name)], + ) }) ]) } diff --git a/apps/frontend/src/frontend/view/footer/footer.gleam b/apps/frontend/src/frontend/view/footer/footer.gleam index db73cb0..9c3994e 100644 --- a/apps/frontend/src/frontend/view/footer/footer.gleam +++ b/apps/frontend/src/frontend/view/footer/footer.gleam @@ -42,7 +42,7 @@ pub fn view() { pub fn search_bar(model: Model) { use <- bool.guard(when: !model.is_mobile, return: el.none()) h.div([a.class("footer-search")], [ - h.form([e.on_submit(msg.SubmitSearch)], [ + h.form([e.on_submit(msg.UserSubmittedSearch)], [ search_input.view(model.loading, model.input, small: True), ]), ]) diff --git a/apps/frontend/src/frontend/view/search_input/search_input.gleam b/apps/frontend/src/frontend/view/search_input/search_input.gleam index 74b5d2d..afdf0e9 100644 --- a/apps/frontend/src/frontend/view/search_input/search_input.gleam +++ b/apps/frontend/src/frontend/view/search_input/search_input.gleam @@ -11,7 +11,7 @@ pub fn view(loading loading: Bool, input input: String, small small: Bool) { s.search_input_content([ a.id("search-input"), a.placeholder("Search for a function, or a type"), - e.on_input(msg.UpdateInput), + e.on_input(msg.UserInputtedSearch), a.value(input), a.attribute("autocorrect", "off"), a.attribute("autocapitalize", "none"),