Skip to content

Commit

Permalink
Rename message with convention Subject-Verb-Object
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Dec 1, 2024
1 parent 4321802 commit 10a63c8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 59 deletions.
29 changes: 15 additions & 14 deletions apps/frontend/src/data/msg.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
61 changes: 26 additions & 35 deletions apps/frontend/src/frontend.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) ->
Expand All @@ -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)
}
}

Expand All @@ -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()))
Expand All @@ -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), _)
Expand Down
6 changes: 3 additions & 3 deletions apps/frontend/src/frontend/view/body/body.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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")]),
Expand Down Expand Up @@ -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)]),
Expand Down
12 changes: 7 additions & 5 deletions apps/frontend/src/frontend/view/body/cache.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
)
})
])
}
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/frontend/view/footer/footer.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]),
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 10a63c8

Please sign in to comment.