Ester Silva
Ester Silva

Reputation: 680

Showing notification before clicking on the actionButton

The following script needs to show an error if the username, password or email exist in the database. If they do not, it inserts them into the database and shows a notification.

It works but the problem is, if in the first attempt the user could insert successfully his information, in the next tries before clicking on the regist button (it is an actionButton), it shows the notification since the regist button is already clicked. So my question is: How to reset the value of the regist button after each click, or how to avoid showing the message and doing act before clicking on the regist button?

Appriciate!

observe({
    if (USER$registed == TRUE){
        if (!is.null(input$regist)){
            if (input$regist > 0){    
                if(is.null(input$uname) || input$uname == "" || is.null(input$pswd) || input$pswd == "" || is.null(input$email) || input$email == "")
                {
                    showNotification("You Forgot to insert your information!", duration = 3, type = c("error"))
                }else{
                    Username <- isolate(input$uname)
                    Password <- isolate(input$pswd)
                    Email <- isolate(input$email)
                    db <- dbConnect(SQLite(), dbname="db.sqlite")
                    existed <- RSQLite::dbGetQuery(db, sprintf({"SELECT rowid FROM users WHERE username='%s' OR password ='%s' OR email='%s'"}, Username, Password, Email, serialize=F))
                    if (nrow(existed)>=1 ) 
                    {
                        showNotification("This Username, Password or Email already exist in the system. Please try something else!", duration = 4, type = c("error"))
                    }else{
                        dbSendQuery(db, sprintf({"INSERT INTO users (username,password,email) VALUES ('%s','%s','%s')"},Username, Password,Email))
                        MAX_studentID <- dbGetQuery(db,"SELECT MAX(studentID) FROM UserID_Map")
                        New_user_StudentID <- as.integer(MAX_studentID)+1
                        dbSendQuery(db, sprintf({"INSERT INTO UserID_Map (username,studentID) VALUES ('%s','%s')"},Username, New_user_StudentID))
                        showNotification("Successful Registeration. You can Login!", duration = 4, type = c("message"))
                    }
                    RSQLite::dbDisconnect(db)
                }
            }
        }
    }
})

Upvotes: 1

Views: 37

Answers (1)

Florian
Florian

Reputation: 25405

Maybe making it an observeEvent would solve your issue? In that way, the code only fires when someone clicks the actionButton. Example:

observeEvent(input$regist, {
    if (USER$registed == TRUE){
        if (!is.null(input$regist)){
            if (input$regist > 0){    
                if(is.null(input$uname) || input$uname == "" || is.null(input$pswd) || input$pswd == "" || is.null(input$email) || input$email == "")
                {
                    showNotification("You Forgot to insert your information!", duration = 3, type = c("error"))
                }else{
                    Username <- isolate(input$uname)
                    Password <- isolate(input$pswd)
                    Email <- isolate(input$email)
                    db <- dbConnect(SQLite(), dbname="db.sqlite")
                    existed <- RSQLite::dbGetQuery(db, sprintf({"SELECT rowid FROM users WHERE username='%s' OR password ='%s' OR email='%s'"}, Username, Password, Email, serialize=F))
                    if (nrow(existed)>=1 ) 
                    {
                        showNotification("This Username, Password or Email already exist in the system. Please try something else!", duration = 4, type = c("error"))
                    }else{
                        dbSendQuery(db, sprintf({"INSERT INTO users (username,password,email) VALUES ('%s','%s','%s')"},Username, Password,Email))
                        MAX_studentID <- dbGetQuery(db,"SELECT MAX(studentID) FROM UserID_Map")
                        New_user_StudentID <- as.integer(MAX_studentID)+1
                        dbSendQuery(db, sprintf({"INSERT INTO UserID_Map (username,studentID) VALUES ('%s','%s')"},Username, New_user_StudentID))
                        showNotification("Successful Registeration. You can Login!", duration = 4, type = c("message"))
                    }
                    RSQLite::dbDisconnect(db)
                }
            }
        }
    }
})

Hope this helps!

Upvotes: 1

Related Questions