From 7320de184b9e783dad33d0fbb5e157917f682e3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Julius=20H=C3=A4rtl?= <jus@bitgrid.net>
Date: Mon, 30 Dec 2019 13:22:49 +0100
Subject: [PATCH] Refactor validity propagation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Julius Härtl <jus@bitgrid.net>
---
 apps/workflowengine/src/components/Check.vue  |  9 ++++---
 .../src/components/Checks/RequestTime.vue     | 17 ++++++++++---
 apps/workflowengine/src/components/Rule.vue   | 25 ++++++++-----------
 3 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/apps/workflowengine/src/components/Check.vue b/apps/workflowengine/src/components/Check.vue
index e447fbeb4bd..c15907852e8 100644
--- a/apps/workflowengine/src/components/Check.vue
+++ b/apps/workflowengine/src/components/Check.vue
@@ -25,7 +25,7 @@
 			class="option"
 			@input="updateCheck"
 			@valid="(valid=true) && validate()"
-			@invalid="(valid=false) && validate()" />
+			@invalid="!(valid=false) && validate()" />
 		<input v-else
 			v-model="check.value"
 			type="text"
@@ -111,7 +111,7 @@ export default {
 		if (this.check.class === null) {
 			this.$nextTick(() => this.$refs.checkSelector.$el.focus())
 		}
-		this.check.invalid = !this.validate()
+		this.validate()
 	},
 	methods: {
 		showDelete() {
@@ -124,7 +124,8 @@ export default {
 			if (this.currentOption && this.currentOption.validate) {
 				this.valid = !!this.currentOption.validate(this.check)
 			}
-			return this.valid
+			this.check.invalid = !this.valid
+			this.$emit('validate', this.valid)
 		},
 		updateCheck() {
 			const matchingOperator = this.operators.findIndex((operator) => this.check.operator === operator.operator)
@@ -134,7 +135,7 @@ export default {
 			this.check.class = this.currentOption.class
 			this.check.operator = this.currentOperator.operator
 
-			this.check.invalid = !this.validate()
+			this.validate()
 
 			this.$emit('update', this.check)
 		},
diff --git a/apps/workflowengine/src/components/Checks/RequestTime.vue b/apps/workflowengine/src/components/Checks/RequestTime.vue
index df859c01866..42f5ff20671 100644
--- a/apps/workflowengine/src/components/Checks/RequestTime.vue
+++ b/apps/workflowengine/src/components/Checks/RequestTime.vue
@@ -50,6 +50,9 @@ export default {
 			},
 		}
 	},
+	mounted() {
+		this.validate()
+	},
 	methods: {
 		updateInternalValue(value) {
 			try {
@@ -66,17 +69,23 @@ export default {
 			}
 		},
 		validate() {
-			return this.newValue.startTime && this.newValue.startTime.match(/^(0[0-9]|1[0-9]|2[0-3]|[0-9]):[0-5][0-9]$/i) !== null
+			this.valid = this.newValue.startTime && this.newValue.startTime.match(/^(0[0-9]|1[0-9]|2[0-3]|[0-9]):[0-5][0-9]$/i) !== null
 				&& this.newValue.endTime && this.newValue.endTime.match(/^(0[0-9]|1[0-9]|2[0-3]|[0-9]):[0-5][0-9]$/i) !== null
 				&& moment.tz.zone(this.newValue.timezone) !== null
+			if (this.valid) {
+				this.$emit('valid')
+			} else {
+				this.$emit('invalid')
+			}
+			return this.valid
 		},
 		update() {
+			if (this.newValue.timezone === null) {
+				this.newValue.timezone = moment.tz.guess()
+			}
 			if (this.validate()) {
 				const output = `["${this.newValue.startTime} ${this.newValue.timezone}","${this.newValue.endTime} ${this.newValue.timezone}"]`
 				this.$emit('input', output)
-				this.valid = true
-			} else {
-				this.valid = false
 			}
 		},
 	},
diff --git a/apps/workflowengine/src/components/Rule.vue b/apps/workflowengine/src/components/Rule.vue
index 51eb0665eeb..7796180be6f 100644
--- a/apps/workflowengine/src/components/Rule.vue
+++ b/apps/workflowengine/src/components/Rule.vue
@@ -10,6 +10,7 @@
 				<Check :check="check"
 					:rule="rule"
 					@update="updateRule"
+					@validate="validate"
 					@remove="removeCheck(check)" />
 			</p>
 			<p>
@@ -77,7 +78,6 @@ export default {
 			checks: [],
 			error: null,
 			dirty: this.rule.id < 0,
-			checking: false,
 			originalRule: null,
 		}
 	},
@@ -93,7 +93,7 @@ export default {
 					tooltip: { placement: 'bottom', show: true, content: this.error },
 				}
 			}
-			if (!this.dirty || this.checking) {
+			if (!this.dirty) {
 				return { title: t('workflowengine', 'Active'), class: 'icon icon-checkmark' }
 			}
 			return { title: t('workflowengine', 'Save'), class: 'icon-confirm-white primary' }
@@ -112,22 +112,17 @@ export default {
 			this.$set(this.rule, 'operation', operation)
 			await this.updateRule()
 		},
-		async updateRule() {
-			this.checking = true
+		validate(state) {
+			this.error = null
+			this.$store.dispatch('updateRule', this.rule)
+		},
+		updateRule() {
 			if (!this.dirty) {
 				this.dirty = true
 			}
-			try {
-				// TODO: add new verify endpoint
-				// let result = await axios.post(OC.generateUrl(`/apps/workflowengine/operations/test`), this.rule)
-				this.error = null
-				this.checking = false
-				this.$store.dispatch('updateRule', this.rule)
-			} catch (e) {
-				console.error('Failed to update operation', e)
-				this.error = e.response.ocs.meta.message
-				this.checking = false
-			}
+
+			this.error = null
+			this.$store.dispatch('updateRule', this.rule)
 		},
 		async saveRule() {
 			try {
-- 
GitLab