Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion R/data_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@ WarningHandler <- R6::R6Class(

# Store warning messages
warning_messages = NULL,
# Store critical error messages
critical_messages = NULL,
invalid_sheets_name = NULL,


# Initialize
initialize = function() {
self$warning_messages <- reactiveValues()
self$critical_messages <- reactiveValues()
},

# Method to add a warning
Expand All @@ -277,7 +279,46 @@ WarningHandler <- R6::R6Class(
if (!is.null(warning_code)) {
self$invalid_sheets_name[[config_file]] <- c(self$invalid_sheets_name[[config_file]], sheet_name)
}
},

# Method to add a critical error
add_critical_error = function(config_file, category, message) {
self$critical_messages$config_files <- c(self$critical_messages$config_files, config_file)
error_msg <- sprintf("<b>[%s]</b> %s", category, message)
self$critical_messages[[config_file]] <- c(self$critical_messages[[config_file]], error_msg)
},

# Check if any critical errors exist
has_critical_errors = function() {
length(self$critical_messages$config_files) > 0
},

# Clear all warnings and critical errors
clear = function() {
self$warning_messages <- reactiveValues()
self$critical_messages <- reactiveValues()
self$invalid_sheets_name <- NULL
},

# Populate from esqlabsR validationResults
populate_from_validation = function(validationResults) {
for (file_name in names(validationResults)) {
result <- validationResults[[file_name]]
if (!inherits(result, "validationResult")) next

for (err in result$critical_errors) {
self$add_critical_error(file_name, err$category, err$message)
}
for (warn in result$warnings) {
self$warning_messages$config_files <- c(
self$warning_messages$config_files, file_name
)
warning_msg <- sprintf("<b>[%s]</b> %s", warn$category, warn$message)
self$warning_messages[[file_name]] <- c(
self$warning_messages[[file_name]], warning_msg
)
}
}
}
)
)
29 changes: 29 additions & 0 deletions R/mod_export.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,37 @@ mod_export_server <- function(id, r, configuration_path) {
)


# Disable export when critical errors exist
observeEvent(r$states$has_critical_errors, {
if (isTRUE(r$states$has_critical_errors)) {
updateActionButton(session, inputId = "export", disabled = TRUE)
} else {
updateActionButton(session, inputId = "export", disabled = FALSE)
}
})

# Listen to the export button
observeEvent(input$export, {
# Re-validate before exporting
if (!is.null(r$config$projectConfiguration)) {
validationResults <- esqlabsR::validateAllConfigurations(
r$config$projectConfiguration
)
if (esqlabsR::isAnyCriticalErrors(validationResults)) {
r$warnings$clear()
r$warnings$populate_from_validation(validationResults)
r$states$has_critical_errors <- TRUE
r$states$modal_message <- list(
status = "Export Blocked",
message = paste(
"Critical validation errors found.",
"Please fix the errors and try again."
)
)
return()
}
}

message("exporting data")

export_success <- TRUE # Track overall success
Expand Down
45 changes: 44 additions & 1 deletion R/mod_import.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ mod_import_server <- function(id, r, DROPDOWNS) {
)

# Clear warnings before reload
r$warnings$warning_messages <- reactiveValues()
r$warnings$clear()

# Re-read all files and reset modified data
for (config_file in r$data$get_config_files()) {
Expand Down Expand Up @@ -160,6 +160,22 @@ mod_import_server <- function(id, r, DROPDOWNS) {
# Update dropdowns with fresh data
runAfterConfig()

# Re-run esqlabsR validators
validationResults <- esqlabsR::validateAllConfigurations(
r$config$projectConfiguration
)
r$warnings$populate_from_validation(validationResults)

if (esqlabsR::isAnyCriticalErrors(validationResults)) {
r$states$has_critical_errors <- TRUE
showNotification(
"Critical validation errors found. Export is blocked.",
type = "error"
)
} else {
r$states$has_critical_errors <- FALSE
}

# Trigger UI refresh to update the tables
r$ui_triggers$selected_sheet <- runif(1)

Expand Down Expand Up @@ -214,6 +230,33 @@ mod_import_server <- function(id, r, DROPDOWNS) {

# if no data file or no sheets
runAfterConfig()

# Run esqlabsR validators
validationResults <- esqlabsR::validateAllConfigurations(pc)
r$warnings$populate_from_validation(validationResults)

if (esqlabsR::isAnyCriticalErrors(validationResults)) {
summary <- esqlabsR::validationSummary(validationResults)
r$states$has_critical_errors <- TRUE
showModal(modalDialog(
title = "Critical Validation Errors",
tagList(
tags$p(tags$strong(paste0(
summary$total_critical_errors, " critical error(s) found."
))),
tags$p(
"The imported configuration has critical errors that must be",
"fixed before you can export. Click the",
icon("circle-exclamation"),
"icon to view details."
)
),
easyClose = TRUE,
footer = modalButton("OK")
))
} else {
r$states$has_critical_errors <- FALSE
}
})

# Show confirmation modal when reload button is clicked
Expand Down
107 changes: 80 additions & 27 deletions R/mod_warning_modal.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod_warning_server <- function(id, r) {
observeEvent(input$open_warning_modal, {
showModal(
modalDialog(
title = "Warning",
title = "Validation Results",
uiOutput(ns("warning_accordion")),
easyClose = TRUE,
footer = tagList(
Expand All @@ -41,45 +41,98 @@ mod_warning_server <- function(id, r) {
)
})

observeEvent(r$warnings$warning_messages$config_files, {
# Watch for critical errors
observeEvent(r$warnings$critical_messages$config_files, {
updateActionButton(
session = session,
inputId = "open_warning_modal",
icon = icon("warning"),
icon = icon("circle-exclamation"),
disabled = FALSE
)

showNotification(
HTML(paste("Warnings found in config files. Press the", icon("warning"), "to view them")),
type = "warning",
duration = 7
HTML(paste(
"Critical errors found in config files. Press the",
icon("circle-exclamation"), "to view them"
)),
type = "error",
duration = 10
)

output$warning_accordion <- renderUI({
items <- lapply(unique(r$warnings$warning_messages$config_files), function(x) {
# Check if there are messages for the current config file
if (length(r$warnings$warning_messages[[x]]) > 0) {
# Loop through each message inside the vector and create a list item
bullet_list <- sapply(r$warnings$warning_messages[[x]], function(msg) {
paste0("<li>", msg, "</li>")
})
# Combine the bullet list items into a single string
bullet_list_content <- paste(bullet_list, collapse = "")
# Wrap the content in an unordered list <ul> and pass to the accordion panel
accordion_panel(paste("File: ", x), HTML(paste("<ul>", bullet_list_content, "</ul>")))
} else {
# In case there are no messages, just show a message in the accordion panel
accordion_panel(paste("File: ", x), "No warnings.")
}
})

# Return the accordion with the items
accordion(!!!items, multiple = FALSE)
})
.render_accordion(output, ns, r)
})

# Watch for warnings (only update icon if no critical errors)
observeEvent(r$warnings$warning_messages$config_files, {
if (!r$warnings$has_critical_errors()) {
updateActionButton(
session = session,
inputId = "open_warning_modal",
icon = icon("triangle-exclamation"),
disabled = FALSE
)

showNotification(
HTML(paste(
"Warnings found in config files. Press the",
icon("triangle-exclamation"), "to view them"
)),
type = "warning",
duration = 7
)
}

.render_accordion(output, ns, r)
})

observeEvent(input$close_warning_modal, {
removeModal()
})
})
}

#' Render the warning/error accordion into an output
#' @noRd
.render_accordion <- function(output, ns, r) {
output$warning_accordion <- renderUI({
# Collect all config files that have issues
all_files <- unique(c(
r$warnings$critical_messages$config_files,
r$warnings$warning_messages$config_files
))

items <- lapply(all_files, function(x) {
content_parts <- list()

# Critical errors section
critical_msgs <- r$warnings$critical_messages[[x]]
if (length(critical_msgs) > 0) {
bullets <- paste0("<li>", critical_msgs, "</li>", collapse = "")
content_parts <- c(content_parts, list(HTML(paste0(
"<div style='color: #dc3545; margin-bottom: 8px;'>",
"<strong>Critical Errors:</strong>",
"<ul>", bullets, "</ul></div>"
))))
}

# Warnings section
warning_msgs <- r$warnings$warning_messages[[x]]
if (length(warning_msgs) > 0) {
bullets <- paste0("<li>", warning_msgs, "</li>", collapse = "")
content_parts <- c(content_parts, list(HTML(paste0(
"<div style='color: #856404;'>",
"<strong>Warnings:</strong>",
"<ul>", bullets, "</ul></div>"
))))
}

if (length(content_parts) == 0) {
content_parts <- list("No issues.")
}

accordion_panel(paste("File:", x), tagList(content_parts))
})

accordion(!!!items, multiple = FALSE)
})
}
Loading