From bc9252f410262fb6f07f22c0edf0f4a00b388650 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Wed, 20 Oct 2021 21:54:22 +0530 Subject: [PATCH] Autogenerate subscriber name from e-mail on the UI if it's empty. Closes #525. --- frontend/src/utils.js | 2 ++ frontend/src/views/SubscriberForm.vue | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/frontend/src/utils.js b/frontend/src/utils.js index c20e6eed2..89c351381 100644 --- a/frontend/src/utils.js +++ b/frontend/src/utils.js @@ -103,6 +103,8 @@ export default class Utils { // https://stackoverflow.com/a/12034334 escapeHTML = (html) => html.replace(/[&<>"'`=/]/g, (s) => htmlEntities[s]); + titleCase = (str) => str[0].toUpperCase() + str.substr(1).toLowerCase(); + // UI shortcuts. confirm = (msg, onConfirm, onCancel) => { Dialog.confirm({ diff --git a/frontend/src/views/SubscriberForm.vue b/frontend/src/views/SubscriberForm.vue index 372bcdd2c..f0a87cda7 100644 --- a/frontend/src/views/SubscriberForm.vue +++ b/frontend/src/views/SubscriberForm.vue @@ -165,6 +165,18 @@ export default Vue.extend({ }, onSubmit() { + // If there is no name, auto-generate one from the e-mail. + if (!this.form.name) { + let name = ''; + [name] = this.form.email.toLowerCase().split('@'); + + if (name.includes('.')) { + this.form.name = name.split('.').map((c) => this.$utils.titleCase(c)).join(' '); + } else { + this.form.name = this.$utils.titleCase(name); + } + } + if (this.isEditing) { this.updateSubscriber(); return;