Skip to content

Commit

Permalink
more tags, checked examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosell committed Aug 4, 2024
1 parent 1a05d63 commit 3306b0b
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 122 deletions.
26 changes: 0 additions & 26 deletions R/ambhtmx.R
Original file line number Diff line number Diff line change
Expand Up @@ -546,32 +546,6 @@ process_loggedin_middleware <- \(
}


#' Process error post requests
#'
#' @param req request object
#' @param res response object
#' @param errors the error message character vector
#' @param cookie_errors if you need to customize the name of the errors cookie
#' @param error_url if you need to customize the url of the error to redirect to.
#' @returns the error process response
#' @export
process_error_post <- \(
req,
res,
errors = NULL,
cookie_errors = "errors",
error_url = NULL
) {
if (is_debug_enabled()) print("process_error_post")
error_message <- paste0(errors, ". ", collapse = "")
res$cookie(
name = cookie_errors,
value = error_message
)
res$header("HX-Redirect", error_url)
return(res$redirect(error_url, status = 302L))
}

#' Process error get requests
#'
#' @param req request object
Expand Down
22 changes: 21 additions & 1 deletion R/extra-tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@ input <- htmltools::tags$input

#' @rdname builder
#' @export
label <- htmltools::tags$label
label <- htmltools::tags$label

#' @rdname builder
#' @export
nav <- htmltools::tags$nav

#' @rdname builder
#' @export
li <- htmltools::tags$li

#' @rdname builder
#' @export
ul <- htmltools::tags$ul

#' @rdname builder
#' @export
ol <- htmltools::tags$ol

#' @rdname builder
#' @export
form <- htmltools::tags$form
2 changes: 1 addition & 1 deletion inst/examples/01-counter.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library(ambhtmx)
# devtools::load_all()
# devtools::load_all(); remotes::install_github("jrosell/ambhtmx", force = TRUE)

#' Starting the app
counter <- 0
Expand Down
25 changes: 12 additions & 13 deletions inst/examples/02-ggplot2.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
tryCatch({
library(ambhtmx)
},
error = \(e) print(e)
)

# devtools::load_all()
library(ambhtmx)
library(ggplot2)

#' Starting the app
counter <- 0
Expand Down Expand Up @@ -35,12 +30,16 @@ app$get("/", \(req, res){

#' Post call to return the plot
app$post("/increment", \(req, res){
counter <<- counter + 1
rexp_data <<- c(rexp_data, rexp(1))
rexp_df <- tibble(x = 1:length(rexp_data), y = rexp_data)
p <- ggplot(rexp_df, aes(x, y)) + geom_line()
png(p_file <- tempfile(fileext = ".png")); print(p); dev.off()
p_txt <- b64::encode_file(p_file)
tryCatch({
counter <<- counter + 1
rexp_data <<- c(rexp_data, rexp(1))
rexp_df <- tibble(x = 1:length(rexp_data), y = rexp_data)
p <- ggplot(rexp_df, aes(x, y)) + geom_line()
png(p_file <- tempfile(fileext = ".png")); print(p); dev.off()
p_txt <- b64::encode_file(p_file)
},
error = \(e) print(e)
)
res$send(render_tags(
tags$p(glue("Counter is set to {counter}")),
tags$img(src = glue("data:image/png;base64,{p_txt}"))
Expand Down
7 changes: 1 addition & 6 deletions inst/examples/03-slider.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(tidyverse)
library(zeallot)
library(glue)
library(htmltools)
library(ggplot2)

#' Starting the app
counter <- 1
Expand Down
6 changes: 0 additions & 6 deletions inst/examples/04-todo.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(scilis)
library(tidyverse)
library(zeallot)
library(glue)
library(htmltools)

#' Starting the app
counter <- 0
Expand Down
13 changes: 8 additions & 5 deletions inst/examples/05-live.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# TODO: Use websockets to refresh the HTML page when R server is restarted.
# TODO: Detect if the script is run from nodemon or not.

library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(tidyverse)
library(zeallot)
library(glue)
library(htmltools)


live_path <- tryCatch(
{this.path::this.path()},
error = function(e) return("")
)


#' Starting the app
counter <- 0
c(app, context, operations) %<-% ambhtmx_app(live = live_path)


#' Main page of the app
app$get("/", \(req, res){
html <- render_page(
Expand All @@ -32,11 +33,13 @@ app$get("/", \(req, res){
res$send(html)
})


#' Post call to return the value of the global counter variable
app$post("/increment", \(req, res){
counter <<- counter + 1
res$send(glue("Counter is set to {counter}"))
})


#' Start the app with all the previous defined routes
app$start(open = FALSE)
12 changes: 3 additions & 9 deletions inst/examples/06-basic-auth.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(tidyverse)
library(zeallot)
library(glue)
library(htmltools)
library(ggplot2)

page_title <- "ambhtmx basic authentication example"

Expand All @@ -14,9 +10,7 @@ live_path <- tryCatch(
)

#' Starting the app
c(app, context, operations) %<-% ambhtmx_app(
live = live_path
)
c(app, context, operations) %<-% ambhtmx_app()

#' Authentication feature with secret cookies and .Renviron variables
app$get("/login", \(req, res) {
Expand Down Expand Up @@ -85,5 +79,5 @@ app$post("/increment", \(req, res){
})

#' Start the app with all the previous defined routes
app$start(open = FALSE)
app$start()

99 changes: 44 additions & 55 deletions inst/examples/07-crud.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(tidyverse)
library(zeallot)
library(glue)
library(htmltools)
library(signaculum)

page_title <- "Password protected CRUD (Create, Read, Update, and Delete) example with ambhtmx"

live_path <- tryCatch(
{this.path::this.path()},
error = function(e) return("")
)
print(live_path)

render_index <- \() {
main <- NULL
tryCatch({
Expand Down Expand Up @@ -57,6 +45,33 @@ render_index <- \() {
return(main)
}

render_new <- \(req, res) {
errors <- process_error_get(req, res)
render_tags(tagList(
h2("New item"),
div(label("Name", p(input(name = "name")))),
div(label("Content", p(textarea(name = "content")))),
a(
"Go back",
href = "/",
style = "margin-right:20px",
`hx-confirm` = "Are you sure you want to go back?",
`hx-get` = "/items",
`hx-target` = "#page",
`hx-swap` = "outerHTML",
`hx-encoding` = "multipart/form-data"
),
button(
"Create",
style = "margin-top:20px",
`hx-post` = "/items",
`hx-target` = "#page",
`hx-swap` = "outerHTML",
`hx-include` = "[name='name'], [name='content']",
),
errors
))
}

render_row <- \(item) {
tags$div(
Expand All @@ -76,7 +91,6 @@ c(app, context, items) %<-%
name = character(1),
content = character(1)
),
live = live_path,
render_index = render_index,
render_row = render_row
)
Expand All @@ -86,22 +100,13 @@ app$get("/login", \(req, res) {
process_login_get(req, res)
})
app$post("/login", \(req, res) {
process_login_post(
req,
res,
user = Sys.getenv("AMBHTMX_USER"),
password = Sys.getenv("AMBHTMX_PASSWORD")
)
process_login_post(req, res)
})
app$get("/logout", \(req, res) {
process_logout_get(req, res)
})
app$use(\(req, res){
process_loggedin_middleware(
req,
res,
user = Sys.getenv("AMBHTMX_USER")
)
process_loggedin_middleware(req, res)
})

#' Some CRUD operations examples
Expand Down Expand Up @@ -169,31 +174,11 @@ app$get("/items/new", \(req, res){
if (!req$loggedin) {
return(res$redirect("/login", status = 302L))
}
errors <- process_error_get(req, res)
html <- render_tags(tagList(
h2("New item"),
div(label("Name", p(input(name = "name")))),
div(label("Content", p(textarea(name = "content")))),
a(
"Go back",
href = "/",
style = "margin-right:20px",
`hx-confirm` = "Are you sure you want to go back?",
`hx-get` = "/items",
`hx-target` = "#page",
`hx-swap` = "outerHTML",
`hx-encoding` = "multipart/form-data"
),
button(
"Create",
style = "margin-top:20px",
`hx-post` = "/items",
`hx-target` = "#page",
`hx-swap` = "outerHTML",
`hx-include` = "[name='name'], [name='content']",
),
errors
))
tryCatch({
html <- render_new(req, res)
},
error = \(e) print(e)
)
res$send(html)
})

Expand Down Expand Up @@ -271,19 +256,23 @@ app$get("/items/:id/edit", \(req, res){
res$send(html)
})


#' Create a new item
app$post("/items", \(req, res){
if (!req$loggedin) {
return(res$redirect("/login", status = 302L))
}
params <- parse_multipart(req)
if (is.null(params[["name"]])) {
return(process_error_post(
req,
res,
errors = "Name is required",
error_url = "/items/new"
))
error_message <- "Name is required."
res$cookie(
name = "errors",
value = error_message
)
res$header("HX-Retarget", "#main")
res$header("HX-Reswap", "innerHTML")
print("Retarget amb error")
return(res$send(render_new(req, res)))
}
if (is.null(params[["content"]])) {
params[["content"]] = ""
Expand Down

0 comments on commit 3306b0b

Please sign in to comment.