Skip to content

Commit

Permalink
ContactApp: contact count indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanayx committed Feb 28, 2024
1 parent 4731dda commit a9207b9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/ContactApp/ContactService.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ContactApp.ContactService

open System
open System.Threading
open ContactApp.Models

let private contactDb = ResizeArray([
Expand All @@ -18,6 +19,10 @@ let private contactDb = ResizeArray([
{ Id = 12; First = "Edith4"; Last = "Neutvaar"; Email = "en4@example.com"; Phone = "123-456-7890" }
])

let count() =
Thread.Sleep 2000
contactDb.Count

let searchContact (search: string) =
contactDb
|> Seq.filter(fun c -> c.First.Contains(search, StringComparison.OrdinalIgnoreCase)
Expand Down
5 changes: 5 additions & 0 deletions examples/ContactApp/Handlers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ let getContacts: EndpointHandler =
|> Seq.toArray
ctx |> writeHtml (index.html "" page result)

let getContactsCount: EndpointHandler =
fun ctx ->
let count = ContactService.count()
ctx.WriteText $"({count} total Contacts)"

let getNewContact: EndpointHandler =
let newContact = {
id = 0
Expand Down
1 change: 1 addition & 0 deletions examples/ContactApp/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let endpoints = [
subRoute "/contacts" [
route "/" getContacts
route "/new" getNewContact
route "/count" getContactsCount
routef "/{%i}" viewContact
routef "/{%i}/edit" getEditContact
routef "/{%i}/email" validateEmail
Expand Down
3 changes: 3 additions & 0 deletions examples/ContactApp/templates/index.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ let html q page (contacts: Contact[]) =

p() {
a(href="/contacts/new") { "Add Contact" }
span(hxGet="/contacts/count", hxTrigger="revealed"){
img(class'="spinner htmx-indicator", src="/spinning-circles.svg")
}
}
}
|> layout.html

0 comments on commit a9207b9

Please sign in to comment.