Day 28 – Updating User’s List When User Joins App

I’m building an app where I want users to be able to “join” the app. The plan is to eventually have this Kynetx app be a Kynetx powered Twilio app that users can sign up to get reminders based on their current status for the month. This example doesn’t cover users “leaving” the app.

ruleset a60x517 {
  meta {
    name "user-join-quit-data-hash"
    description <<
      user-join-quit-data-hash
    >>
    author "Mike Grace"
    logging on
  }

  rule running {
    select when pageview ".*"
    pre {
      joinStuff =<<
        Phone: <input id="phone" /><br/>
        Name: <input id="name" /><br/>
        <button id="join">Join</button>
      >>;
    }
    {
      append("body", joinStuff);
      emit <|
        // get app object for raising web event
        app = KOBJ.get_application("a60x517");

        // get user info when they click join
        $K("#join").live("click", function() {
          var phone = $K("#phone").val();
          var userName = $K("#name").val();

          // raise web event to submit join info
          app.raise_event("user_wants_to_join", {"phone":phone, "userName":userName});
        });
      |>;
    }
  }

  rule user_wants_to_join {
    select when web user_wants_to_join
    pre {

      // get data passed from web event
      phone = event:param("phone");
      userName = event:param("userName");

      // get time stuff for data hash
      month = time:strftime(time:now({"tz":"America/Denver"}), "%B");
      year = time:strftime(time:now({"tz":"America/Denver"}), "%Y");

      // get data hash to merge in new data
      dataHash = app:dataHash || {};

      // build user's hash that wants to join
      userData = {
        "#{phone}": {
          "name": "#{userName}",
          "yearMonth": {
            "#{year}#{month}": {
              "scheduled": false,
              "completed": false,
              "scheduledDay": false
            }
          }
        }
      };

      // morge user's hash with all other user's hash
      newDataHash = dataHash.put(userData);

    } // end of pre
    {
      notify("added to app","content") with sticky = true;
      emit <|
        console.log(userData);
        console.log(newDataHash);
      |>;
    }
    fired {
      set app:dataHash newDataHash;
    }
  }

}
  • 14-18 form for user to submit to “join” app
  • 21 put form on the page
  • 27-32 submit form when user clicks “join”
  • 39 select when the user submits form to “join”
  • 43-44 get user data from submitted form
  • 47-48 get month and year for creating hash data
  • 51 get the saved hash of users who have previously joined the app
  • 54-65 build hash of data for new user that wants to join
  • 68 build new hash of data by merging user who wants to join with data of those previously joined
  • 72 let user know that the form was accepted
  • 74-75 print hash object to console for debug and demonstration
  • 79 save new hash to the app with all the happy user’s data

Form submitted on example.com after several previous test runs of running app with bookmarklet

Console output after form submission

Get the bookmarklet to try it out yourself!

Gratuitous day 28 Grace face

This entry was posted in Kynetx. Bookmark the permalink.

Leave a comment