Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"socket.io-client": "^4.7.2",
"svg-inline-loader": "^0.8.2",
"tinycolor2": "^1.4.2",
"uuid": "^13.0.0",
"vue": "^2.6.12",
"vue-deepset": "^0.6.3",
"vue-inline-svg": "^2.1.3",
Expand Down
165 changes: 165 additions & 0 deletions src/components/inspectors/ConditionalRedirect/ConditionalRedirect.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<template>
<div>
<label>{{ $t(label) }}</label>
<div class="d-flex justify-content-between align-items-center mb-2">
<label for="conditionalRedirectEnabled" class="text-muted small">
{{ $t("Enable to add rules that route users to different tasks. If none match, the default destination is used.") }}
</label>
<b-form-checkbox
id="conditionalRedirectEnabled"
v-model="isEnabled"
name="conditionalRedirectEnabled"
:aria-checked="isEnabled"
switch
data-test="conditional-toggle"
/>
</div>
<div v-if="isEnabled">
<button
type="button"
class="btn btn-light"
@click="addCondition"
:disabled="conditions.length >= MAX_CONDITIONS"
data-test="conditional-add-button"
>
<i class="fas fa-plus-circle" />
</button>

<div
v-for="condition in conditions"
:key="condition.id"
data-test="conditional-box"
>
<TaskDestination
:value="condition"
:taskDestinationOptions="taskDestinationOptions"
:conditionId="condition.id"
@input="onSaveCondition"
@duplicate="onDuplicateCondition"
@remove="onRemoveCondition"
/>
</div>

<hr class="my-2">

<div class="d-flex justify-content-end">
<small class="text-muted text-right">
{{ $t('Rules are evaluated top to bottom.') }}
</small>
</div>
</div>
</div>
</template>

<script>
import TaskDestination from './TaskDestination.vue';
import { v4 as uuidv4 } from 'uuid';

const MAX_CONDITIONS = 10;

export default {
components: {
TaskDestination,
},
props: {
value: {
type: String,
},
label: {
type: String,
required: true,
},
options: {
type: Array,
required: true,
},
},
data() {
return {
isEnabled: false,
conditions: [],
taskDestinationOptions: [],
MAX_CONDITIONS,
};
},
watch: {
isEnabled: {
handler() {
this.updateConditionalRedirect();
},
},
},
mounted() {
this.initTaskDestinationOptions();

if (this.value) {
try {
const local = JSON.parse(this.value);

this.isEnabled = local.isEnabled ?? false;
this.conditions = local.conditions ?? [];
} catch (error) {
console.warn(error, 'Error parsing conditional redirect', this.value);
}
}
},
methods: {
initTaskDestinationOptions() {
this.taskDestinationOptions = this.options.map(({ value, content }) => ({
value,
content: this.$t(content),
}));

this.taskDestination = this.taskDestinationOptions?.[0] ?? null;
},
updateConditionalRedirect() {
const data = JSON.stringify({
isEnabled: this.isEnabled,
conditions: this.conditions,
});

this.$emit('input', data);
},
addCondition() {
if (this.conditions.length >= MAX_CONDITIONS) {
return;
}

this.conditions.push({
id: uuidv4(),
condition: '',
taskDestination: null,
});
Comment thread
devmiguelangel marked this conversation as resolved.

this.updateConditionalRedirect();
},
onSaveCondition(value) {
const index = this.conditions.findIndex((condition) => condition.id === value.conditionId);

if (index !== -1) {
this.conditions[index] = {
...this.conditions[index],
...value.condition,
};
}

this.updateConditionalRedirect();
},
onDuplicateCondition(conditionId) {
const condition = this.conditions.find((condition) => condition.id === conditionId);

this.conditions.push({
...condition,
id: uuidv4(),
});

this.updateConditionalRedirect();
},
onRemoveCondition(conditionId) {
this.conditions = this.conditions.filter((condition) => condition.id !== conditionId);

this.updateConditionalRedirect();
},
},
};
</script>
Loading
Loading