inst/hbcdtlfb/www/R/observe/add_substance_P4.R

substance <- input$substance_P4
frequency <- input$frequency_P4
add_calendar <- TRUE

if(is.null(input$substance_date_P4)) {
  show_toast(title = "Missing dates", text = "", type = "error", 
             position = "center", width = "50%")
  add_calendar <- FALSE
}
if(substance == "") {
  show_toast(title = "Missing substance", text = "", type = "error", 
             position = "center", width = "50%")
  add_calendar <- FALSE
}

if(frequency == "") {
  show_toast(title = "Missing frequency", text = "", type = "error", 
             position = "center", width = "50%")
  add_calendar <- FALSE
}

if(add_calendar) {
  
  for (day in as.character(input$substance_date_P4)) {
    timevis_data_cal(
      timevis_data_cal() |> 
        add_row(
          tibble(
            id = randomID(),
            calendarId = substances$calendar_id[substances$name_short_html == substance],
            period = "Last Two Weeks Before Delivery",
            what = "Substance",
            event = NA,
            substance = substance,
            frequency = frequency,
            title = glue("{frequency} {substance}"),
            body = "",
            recurrenceRule = NA,
            start = as.Date(day),
            end = as.Date(day), 
            category = "allday",
            location = NA,
            bgColor = NA,
            color = NA,
            borderColor = NA
          )
        )
    )
  }
  
  # Reset substance and frequency inputs and save on AWS
  if(input$all_substances) {
    updateVirtualSelect(
      session = session, 
      "substance_P4", 
      "Substance:",
      choices = substances |> 
        prepare_choices(name_short_html, name_short_html, group), 
      selected = ""
    )
  }
  
  if(! input$all_substances) {
    updateVirtualSelect(
      session = session, 
      "substance_P4", 
      "Substance", 
      choices = substances_names_grouped(), 
      selected = ""
    )
  }
  
  updateVirtualSelect(
    session = session, 
    "frequency_P4", 
    "Frequency", 
    choices = 1:100, 
    selected = character(0)
  )
  
  save_aws(
    data = timevis_data_cal(),
    data_table_routine = table_substance_routine_data(),
    id = tlfb_id(),
    v = tlfb_v(),
    last_action = "add_substance",
    session = session
  )
  
}
ucsd-dsm/hbcd-tlfb documentation built on July 10, 2022, 5:46 p.m.