Skip to content

Commit

Permalink
Go to URL
Browse files Browse the repository at this point in the history
  • Loading branch information
wsieroci committed Feb 19, 2024
1 parent 1b7fb05 commit b06a9f2
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 102 deletions.
8 changes: 5 additions & 3 deletions api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ActionSelectedType(str, Enum):
Jump = 'jump'
Hide = 'hide'
Go = 'go'
Url = 'url'


class IsOperatorSelectedType(str, Enum):
Expand All @@ -85,10 +86,11 @@ class IsOperatorSelectedType(str, Enum):

class BlockCondition(Block):
whenBlockSelectedId: str
isOperatorSelectedId: IsOperatorSelectedType
isOperatorValue: Union[str, int]
isOperatorSelectedId: Optional[IsOperatorSelectedType] = None
isOperatorValue: Optional[Union[str, int]] = None
actionSelectedId: ActionSelectedType
actionBlockSelectedId: str
actionBlockSelectedId: Optional[str] = None
actionUrl: Optional[str] = None

@root_validator(pre=True)
def check_type(cls, values):
Expand Down
27 changes: 17 additions & 10 deletions src/components/Simulai.vue
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ async function publishPage() {
}
function checkIfBlockShouldShowSkipButton(i, block) {
debugger;
return props.page.isChat && block.isRequired === false && i === currentVisibleBlock.value
}
Expand Down Expand Up @@ -442,7 +441,6 @@ function typingHasCompleted() {
}
function showNextBlock() {
debugger;
if (props.page.blocks.length === 0) {
setTimeout(() => showNextBlock(), 1000)
return
Expand All @@ -454,13 +452,15 @@ function showNextBlock() {
return
}
const nowBlock = props.page.blocks[currentVisibleBlock.value]
if (currentVisibleBlock.value === null) {
currentVisibleBlock.value = 0
scrollToBottom()
} else if (showUntilAndWait && currentVisibleBlock.value === showUntilAndWait) {
chatInput.value.focusInput()
return
} else {
} else if (nowBlock.type !== BlockType.Condition) {
currentVisibleBlock.value += 1
scrollToBottom()
setTimeout(() => {
Expand Down Expand Up @@ -509,16 +509,16 @@ function showNextBlock() {
const hide = resultAction.hide
const jump = resultAction.jump
const go = resultAction.go
const url = resultAction.url
if (hide.length > 0) {
hide.forEach(hideId => {
const foundItem = props.page.blocks.find(block => block.id === hideId)
if (foundItem) {
foundItem.isHidden = true
}
})
}
if (jump !== null) {
currentVisibleBlock.value = currentVisibleBlock.value + 1
} else if (jump !== null) {
// const foundItem = props.page.blocks.find(block => block.id === jump)
let i = currentVisibleBlock.value + 1
while (true) {
Expand All @@ -531,9 +531,7 @@ function showNextBlock() {
}
i++;
}
}
if (go !== null) {
} else if (go !== null) {
const foundBot = props.page.workspaceBots.find(bot => bot.id === go)
const foundBotBlocks = foundBot.blocks
Expand All @@ -542,6 +540,16 @@ function showNextBlock() {
// Insert foundBotBlocks after currentIndex
props.page.blocks.splice(currentVisibleBlock.value + 1, 0, ...foundBotBlocks);
currentVisibleBlock.value = currentVisibleBlock.value + 1
} else if (url !== null) {
if (window.self === window.top) {
window.location.href = url;
} else {
parent.window.location.href = url;
}
return;
} else {
currentVisibleBlock.value = currentVisibleBlock.value + 1
}
setTimeout(() => {
Expand Down Expand Up @@ -581,7 +589,6 @@ function isYouVisibleBlock(block: Block, i: number) {
}
function goNextBlock() {
debugger;
showNextBlock()
const currentBlock = props.page.blocks[currentVisibleBlock.value]
if (chatInput.value && shouldWaitForValueFromInput(currentBlock)) {
Expand Down
16 changes: 14 additions & 2 deletions src/components/blocks/ConditionBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</div>
</div>

<div class="flex items-center w-full mt-2 md:mt-2 ">
<div v-if="props.block.whenBlockSelectedId !== ALWAYS_CONDITION" class="flex items-center w-full mt-2 md:mt-2 ">
<label class="text-gray-300 mr-2">Is:</label>
<div class="flex items-center h-full"> <!-- Set the parent container's height to 100% -->
<div class="relative">
Expand Down Expand Up @@ -88,6 +88,7 @@
<div class="relative h-full">
<div class="relative">
<Dropdown
v-if="props.block.actionSelectedId !== ComparisonsAction.Url"
v-model="props.block.actionBlockSelectedId"
:options="allNextBlockOptions"
optionLabel="name"
Expand All @@ -96,6 +97,13 @@
class="w-32 md:w-64 h-full ml-1"
:key="blocksHistoryKey"
/>
<input
v-if="props.block.actionSelectedId === ComparisonsAction.Url"
v-model="props.block.actionUrl"
type="text"
class="w-full h-full bg-gray-700 placeholder-gray-200 text-gray-300 focus:outline-none p-2 rounded-md ml-1"
placeholder=""
/>
</div>
</div>
</div>
Expand All @@ -114,7 +122,7 @@ import {
ComparisonType,
getBlockFuncs,
getBlockOptions,
isFlowBlock
isFlowBlock, WorkspaceBot
} from '@/utils/types'
import {
setUpInitialValuesForBlock,
Expand All @@ -124,6 +132,7 @@ import {
import Dropdown from '../elements/Dropdown.vue';
import DatePicker from "vue3-datepicker";
import Calendar from 'primevue/calendar';
import {ALWAYS_CONDITION} from "@/utils/conditions";
const previousInputBlockOptions = ref([])
const allNextBlockOptions = ref([])
Expand All @@ -146,6 +155,7 @@ const actionOptions = ref([
{value: ComparisonsAction.Jump, name: 'Jump to block'},
{value: ComparisonsAction.Hide, name: 'Hide block'},
{value: ComparisonsAction.Go, name: 'Go to bot'},
{value: ComparisonsAction.Url, name: 'Go to URL'},
]);
const props = defineProps({
Expand Down Expand Up @@ -214,6 +224,8 @@ function updatePreviousInputBlocksDropdowns() {
const optionTitle = `[${index + 1}] ${title} ...`; // Adding 1 to the index to start from 1 instead of 0
return {'value': block.id, 'name': optionTitle};
});
previousInputBlockOptions.value.push({'value': ALWAYS_CONDITION, 'name': ALWAYS_CONDITION})
}
function updateAllNextBlocksDropdowns() {
Expand Down
182 changes: 97 additions & 85 deletions src/utils/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,131 @@
import {Block, BlockCondition, ComparisonsAction, ComparisonsValue, ComparisonType, getBlockOptions} from './types'

export const ALWAYS_CONDITION = 'Always'

export function calculateConditionAction(
condition: BlockCondition, blocks: Block[]
) {
const whenSelectedBlock = blocks.find((block) => block.id === condition.whenBlockSelectedId)
const resultAction = {
hide: [],
jump: null,
go: null
}
let isConditionSatisifed = false
if (!whenSelectedBlock) {
return resultAction
}
const blockOptions = getBlockOptions(whenSelectedBlock)
if (condition.isOperatorSelectedId === ComparisonsValue.EqualTo) {
if (whenSelectedBlock.details.value == condition.isOperatorValue) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.NotEqualTo) {
if (whenSelectedBlock.details.value != condition.isOperatorValue) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Greater) {
if (whenSelectedBlock.details.value > condition.isOperatorValue) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Less) {
if (whenSelectedBlock.details.value < condition.isOperatorValue) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Contains) {
if (whenSelectedBlock.details.value && whenSelectedBlock.details.value.includes(condition.isOperatorValue)) {
isConditionSatisifed = true
if (condition.whenBlockSelectedId === ALWAYS_CONDITION) {
isConditionSatisifed = true
} else {
const whenSelectedBlock = blocks.find((block) => block.id === condition.whenBlockSelectedId)
if (!whenSelectedBlock) {
return resultAction
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Selected) {
const foundItem = whenSelectedBlock.items.find(item => item.label === condition.isOperatorValue && item.isChecked === true)
if (foundItem) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Before) {
if (blockOptions.comparisonType === ComparisonType.Time) {
const blockTimeParts = whenSelectedBlock.details.value.split(":");
const conditionTimeParts = condition.isOperatorValue.split(":");
const blockOptions = getBlockOptions(whenSelectedBlock)
if (condition.isOperatorSelectedId === ComparisonsValue.EqualTo) {
if (whenSelectedBlock.details.value == condition.isOperatorValue) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.NotEqualTo) {
if (whenSelectedBlock.details.value != condition.isOperatorValue) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Greater) {
if (whenSelectedBlock.details.value > condition.isOperatorValue) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Less) {
if (whenSelectedBlock.details.value < condition.isOperatorValue) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Contains) {
if (whenSelectedBlock.details.value && whenSelectedBlock.details.value.includes(condition.isOperatorValue)) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Selected) {
const foundItem = whenSelectedBlock.items.find(item => item.label === condition.isOperatorValue && item.isChecked === true)
if (foundItem) {
isConditionSatisifed = true
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.Before) {
if (blockOptions.comparisonType === ComparisonType.Time) {
const blockTimeParts = whenSelectedBlock.details.value.split(":");
const conditionTimeParts = condition.isOperatorValue.split(":");

const blockHours = parseInt(blockTimeParts[0]);
const blockMinutes = parseInt(blockTimeParts[1]);
const blockHours = parseInt(blockTimeParts[0]);
const blockMinutes = parseInt(blockTimeParts[1]);

const conditionHours = parseInt(conditionTimeParts[0]);
const conditionMinutes = parseInt(conditionTimeParts[1]);
const conditionHours = parseInt(conditionTimeParts[0]);
const conditionMinutes = parseInt(conditionTimeParts[1]);

if (blockHours < conditionHours || (blockHours === conditionHours && blockMinutes < conditionMinutes)) {
isConditionSatisifed = true;
}
} else {
const blockDate = new Date(whenSelectedBlock.details.value)
const conditionDate = new Date(condition.isOperatorValue)
if (blockHours < conditionHours || (blockHours === conditionHours && blockMinutes < conditionMinutes)) {
isConditionSatisifed = true;
}
} else {
const blockDate = new Date(whenSelectedBlock.details.value)
const conditionDate = new Date(condition.isOperatorValue)

if (blockDate < conditionDate) {
isConditionSatisifed = true
if (blockDate < conditionDate) {
isConditionSatisifed = true
}
}
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.After) {
if (blockOptions.comparisonType === ComparisonType.Time) {
const blockTimeParts = whenSelectedBlock.details.value.split(":");
const conditionTimeParts = condition.isOperatorValue.split(":");
} else if (condition.isOperatorSelectedId === ComparisonsValue.After) {
if (blockOptions.comparisonType === ComparisonType.Time) {
const blockTimeParts = whenSelectedBlock.details.value.split(":");
const conditionTimeParts = condition.isOperatorValue.split(":");

const blockHours = parseInt(blockTimeParts[0]);
const blockMinutes = parseInt(blockTimeParts[1]);
const blockHours = parseInt(blockTimeParts[0]);
const blockMinutes = parseInt(blockTimeParts[1]);

const conditionHours = parseInt(conditionTimeParts[0]);
const conditionMinutes = parseInt(conditionTimeParts[1]);
const conditionHours = parseInt(conditionTimeParts[0]);
const conditionMinutes = parseInt(conditionTimeParts[1]);

if (blockHours > conditionHours || (blockHours === conditionHours && blockMinutes > conditionMinutes)) {
isConditionSatisifed = true;
}
} else {
const blockDate = new Date(whenSelectedBlock.details.value)
const conditionDate = new Date(condition.isOperatorValue)
if (blockHours > conditionHours || (blockHours === conditionHours && blockMinutes > conditionMinutes)) {
isConditionSatisifed = true;
}
} else {
const blockDate = new Date(whenSelectedBlock.details.value)
const conditionDate = new Date(condition.isOperatorValue)

if (blockDate > conditionDate) {
isConditionSatisifed = true
if (blockDate > conditionDate) {
isConditionSatisifed = true
}
}
}
} else if (condition.isOperatorSelectedId === ComparisonsValue.AtDate) {
if (blockOptions.comparisonType === ComparisonType.Time) {
const blockTimeParts = whenSelectedBlock.details.value.split(":");
const conditionTimeParts = condition.isOperatorValue.split(":");
} else if (condition.isOperatorSelectedId === ComparisonsValue.AtDate) {
if (blockOptions.comparisonType === ComparisonType.Time) {
const blockTimeParts = whenSelectedBlock.details.value.split(":");
const conditionTimeParts = condition.isOperatorValue.split(":");

const blockHours = parseInt(blockTimeParts[0]);
const blockMinutes = parseInt(blockTimeParts[1]);
const blockHours = parseInt(blockTimeParts[0]);
const blockMinutes = parseInt(blockTimeParts[1]);

const conditionHours = parseInt(conditionTimeParts[0]);
const conditionMinutes = parseInt(conditionTimeParts[1]);
const conditionHours = parseInt(conditionTimeParts[0]);
const conditionMinutes = parseInt(conditionTimeParts[1]);

if (blockHours === conditionHours && blockMinutes === conditionMinutes) {
isConditionSatisifed = true;
}
} else {
const blockDate = new Date(whenSelectedBlock.details.value)
const conditionDate = new Date(condition.isOperatorValue)
if (blockHours === conditionHours && blockMinutes === conditionMinutes) {
isConditionSatisifed = true;
}
} else {
const blockDate = new Date(whenSelectedBlock.details.value)
const conditionDate = new Date(condition.isOperatorValue)

if (blockDate.getTime() === conditionDate.getTime()) {
isConditionSatisifed = true
if (blockDate.getTime() === conditionDate.getTime()) {
isConditionSatisifed = true
}
}
}
}

const resultAction = {
hide: [],
jump: null,
go: null,
url: null,
}

debugger;

if (isConditionSatisifed) {
if (condition.actionSelectedId === ComparisonsAction.Hide) {
resultAction.hide = [condition.actionBlockSelectedId]
} else if (condition.actionSelectedId === ComparisonsAction.Jump) {
resultAction.jump = condition.actionBlockSelectedId
} else if(condition.actionSelectedId === ComparisonsAction.Go) {
} else if (condition.actionSelectedId === ComparisonsAction.Go) {
resultAction.go = condition.actionBlockSelectedId
} else if (condition.actionSelectedId === ComparisonsAction.Url) {
resultAction.url = condition.actionUrl
}
}

Expand Down
Loading

0 comments on commit b06a9f2

Please sign in to comment.