REFACTOR: Remove $.cookie in favor of a local library based on it (#10548)

REFACTOR: Remove $.cookie in favor of a local library based on it (#10548)

This helps us out in a few ways:

  1. It lessens our reliance on jQuery
  2. It’s slightly less code because it omits options we don’t use
  3. It is one less library to import and put into ES6 modules
diff --git a/app/assets/javascripts/discourse/app/components/create-account.js b/app/assets/javascripts/discourse/app/components/create-account.js
index e382244..f1d8c8f 100644
--- a/app/assets/javascripts/discourse/app/components/create-account.js
+++ b/app/assets/javascripts/discourse/app/components/create-account.js
@@ -1,12 +1,14 @@
 import Component from "@ember/component";
+import cookie from "discourse/lib/cookie";
+
 export default Component.extend({
   classNames: ["create-account"],
 
   didInsertElement() {
     this._super(...arguments);
 
-    if ($.cookie("email")) {
-      this.set("email", $.cookie("email"));
+    if (cookie("email")) {
+      this.set("email", cookie("email"));
     }
 
     $(this.element).on("keydown.discourse-create-account", e => {
diff --git a/app/assets/javascripts/discourse/app/components/global-notice.js b/app/assets/javascripts/discourse/app/components/global-notice.js
index e76a368..01e3636 100644
--- a/app/assets/javascripts/discourse/app/components/global-notice.js
+++ b/app/assets/javascripts/discourse/app/components/global-notice.js
@@ -4,6 +4,7 @@ import I18n from "I18n";
 import Component from "@ember/component";
 import LogsNotice from "discourse/services/logs-notice";
 import EmberObject, { computed } from "@ember/object";
+import cookie, { removeCookie } from "discourse/lib/cookie";
 
 const _pluginNotices = [];
 
@@ -67,8 +68,8 @@ export default Component.extend({
     function() {
       let notices = [];
 
-      if ($.cookie("dosp") === "1") {
-        $.removeCookie("dosp", { path: "/" });
+      if (cookie("dosp") === "1") {
+        removeCookie("dosp", { path: "/" });
         notices.push(
           Notice.create({
             text: I18n.t("forced_anonymous"),
diff --git a/app/assets/javascripts/discourse/app/components/group-membership-button.js b/app/assets/javascripts/discourse/app/components/group-membership-button.js
index e023776..f35b654 100644
--- a/app/assets/javascripts/discourse/app/components/group-membership-button.js
+++ b/app/assets/javascripts/discourse/app/components/group-membership-button.js
@@ -4,6 +4,7 @@ import discourseComputed from "discourse-common/utils/decorators";
 import { popupAjaxError } from "discourse/lib/ajax-error";
 import showModal from "discourse/lib/show-modal";
 import bootbox from "bootbox";
+import cookie from "discourse/lib/cookie";
 
 export default Component.extend({
   classNames: ["group-membership-button"],
@@ -30,7 +31,7 @@ export default Component.extend({
 
   _showLoginModal() {
     this.showLogin();
-    $.cookie("destination_url", window.location.href);
+    cookie("destination_url", window.location.href);
   },
 
   removeFromGroup() {
diff --git a/app/assets/javascripts/discourse/app/components/login-modal.js b/app/assets/javascripts/discourse/app/components/login-modal.js
index 2ee72f8..42b5580 100644
--- a/app/assets/javascripts/discourse/app/components/login-modal.js
+++ b/app/assets/javascripts/discourse/app/components/login-modal.js
@@ -1,5 +1,7 @@
 import { schedule } from "@ember/runloop";
 import Component from "@ember/component";
+import cookie from "discourse/lib/cookie";
+
 export default Component.extend({
   didInsertElement() {
     this._super(...arguments);
@@ -11,8 +13,8 @@ export default Component.extend({
         "loginPassword",
         $("#hidden-login-form input[name=password]").val()
       );
-    } else if ($.cookie("email")) {
-      this.set("loginName", $.cookie("email"));
+    } else if (cookie("email")) {
+      this.set("loginName", cookie("email"));
     }
 
     schedule("afterRender", () => {
diff --git a/app/assets/javascripts/discourse/app/controllers/create-account.js b/app/assets/javascripts/discourse/app/controllers/create-account.js
index 3c9c7fe..1b97128 100644
--- a/app/assets/javascripts/discourse/app/controllers/create-account.js
+++ b/app/assets/javascripts/discourse/app/controllers/create-account.js
@@ -21,6 +21,7 @@ import { findAll } from "discourse/models/login-method";
 import EmberObject from "@ember/object";
 import User from "discourse/models/user";
 import { Promise } from "rsvp";
+import cookie, { removeCookie } from "discourse/lib/cookie";
 
 export default Controller.extend(
   ModalFunctionality,
@@ -270,7 +271,7 @@ export default Controller.extend(
       const destinationUrl = this.get("authOptions.destination_url");
 
       if (!isEmpty(destinationUrl)) {
-        $.cookie("destination_url", destinationUrl, { path: "/" });
+        cookie("destination_url", destinationUrl, { path: "/" });
       }
 
       // Add the userfields to the data
@@ -326,12 +327,12 @@ export default Controller.extend(
               this.rejectedPasswords.pushObject(attrs.accountPassword);
             }
             this.set("formSubmitted", false);
-            $.removeCookie("destination_url");
+            removeCookie("destination_url");
           }
         },
         () => {
           this.set("formSubmitted", false);
-          $.removeCookie("destination_url");
+          removeCookie("destination_url");
           return this.flash(I18n.t("create_account.failed"), "error");
         }
       );
diff --git a/app/assets/javascripts/discourse/app/controllers/forgot-password.js b/app/assets/javascripts/discourse/app/controllers/forgot-password.js
index 1adaa5a..ad838d5 100644
--- a/app/assets/javascripts/discourse/app/controllers/forgot-password.js
+++ b/app/assets/javascripts/discourse/app/controllers/forgot-password.js
@@ -7,6 +7,7 @@ import ModalFunctionality from "discourse/mixins/modal-functionality";
 import { escapeExpression } from "discourse/lib/utilities";
 import { extractError } from "discourse/lib/ajax-error";
 import getURL from "discourse-common/lib/get-url";
+import cookie from "discourse/lib/cookie";
 
 export default Controller.extend(ModalFunctionality, {
   offerHelp: null,
@@ -18,8 +19,8 @@ export default Controller.extend(ModalFunctionality, {
   },
 
   onShow() {
-    if ($.cookie("email")) {
-      this.set("accountEmailOrUsername", $.cookie("email"));
+    if (cookie("email")) {
+      this.set("accountEmailOrUsername", cookie("email"));
     }
   },
 
diff --git a/app/assets/javascripts/discourse/app/controllers/login.js b/app/assets/javascripts/discourse/app/controllers/login.js
index f43886a..357bfde 100644
--- a/app/assets/javascripts/discourse/app/controllers/login.js
+++ b/app/assets/javascripts/discourse/app/controllers/login.js
@@ -17,6 +17,7 @@ import { extractError } from "discourse/lib/ajax-error";
 import { SECOND_FACTOR_METHODS } from "discourse/models/user";
 import { getWebauthnCredential } from "discourse/lib/webauthn";
 import bootbox from "bootbox";
+import cookie, { removeCookie } from "discourse/lib/cookie";
 
 // This is happening outside of the app via popup
 const AuthErrors = [
@@ -187,19 +188,19 @@ export default Controller.extend(ModalFunctionality, {
               hiddenLoginForm.querySelector(`input[name=${key}]`).value = value;
             };
 
-            const destinationUrl = $.cookie("destination_url");
-            const ssoDestinationUrl = $.cookie("sso_destination_url");
+            const destinationUrl = cookie("destination_url");
+            const ssoDestinationUrl = cookie("sso_destination_url");
 
             applyHiddenFormInputValue(this.loginName, "username");
             applyHiddenFormInputValue(this.loginPassword, "password");
 
             if (ssoDestinationUrl) {
-              $.removeCookie("sso_destination_url");
+              removeCookie("sso_destination_url");
               window.location.assign(ssoDestinationUrl);
               return;
             } else if (destinationUrl) {
               // redirect client to the original URL
-              $.removeCookie("destination_url");
+              removeCookie("destination_url");
 
               applyHiddenFormInputValue(destinationUrl, "redirect");
             } else {

[... diff too long, it was truncated ...]

GitHub sha: c172f206

1 Like

This commit appears in #10548 which was merged by eviltrout.