Harshit Luthra avatar

Susi-Chromebot: Add "Sync Settings While User Logs in & update settings "

sachincool

Published: 06 Jan 2018 › Updated: 06 Jan 2018Susi-Chromebot: Add "Sync Settings While User Logs in & update settings "

Susi-Chromebot: Add "Sync Settings While User Logs in & update settings "

susi_chromebot.gif

Summary: Added a Feature in Susi-chromebot Which is under Fossasia Org.

  • Susi-chrombot is part of one of the projects which use Susi You can find more about it :- Here.
  • Susi is maintained by Fossasia, It has several other projects like Susi-Android, Web-app,Mozzila extenstion and this.
  • We use susi api endpoints which are specified in this repo susi-repo
  • If you want your own Custom Skill for susi you can go to susi-skill And release your skills, much like how google,alexa have their own skill building crowd.

This was an intro to the project, Now onto my contributions :D

I took on this Issue . At first it seemed fairly simple, We just have to do these steps :-

  • Use api endpoint to check if the user has theme settings saved
  • If he/she did, apply them in your local.storage or chrome.storage
  • Also check if a user toggles the theme button && isLoggedIn in that case we'll have to save the settings again in the api call.
  • find bugs and conquer them --> toughest task right next after Indenting your code
    1.png
    Later on i understood the situation i got myself into, and i was ready to tackle it :- here's a sneakpeak If you want to check the whole code go here
  var loginEndPoint = BASE_URL + "/aaa/login.json?type=access-token&login=" + encodeURIComponent(email) + "&password=" + encodeURIComponent(password);
     $.ajax({ // Checks if the user has pevious UserSettings to update or not
         url: loginEndPoint,
         dataType: "jsonp",
         jsonpCallback: "p",
         jsonp: "callback",
         crossDomain: true,
         success: function(response) {
             if (response.accepted) {
                 accessToken = response.access_token;
                 var listUserSettings = BASE_URL + "/aaa/listUserSettings.json?access_token=" + accessToken;
               checkLogin = "true";
                 localStorage.setItem("checkLogin", checkLogin);
                 chrome.storage.sync.set({
                     loggedUser: {
                         email: email,
                         accessToken: accessToken
                     }
                 }); // Ends here ~~~~~!
                time = response.validSeconds;
                loginButton.innerHTML = "Login";
                $("#loginbutton").button("reset");
                alert(response.message);
                $.ajax({
                    url: listUserSettings,
                    dataType: "jsonp",
                    jsonpCallback: "p",
                    jsonp: "callback",
                    crossDomain: "true",
                    success: function(response) {
                        if (response.accepted) {
                            if (response.settings.theme != null) {
                                var userThemeValue = response.settings.theme;
                                if (defaultThemeValue !== userThemeValue) {
                                     defaultThemeValue = userThemeValue;
                                     if (defaultThemeValue === "dark") {
                                        chrome.storage.sync.set({
                                             "darktheme": true
                                         }, () => {});
                                     } else {
                                         chrome.storage.sync.set({
                                             "darktheme": false
                                         }, () => {});
                                     }
                            }

2.png

the endpoints and the docs did a great help

  • send creds to login api and get access-token
  • send settings to changesettings endpoint with access-token and get a json
  • send a request to listsettings endpoint with access-token and get json
    3.png

My main motive was to have minimal amount of ajax requests and get the job done !!

P.s i did it in 2 . i don't this it can get any lower
Thanks for reading, Cheers ~!!



Posted on Utopian.io - Rewarding Open Source Contributors

Leave Susi-Chromebot: Add "Sync Settings While User Logs in & update settings " to:

Written by

*,..,* Sarcasm is my bitch ~!! I like my chocolates just like my humour....... Dark Very Very Dark

Read more #utopian-io posts


Best Posts From Harshit Luthra

We have not curated any of sachincool's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Harshit Luthra