feat: рендеринг формы максимально реактивный
This commit is contained in:
65
app.R
65
app.R
@@ -40,7 +40,6 @@ if (!rmarkdown::pandoc_available()) warning("Can't find pandoc!")
|
|||||||
|
|
||||||
# SCHEME_MAIN UNPACK ==========================
|
# SCHEME_MAIN UNPACK ==========================
|
||||||
schm <- readRDS("scheme.rds")
|
schm <- readRDS("scheme.rds")
|
||||||
nav_panels_list <- schm$get_main_form_ui
|
|
||||||
|
|
||||||
# two_obj <- purrr::map(
|
# two_obj <- purrr::map(
|
||||||
# c(one = "configs/schemas/schema.xlsx", two = "configs/schemas/schema_example.xlsx"),
|
# c(one = "configs/schemas/schema.xlsx", two = "configs/schemas/schema_example.xlsx"),
|
||||||
@@ -154,27 +153,23 @@ server <- function(input, output, session) {
|
|||||||
nested_key = NULL,
|
nested_key = NULL,
|
||||||
nested_form_id = NULL
|
nested_form_id = NULL
|
||||||
)
|
)
|
||||||
|
main_form_is_empty <- reactiveVal(TRUE)
|
||||||
# VALIDATIONS ============================
|
validator_main <- reactiveVal(NULL)
|
||||||
# create new validator
|
validator_nested <- reactiveVal(NULL)
|
||||||
# TODO: как осуществить инициализацию валидатора после
|
|
||||||
iv_main <- data_validation$init_val(schm$get_schema("main"))
|
|
||||||
iv_main$enable()
|
|
||||||
|
|
||||||
# динамический рендеринг --------------------------
|
# динамический рендеринг --------------------------
|
||||||
output$main_ui_navset <- renderUI({
|
output$main_ui_navset <- renderUI({
|
||||||
|
|
||||||
shiny::validate(
|
if (main_form_is_empty()) {
|
||||||
need(values$main_key, "Для начала работы нужно создать новую запись или загрузить существующую!")
|
validator_main(NULL)
|
||||||
)
|
"Для начала работы нужно создать новую запись или загрузить существующую!"
|
||||||
|
} else {
|
||||||
|
|
||||||
# list of rendered panels
|
# list of rendered panels
|
||||||
navset_card_underline(
|
validator_main(data_validation$init_val(schm$get_schema("main")))
|
||||||
id = "main",
|
validator_main()$enable()
|
||||||
!!!nav_panels_list,
|
schm$get_main_form_ui
|
||||||
header = NULL,
|
}
|
||||||
height = NULL
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
@@ -377,6 +372,7 @@ server <- function(input, output, session) {
|
|||||||
actionButton("add_new_nested_key_button", "add"),
|
actionButton("add_new_nested_key_button", "add"),
|
||||||
actionButton("nested_form_save_button", "save"),
|
actionButton("nested_form_save_button", "save"),
|
||||||
actionButton("nested_form_dt_button", "dt"),
|
actionButton("nested_form_dt_button", "dt"),
|
||||||
|
uiOutput("validation_nested"),
|
||||||
open = list(mobile = "always-above")
|
open = list(mobile = "always-above")
|
||||||
),
|
),
|
||||||
# if (!is.null(values$nested_key)) {rlang::syms(!!!yay_its_fun)} else bslib::nav_panel("empty")
|
# if (!is.null(values$nested_key)) {rlang::syms(!!!yay_its_fun)} else bslib::nav_panel("empty")
|
||||||
@@ -384,8 +380,8 @@ server <- function(input, output, session) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
# проверка данных для внутренних таблиц
|
# проверка данных для внутренних таблиц
|
||||||
iv_inner <- data_validation$init_val(this_nested_form_scheme, ns)
|
validator_nested(data_validation$init_val(this_nested_form_scheme, ns))
|
||||||
iv_inner$enable()
|
validator_nested()$enable()
|
||||||
|
|
||||||
showModal(modalDialog(
|
showModal(modalDialog(
|
||||||
ui_for_inline_table,
|
ui_for_inline_table,
|
||||||
@@ -450,12 +446,12 @@ server <- function(input, output, session) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
### обновление данных при изменении --------------------
|
### обновление данных при изменении DT --------------------
|
||||||
observeEvent(input$dt_nested_cell_edit, {
|
observeEvent(input$dt_nested_cell_edit, {
|
||||||
values$data <- DT::editData(values$data, input$dt_nested_cell_edit, 'dt_nested', rownames = FALSE)
|
values$data <- DT::editData(values$data, input$dt_nested_cell_edit, 'dt_nested', rownames = FALSE)
|
||||||
})
|
})
|
||||||
|
|
||||||
### кнопка: отображение -----------------------------
|
### кнопка: отображение DT -----------------------------
|
||||||
observeEvent(input$nested_form_dt_button, {
|
observeEvent(input$nested_form_dt_button, {
|
||||||
con <- db$make_db_connection("nested_form_save_button")
|
con <- db$make_db_connection("nested_form_save_button")
|
||||||
on.exit(db$close_db_connection(con, "nested_form_save_button"), add = TRUE)
|
on.exit(db$close_db_connection(con, "nested_form_save_button"), add = TRUE)
|
||||||
@@ -464,7 +460,7 @@ server <- function(input, output, session) {
|
|||||||
show_modal_for_nested_form_dt(con)
|
show_modal_for_nested_form_dt(con)
|
||||||
})
|
})
|
||||||
|
|
||||||
### кнопка: сохранить изменения --------------------
|
### кнопка: сохранить изменения DT --------------------
|
||||||
observeEvent(input$nested_form_dt_save, {
|
observeEvent(input$nested_form_dt_save, {
|
||||||
|
|
||||||
con <- db$make_db_connection("nested_form_dt_save")
|
con <- db$make_db_connection("nested_form_dt_save")
|
||||||
@@ -512,11 +508,22 @@ server <- function(input, output, session) {
|
|||||||
log_action_to_db("saving data", values$main_key, con)
|
log_action_to_db("saving data", values$main_key, con)
|
||||||
|
|
||||||
showNotification(
|
showNotification(
|
||||||
"Данные успешно сохраннены",
|
"Данные успешно сохранены",
|
||||||
type = "message"
|
type = "message"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
## nested validation ------------------------------------
|
||||||
|
output$validation_nested <- renderText({
|
||||||
|
req(validator_nested())
|
||||||
|
|
||||||
|
if (validator_nested()$is_valid()) {
|
||||||
|
"✅ Данные внесены корректно"
|
||||||
|
} else {
|
||||||
|
"⚠️ Данные внесены не полностью или некорректно"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
## обновление данных при переключении ключей ------------
|
## обновление данных при переключении ключей ------------
|
||||||
observeEvent(input$nested_key_selector, {
|
observeEvent(input$nested_key_selector, {
|
||||||
req(input$nested_key_selector)
|
req(input$nested_key_selector)
|
||||||
@@ -620,7 +627,13 @@ server <- function(input, output, session) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
output$status_message2 <- renderText({
|
output$status_message2 <- renderText({
|
||||||
iv_main$is_valid()
|
req(validator_main())
|
||||||
|
|
||||||
|
if (validator_main()$is_valid()) {
|
||||||
|
"✅ Данные внесены корректно"
|
||||||
|
} else {
|
||||||
|
"⚠️ Данные внесены не полностью или некорректно"
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
# =========================================
|
# =========================================
|
||||||
@@ -673,6 +686,7 @@ server <- function(input, output, session) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
values$main_key <- new_main_key
|
values$main_key <- new_main_key
|
||||||
|
main_form_is_empty(FALSE)
|
||||||
log_action_to_db("creating new key", values$main_key, con)
|
log_action_to_db("creating new key", values$main_key, con)
|
||||||
utils$clean_forms("main", schm)
|
utils$clean_forms("main", schm)
|
||||||
|
|
||||||
@@ -697,8 +711,9 @@ server <- function(input, output, session) {
|
|||||||
observeEvent(input$clean_all_action, {
|
observeEvent(input$clean_all_action, {
|
||||||
|
|
||||||
# rewrite all inputs with empty data
|
# rewrite all inputs with empty data
|
||||||
utils$clean_forms("main", schm)
|
|
||||||
values$main_key <- NULL
|
values$main_key <- NULL
|
||||||
|
utils$clean_forms("main", schm)
|
||||||
|
main_form_is_empty(TRUE)
|
||||||
|
|
||||||
removeModal()
|
removeModal()
|
||||||
showNotification("Данные очищены!", type = "warning")
|
showNotification("Данные очищены!", type = "warning")
|
||||||
@@ -788,6 +803,8 @@ server <- function(input, output, session) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
values$main_key <- input$load_data_key_selector
|
values$main_key <- input$load_data_key_selector
|
||||||
|
main_form_is_empty(FALSE)
|
||||||
|
|
||||||
log_action_to_db("loading data", values$main_key, con = con)
|
log_action_to_db("loading data", values$main_key, con = con)
|
||||||
removeModal()
|
removeModal()
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#' @export
|
#' @export
|
||||||
make_list_of_pages <- function(main_schema, main_key_id) {
|
make_list_of_pages = function(main_schema, main_key_id) {
|
||||||
cli::cli_alert_success("AAAA")
|
cli::cli_alert_success("AAAA")
|
||||||
purrr::map(
|
purrr::map(
|
||||||
.x = unique(main_schema$part),
|
.x = unique(main_schema$part),
|
||||||
@@ -22,7 +22,7 @@ make_list_of_pages <- function(main_schema, main_key_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
make_panels <- function(scheme) {
|
make_panels = function(scheme) {
|
||||||
|
|
||||||
cards <- purrr::map(
|
cards <- purrr::map(
|
||||||
.x = unique(scheme$subgroup),
|
.x = unique(scheme$subgroup),
|
||||||
@@ -61,7 +61,7 @@ make_panels <- function(scheme) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
render_forms <- function(
|
render_forms = function(
|
||||||
form_id,
|
form_id,
|
||||||
form_label,
|
form_label,
|
||||||
form_type,
|
form_type,
|
||||||
@@ -242,7 +242,7 @@ render_forms <- function(
|
|||||||
#' @export
|
#' @export
|
||||||
#' @description
|
#' @description
|
||||||
#' Функция возращает пустое значение для каждого типа формы
|
#' Функция возращает пустое значение для каждого типа формы
|
||||||
get_empty_data <- function(type) {
|
get_empty_data = function(type) {
|
||||||
if (type %in% c("text", "select_one", "select_multiple")) return(as.character(NA))
|
if (type %in% c("text", "select_one", "select_multiple")) return(as.character(NA))
|
||||||
if (type %in% c("radio", "checkbox")) return(as.character(NA))
|
if (type %in% c("radio", "checkbox")) return(as.character(NA))
|
||||||
if (type %in% c("date")) return(as.Date(NA))
|
if (type %in% c("date")) return(as.Date(NA))
|
||||||
@@ -255,7 +255,7 @@ get_empty_data <- function(type) {
|
|||||||
#' @param type - type of form;
|
#' @param type - type of form;
|
||||||
#' @param value - value to update;
|
#' @param value - value to update;
|
||||||
#' @param local_delimeter - delimeter to split file
|
#' @param local_delimeter - delimeter to split file
|
||||||
update_forms_with_data <- function(
|
update_forms_with_data = function(
|
||||||
form_id,
|
form_id,
|
||||||
form_type,
|
form_type,
|
||||||
value,
|
value,
|
||||||
@@ -341,7 +341,7 @@ update_forms_with_data <- function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
clean_forms <- function(
|
clean_forms = function(
|
||||||
table_name,
|
table_name,
|
||||||
schm,
|
schm,
|
||||||
ns
|
ns
|
||||||
@@ -366,5 +366,4 @@ clean_forms <- function(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user