Browse Source

feat: add comparison handler

Ahmad Kholid 3 years ago
parent
commit
c18201c31a

+ 51 - 17
src/background/blocks-handler.js

@@ -2,6 +2,7 @@
 import browser from 'webextension-polyfill';
 import { objectHasKey } from '@/utils/helper';
 import dataExporter from '@/utils/data-exporter';
+import compareBlockValue from '@/utils/compare-block-value';
 
 function getBlockConnection(block, index = 1) {
   const blockId = block.outputs[`output_${index}`]?.connections[0]?.node;
@@ -72,8 +73,11 @@ export function openWebsite(block) {
 }
 
 export function interactionHandler(block) {
-  return new Promise((resolve) => {
-    if (!this._connectedTab) return;
+  return new Promise((resolve, reject) => {
+    if (!this._connectedTab) {
+      reject(new Error("There's no website is opened"));
+      return;
+    }
 
     this._connectedTab.postMessage(block);
     this._listener({
@@ -83,21 +87,22 @@ export function interactionHandler(block) {
       delay: block.name === 'link' ? 5000 : 0,
       callback: (data) => {
         if (objectHasKey(block.data, 'dataColumn')) {
-          console.log(data);
-          const column =
-            Object.values(this.workflow.dataColumns).find(
-              ({ name }) => name === block.data.dataColumn
-            ) ?? {};
-          const name = column.name || 'column';
-
-          if (!objectHasKey(this.data, name)) this.data[name] = [];
-
-          if (Array.isArray(data)) {
-            data.forEach((item) => {
-              this.data[name].push(convertData(item, column.type));
-            });
-          } else {
-            this.data[name].push(convertData(data, column.type));
+          const column = Object.values(this.workflow.dataColumns).find(
+            ({ name }) => name === block.data.dataColumn
+          );
+
+          if (column) {
+            const { name } = column;
+
+            if (!objectHasKey(this.data, name)) this.data[name] = [];
+
+            if (Array.isArray(data)) {
+              data.forEach((item) => {
+                this.data[name].push(convertData(item, column.type));
+              });
+            } else {
+              this.data[name].push(convertData(data, column.type));
+            }
           }
         }
 
@@ -153,3 +158,32 @@ export function elementExists(block) {
     });
   });
 }
+
+export function conditions({ data, outputs }, prevBlockData) {
+  return new Promise((resolve, reject) => {
+    if (data.conditions.length === 0) {
+      reject(new Error('Conditions is empty'));
+      return;
+    }
+
+    let outputIndex = data.conditions.length + 1;
+    let resultData = '';
+    const prevData = Array.isArray(prevBlockData)
+      ? prevBlockData[0]
+      : prevBlockData;
+
+    data.conditions.forEach(({ type, value }, index) => {
+      const result = compareBlockValue(type, prevData, value);
+
+      if (result) {
+        resultData = value;
+        outputIndex = index + 1;
+      }
+    });
+    console.log(resultData, outputIndex);
+    resolve({
+      data: resultData,
+      nextBlockId: getBlockConnection({ outputs }, outputIndex),
+    });
+  });
+}

+ 1 - 2
src/components/block/BlockConditions.vue

@@ -84,7 +84,6 @@
 <script setup>
 import { watch, toRaw } from 'vue';
 import { VRemixIcon as VRemixicon } from 'v-remixicon';
-import { nanoid } from 'nanoid';
 import emitter from 'tiny-emitter/instance';
 import { debounce } from '@/utils/helper';
 import { icons } from '@/lib/v-remixicon';
@@ -113,7 +112,7 @@ const conditions = {
 function addComparison() {
   if (block.data.conditions.length >= 5) return;
 
-  block.data.conditions.push({ id: nanoid(6), type: '==', value: '' });
+  block.data.conditions.push({ type: '==', value: '' });
 
   if (block.data.conditions.length === 1) props.editor.addNodeOutput(block.id);
 

+ 4 - 5
src/content/blocks-handler.js

@@ -78,7 +78,7 @@ export function attributeValue({ data }) {
 export function forms({ data }) {
   return new Promise((resolve) => {
     const elements = handleElement(data, true);
-    console.log(elements, 'form');
+
     if (data.multiple) {
       const promises = Array.from(elements).map((element) => {
         return new Promise((eventResolve) => {
@@ -87,13 +87,12 @@ export function forms({ data }) {
       });
 
       Promise.allSettled(promises).then(() => {
-        console.log('hola amigo');
         resolve('');
       });
     } else if (elements) {
       handleFormElement(elements, data, resolve);
     } else {
-      resolve();
+      resolve('');
     }
   });
 }
@@ -104,7 +103,7 @@ export function triggerEvent({ data }) {
       simulateEvent(element, data.eventName, data.eventParams);
     });
 
-    resolve('');
+    resolve(data.eventName);
   });
 }
 
@@ -121,7 +120,7 @@ export function link({ data }) {
 
     if (url) window.location.href = url;
 
-    resolve('');
+    resolve(url);
   });
 }
 

+ 3 - 3
src/utils/handle-form-element.js

@@ -65,16 +65,16 @@ export default function (element, data, callback) {
   if (data.type === 'checkbox' || data.type === 'radio') {
     element.checked = data.selected;
     formEvent(element, { type: data.type, value: data.selected });
-    callback();
+    callback(element.checked);
     return;
   }
 
   if (data.type === 'select') {
     element.value = data.value;
     formEvent(element, data);
-    callback();
+    callback(element.value);
     return;
   }
 
-  callback();
+  callback('');
 }

+ 1 - 1
src/utils/shared.js

@@ -1,4 +1,4 @@
-/* link, tab loaded, and close tab block? */
+/* screenshot, assets, tab loaded, and close tab block? */
 
 export const tasks = {
   trigger: {