From 4e34a9b6a98e42e461654be8eb02e55ea8589194 Mon Sep 17 00:00:00 2001
From: MattIPv4 <me@mattcowley.co.uk>
Date: Wed, 3 Jun 2020 16:43:35 +0100
Subject: [PATCH] Fix dupe domain name issues

---
 src/nginxconfig/templates/app.vue               | 17 ++++++++++++++---
 .../templates/domain_sections/server.vue        |  7 +++----
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/nginxconfig/templates/app.vue b/src/nginxconfig/templates/app.vue
index 6ee0349..1b75a8d 100644
--- a/src/nginxconfig/templates/app.vue
+++ b/src/nginxconfig/templates/app.vue
@@ -70,8 +70,8 @@ limitations under the License.
                 <div :class="`column ${splitColumn ? 'is-half' : 'is-full'} is-full-mobile is-full-tablet`">
                     <h2>{{ i18n.templates.app.configFiles }}</h2>
                     <div ref="files" class="columns is-multiline">
-                        <NginxPrism v-for="conf in confFilesOutput"
-                                    :key="`${conf[0]}-${hash(conf[1])}`"
+                        <NginxPrism v-for="(conf, i) in confFilesOutput"
+                                    :key="`${conf[0]}-${i}-${hash(conf[1])}`"
                                     :name="`${nginxDir}/${conf[0]}`"
                                     :conf="conf[1]"
                                     :half="confFilesOutput.length > 1 && !splitColumn"
@@ -169,7 +169,18 @@ limitations under the License.
                 return '';
             },
             add() {
-                this.$data.domains.push(clone(Domain.delegated));
+                const data = clone(Domain.delegated);
+
+                // Avoid dupe domains
+                let count = 1;
+                while (this.$data.domains.some(d => d && d.server.domain.computed === data.server.domain.computed)) {
+                    count++;
+                    data.server.domain.computed = data.server.domain.default.replace('.com', `${count}.com`);
+                }
+                data.server.domain.value = data.server.domain.computed;
+
+                // Store
+                this.$data.domains.push(data);
                 this.$data.active = this.$data.domains.length - 1;
             },
             remove(index) {
diff --git a/src/nginxconfig/templates/domain_sections/server.vue b/src/nginxconfig/templates/domain_sections/server.vue
index c6e7d12..a7789fe 100644
--- a/src/nginxconfig/templates/domain_sections/server.vue
+++ b/src/nginxconfig/templates/domain_sections/server.vue
@@ -182,15 +182,14 @@ limitations under the License.
                 i18n,
             };
         },
-        computed: computedFromDefaults(defaults, 'server'), // Getters & setters for the delegated data
+        computed: computedFromDefaults(defaults, 'server'),     // Getters & setters for the delegated data
         watch: {
             '$props.data.domain': {
                 handler(data) {
-                    // This might cause recursion, but seems not to
-
-                    // Ignore www. if given
+                    // Ignore www. if given, enable WWW subdomain
                     if (data.computed.startsWith('www.')) {
                         data.computed = data.computed.slice(4);
+                        this.wwwSubdomain = true;
                     }
 
                     // Use default if empty