Group 2: Exercise 3 (Server)

ymdDateServer <- function(id){
  moduleServer(id, function(input, output, session) {
    
    # Validate and convert the input date
    # Only runs when the "Confirm Date" button is clicked
    date_result <- reactive({
      result <- strptime(input$date, "%Y-%m-%d") |> as.Date()
      if(is.na(result)) result <- NULL
      result
    }) |> bindEvent(input$check) # (OPTIONAL)
    
    # Display an error message if the date is invalid
    # Only updates when the "Confirm Date" button is clicked
    output$error <- renderText({
      if(!is.null(date_result())) return(NULL)
      paste0(input$date, " doesn't match with the expected pattern")
    }) |> bindEvent(input$check) # (OPTIONAL)
    
    return(date_result)
  })
}