Skip to content

Commit

Permalink
⚡ (setVariable) Add "Moment of the day" variable value
Browse files Browse the repository at this point in the history
Closes #564
  • Loading branch information
baptisteArno committed Jun 15, 2023
1 parent fbe63aa commit d8c1a36
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const getExpression =
case 'Today':
case 'Tomorrow':
case 'User ID':
case 'Moment of the day':
case 'Yesterday': {
return `${variableName} = ${options.type}`
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormLabel, Stack, Text } from '@chakra-ui/react'
import { Alert, AlertIcon, FormLabel, Stack, Tag, Text } from '@chakra-ui/react'
import { CodeEditor } from '@/components/inputs/CodeEditor'
import { SetVariableOptions, Variable, valueTypes } from '@typebot.io/schemas'
import React from 'react'
Expand Down Expand Up @@ -131,6 +131,18 @@ const SetVariableValue = ({
</Stack>
)
}
case 'Moment of the day': {
return (
<Alert fontSize="sm">
<AlertIcon />
<Text>
Will return either <Tag size="sm">morning</Tag>,{' '}
<Tag size="sm">afternoon</Tag>,<Tag size="sm">evening</Tag> or{' '}
<Tag size="sm">night</Tag> based on the current user time.
</Text>
</Alert>
)
}
case 'Random ID':
case 'Today':
case 'Tomorrow':
Expand Down
10 changes: 9 additions & 1 deletion apps/docs/docs/editor/blocks/logic/set-variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ For example,

:::

## Map
## Moment of the day

It will set your variable with either one of these values based on the user's time of the day: `morning`, `afternoon`, `evening`, `night`.

Then you can use this variable to conditionally display content:

<img src="/img/blocks/logic/moment-condition.png" width="400" alt="Moment of the day condition"/>

## Map item with same index

This is a convenient value block that allows you to easily get an item from a list that has the same index as an item from another list.

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ export const executeSetVariable = async (
return {
outgoingEdgeId: block.outgoingEdgeId,
}
if (block.options.isExecutedOnClient && block.options.expressionToEvaluate) {
const expressionToEvaluate = getExpressionToEvaluate(state.result.id)(
block.options
)
const isCustomValue = !block.options.type || block.options.type === 'Custom'
if (
expressionToEvaluate &&
((isCustomValue && block.options.isExecutedOnClient) ||
block.options.type === 'Moment of the day')
) {
const scriptToExecute = parseScriptToExecuteClientSideAction(
state.typebot.variables,
block.options.expressionToEvaluate
expressionToEvaluate
)
return {
outgoingEdgeId: block.outgoingEdgeId,
Expand All @@ -31,9 +39,6 @@ export const executeSetVariable = async (
],
}
}
const expressionToEvaluate = getExpressionToEvaluate(state.result.id)(
block.options
)
const evaluatedExpression = expressionToEvaluate
? evaluateSetVariableExpression(variables)(expressionToEvaluate)
: undefined
Expand Down Expand Up @@ -92,6 +97,13 @@ const getExpressionToEvaluate =
case 'Empty': {
return null
}
case 'Moment of the day': {
return `const now = new Date()
if(now.getHours() < 12) return 'morning'
if(now.getHours() >= 12 && now.getHours() < 18) return 'afternoon'
if(now.getHours() >= 18) return 'evening'
if(now.getHours() >= 22 || now.getHours() < 6) return 'night'`
}
case 'Custom':
case undefined: {
return options.expressionToEvaluate ?? null
Expand Down
1 change: 1 addition & 0 deletions packages/schemas/features/blocks/logic/setVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const valueTypes = [
'Yesterday',
'Tomorrow',
'Random ID',
'Moment of the day',
'Map item with same index',
] as const

Expand Down

4 comments on commit d8c1a36

@vercel
Copy link

@vercel vercel bot commented on d8c1a36 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-typebot-io.vercel.app
docs-git-main-typebot-io.vercel.app
docs.typebot.io

@vercel
Copy link

@vercel vercel bot commented on d8c1a36 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

bii.bj
1stop.au
wasap.nl
yobot.me
klujo.com
me.cr8.ai
wachat.io
wassep.io
247987.com
8jours.top
aginap.com
ai.mprs.in
bee.cr8.ai
bot.aws.bj
bot.bbc.bj
cat.cr8.ai
finplex.be
jxi.cr8.ai
nepkit.com
pig.cr8.ai
sat.cr8.ai
wachats.me
wsapio.com
blogely.com
bot.aipr.kr
bot.joof.it
bot.kloo.me
bull.cr8.ai
docs.cr8.ai
getyour.sbs
icon.cr8.ai
minipost.uk
mole.cr8.ai
team.cr8.ai
wolf.cr8.ai
ai.meant.com
bet7k.online
bot.afric.ai
bot.grace.bj
cinecorn.com
gniorder.com
help.taxt.co
kusamint.com
rhino.cr8.ai
sheep.cr8.ai
snake.cr8.ai
svhm.mprs.in
tiger.cr8.ai
video.cr8.ai
yoda.riku.ai
type.opaulovieira.com.br
aibot.angrybranding.co.uk
app.photosbackupin1.click
bot.aidigitalmarketing.kr
bot.amicidisanfaustino.it
bot.arraesecenteno.com.br
bot.blackboxsports.com.br
bot.cabinrentalagency.com
bot.fusionstarreviews.com
boyfriend-breakup.riku.ai
brigadeirosemdrama.com.br
chat.ertcrebateportal.com
chat.thehomebuyersusa.com
chat.thisiscrushhouse.com
chat2.ambassadorelena.com
healthandsafetycourses.uk
sellmyharleylouisiana.com
testbot.sharemyreview.net
typebot-viewer.vercel.app
verfica.botmachine.com.br
ap-help.algorithmpress.com
ap-main.algorithmpress.com
asking.aschenputtel.agency
bcorporate.carlosbusch.com
bot.adventureconsulting.hu
bot2.fusionstarreviews.com
casestudyemb.wpwakanda.com
chat.atlasoutfittersk9.com
configurator.bouclidom.com
demo.virtuesocialmedia.com
gabinete.baleia.formulario
help.atlasoutfittersk9.com
herbalife.barrettamario.it
homepageonly.wpwakanda.com
liveconvert.kandalearn.com
mainmenu1one.wpwakanda.com
newsletter.itshcormeos.com
prenotazione.aldoemaria.it
protocolosecabarriga.store
rsvp.virtuesocialmedia.com
tarian.theiofoundation.org
ted.meujalecobrasil.com.br
type.dericsoncalari.com.br
baleia.eventos.progenbr.com
bot.desafioserrabarriga.fit
bot.pinpointinteractive.com
bot.polychromes-project.com
bot.seidinembroseanchetu.it
chat.semanalimpanome.com.br
designguide.techyscouts.com

@vercel
Copy link

@vercel vercel bot commented on d8c1a36 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on d8c1a36 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app

Please sign in to comment.