|
| 1 | +//> using scala "3.3.1" |
| 2 | +//> using dep ba.sake::sharaf:0.0.22 |
| 3 | + |
| 4 | +import java.util.UUID |
| 5 | +import io.undertow.Undertow |
| 6 | +import scalatags.Text.all.* |
| 7 | +import ba.sake.hepek.html.HtmlPage |
| 8 | +import ba.sake.hepek.htmx.* |
| 9 | +import ba.sake.querson.QueryStringRW |
| 10 | +import ba.sake.sharaf.*, routing.* |
| 11 | + |
| 12 | +object views { |
| 13 | + import scalatags.Text.all.* |
| 14 | + import ba.sake.hepek.html.HtmlPage |
| 15 | + import ba.sake.hepek.htmx.* |
| 16 | + |
| 17 | + trait BasePage extends HtmlPage with HtmxDependencies |
| 18 | + |
| 19 | + class ContactsViewPage(contacts: Seq[Contact]) extends BasePage: |
| 20 | + override def bodyContent = div( |
| 21 | + h1("Delete Row example"), |
| 22 | + table()( |
| 23 | + thead(tr(th("Name"), th("Email"), th(""))), |
| 24 | + tbody(hx.confirm := "Are you sure?", hx.target := "closest tr", hx.swap := "outerHTML swap:1s")( |
| 25 | + contactsRows(contacts) |
| 26 | + ) |
| 27 | + ) |
| 28 | + ) |
| 29 | + |
| 30 | + override def stylesInline = List(""" |
| 31 | + tr.htmx-swapping td { |
| 32 | + opacity: 0; |
| 33 | + transition: opacity 1s ease-out; |
| 34 | + } |
| 35 | + """) |
| 36 | + |
| 37 | + def contactsRows(contacts: Seq[Contact]): Frag = contacts.map { contact => |
| 38 | + tr(td(contact.name), td(contact.email), td(button(hx.delete := s"/contacts/${contact.id}")("Delete"))) |
| 39 | + } |
| 40 | + |
| 41 | +} |
| 42 | +case class Contact(id: String, name: String, email: String) |
| 43 | + |
| 44 | +var allContacts = Seq( |
| 45 | + Contact( "1", "Angie MacDowell", "[email protected]"), |
| 46 | + Contact( "2", "Fuqua Tarkenton", "[email protected]"), |
| 47 | + Contact( "3", "Kim Yee", "[email protected]") |
| 48 | +) |
| 49 | + |
| 50 | +val routes = Routes: |
| 51 | + case GET() -> Path() => |
| 52 | + Response.withBody(views.ContactsViewPage(allContacts)) |
| 53 | + case DELETE() -> Path("contacts", id) => |
| 54 | + allContacts = allContacts.filterNot(_.id == id) |
| 55 | + Response.withBody("") // empty string, remove that <tr> |
| 56 | + |
| 57 | +Undertow.builder |
| 58 | + .addHttpListener(8181, "localhost") |
| 59 | + .setHandler(SharafHandler(routes)) |
| 60 | + .build |
| 61 | + .start() |
| 62 | + |
| 63 | +println(s"Server started at http://localhost:8181") |
0 commit comments