|
3 | 3 | #' @param input,output,session Internal parameters for {shiny}. |
4 | 4 | #' DO NOT REMOVE. |
5 | 5 | #' @import shiny |
| 6 | +#' @importFrom dplyr filter pull tibble |
6 | 7 | #' @noRd |
7 | 8 | app_server <- function(input, output, session) { |
8 | 9 |
|
9 | | - rv <- reactiveValues() |
| 10 | + rv <- reactiveValues( |
| 11 | + supabase_url = Sys.getenv("shinyrealtime_supabase_url"), |
| 12 | + supabase_key = Sys.getenv("shinyrealtime_supabase_key") |
| 13 | + ) |
| 14 | + |
| 15 | + observeEvent(TRUE, once = TRUE, { |
| 16 | + cat_where(whereami()) |
10 | 17 |
|
11 | | - observeEvent(TRUE, { |
12 | | - rv$data <- data.frame( |
13 | | - name = c("Alice", "Bob", "Charlie"), |
14 | | - age = c(25, 30, 35) |
| 18 | + session$sendCustomMessage( |
| 19 | + type = "supabaseConfig", |
| 20 | + list( |
| 21 | + url = rv$supabase_url, |
| 22 | + key = rv$supabase_key |
| 23 | + ) |
15 | 24 | ) |
| 25 | + |
| 26 | + if (getOption("golem.app.prod")) { |
| 27 | + |
| 28 | + rv$connected_user <- session$user |
| 29 | + |
| 30 | + body <- jsonlite::toJSON( |
| 31 | + list( |
| 32 | + name = rv$connected_user, |
| 33 | + is_connected = TRUE, |
| 34 | + is_first_visit = FALSE |
| 35 | + ), |
| 36 | + auto_unbox = TRUE |
| 37 | + ) |
| 38 | + |
| 39 | + response <- httr::PATCH( |
| 40 | + url = paste0(rv$supabase_url, "/rest/v1/users?", "name=eq.", rv$connected_user), |
| 41 | + httr::add_headers( |
| 42 | + "apikey" = rv$supabase_key, |
| 43 | + "Content-Type" = "application/json", |
| 44 | + "Prefer" = "return=representation" |
| 45 | + ), |
| 46 | + body = body, |
| 47 | + encode = "json" |
| 48 | + ) |
| 49 | + |
| 50 | + } else { |
| 51 | + rv$connected_user <- Sys.getenv("USER") |
| 52 | + } |
| 53 | + |
| 54 | + if (getOption("golem.app.prod")) { |
| 55 | + |
| 56 | + response <- httr::GET( |
| 57 | + url = paste0(rv$supabase_url, "/rest/v1/users"), |
| 58 | + httr::add_headers( |
| 59 | + "apikey" = rv$supabase_key, |
| 60 | + "Content-Type" = "application/json", |
| 61 | + "Prefer" = "return=representation" |
| 62 | + ), |
| 63 | + body = body, |
| 64 | + encode = "json" |
| 65 | + ) |
| 66 | + |
| 67 | + if (httr::status_code(response) == 200) { |
| 68 | + rv$users <- jsonlite::fromJSON(rawToChar(response$content)) |
| 69 | + } |
| 70 | + |
| 71 | + } else { |
| 72 | + rv$users <- data.frame( |
| 73 | + id = c(1, 2, 3), |
| 74 | + name = c("user1", "user2", "user3"), |
| 75 | + is_connected = c(FALSE, FALSE, FALSE), |
| 76 | + is_first_visit = c(FALSE, FALSE, FALSE) |
| 77 | + ) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + observeEvent(input$user_update, { |
| 82 | + cat_where(whereami()) |
| 83 | + |
| 84 | + if (getOption("golem.app.prod")) { |
| 85 | + |
| 86 | + new <- tibble( |
| 87 | + id = input$user_update$new$id, |
| 88 | + is_connected = input$user_update$new$is_connected, |
| 89 | + ) |
| 90 | + |
| 91 | + rv$users <- dplyr::rows_update( |
| 92 | + rv$users, |
| 93 | + new, |
| 94 | + by = "id" |
| 95 | + ) |
| 96 | + |
| 97 | + } else { |
| 98 | + rv$users <- data.frame( |
| 99 | + id = c(1, 2, 3), |
| 100 | + name = c("user1", "user2", "user3"), |
| 101 | + is_connected = c(TRUE, FALSE, FALSE), |
| 102 | + is_first_visit = c(FALSE, FALSE, FALSE) |
| 103 | + ) |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + |
| 108 | + session$onSessionEnded(function() { |
| 109 | + cat_where(whereami()) |
| 110 | + |
| 111 | + if (getOption("golem.app.prod")) { |
| 112 | + |
| 113 | + body <- jsonlite::toJSON( |
| 114 | + list( |
| 115 | + is_connected = FALSE |
| 116 | + ), |
| 117 | + auto_unbox = TRUE |
| 118 | + ) |
| 119 | + |
| 120 | + response <- httr::PATCH( |
| 121 | + url = paste0(rv$supabase_url, "/rest/v1/users?", "name=eq.", rv$connected_user), |
| 122 | + httr::add_headers( |
| 123 | + "apikey" = rv$supabase_key, |
| 124 | + "Content-Type" = "application/json", |
| 125 | + "Prefer" = "return=representation" |
| 126 | + ), |
| 127 | + body = body, |
| 128 | + encode = "json" |
| 129 | + ) |
| 130 | + |
| 131 | + } else { |
| 132 | + rv$users <- data.frame( |
| 133 | + id = c(1, 2, 3), |
| 134 | + name = c("user1", "user2", "user3"), |
| 135 | + is_connected = c(FALSE, FALSE, FALSE), |
| 136 | + is_first_visit = c(FALSE, FALSE, FALSE) |
| 137 | + ) |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + observeEvent(input$user_inactive, { |
| 142 | + cat_where(whereami()) |
| 143 | + |
| 144 | + showModal(modalDialog( |
| 145 | + title = "You are inactive", |
| 146 | + "You've been inactive for a while. Please refresh the page to continue.", |
| 147 | + easyClose = FALSE, |
| 148 | + footer = NULL |
| 149 | + )) |
| 150 | + |
| 151 | + if (getOption("golem.app.prod")) { |
| 152 | + |
| 153 | + body <- jsonlite::toJSON( |
| 154 | + list( |
| 155 | + is_connected = FALSE |
| 156 | + ), |
| 157 | + auto_unbox = TRUE |
| 158 | + ) |
| 159 | + |
| 160 | + response <- httr::PATCH( |
| 161 | + url = paste0(rv$supabase_url, "/rest/v1/users?", "name=eq.", rv$connected_user), |
| 162 | + httr::add_headers( |
| 163 | + "apikey" = rv$supabase_key, |
| 164 | + "Content-Type" = "application/json", |
| 165 | + "Prefer" = "return=representation" |
| 166 | + ), |
| 167 | + body = body, |
| 168 | + encode = "json" |
| 169 | + ) |
| 170 | + |
| 171 | + } else { |
| 172 | + rv$users <- data.frame( |
| 173 | + id = c(1, 2, 3), |
| 174 | + name = c("user1", "user2", "user3"), |
| 175 | + is_connected = c(FALSE, FALSE, FALSE), |
| 176 | + is_first_visit = c(FALSE, FALSE, FALSE) |
| 177 | + ) |
| 178 | + } |
| 179 | + |
16 | 180 | }) |
17 | 181 |
|
18 | 182 | mod_navbar_server( |
|
0 commit comments