Skip to content

Commit

Permalink
feat(settings): ✨ Add create result on page refresh option
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 7, 2022
1 parent e3e07dd commit 260819f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
16 changes: 16 additions & 0 deletions apps/builder/components/settings/GeneralSettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useDisclosure,
} from '@chakra-ui/react'
import { UpgradeModal } from 'components/shared/modals/UpgradeModal.'
import { SwitchWithLabel } from 'components/shared/SwitchWithLabel'
import { useUser } from 'contexts/UserContext'
import { GeneralSettings } from 'models'
import React from 'react'
Expand All @@ -27,10 +28,19 @@ export const GeneralSettingsForm = ({
const handleSwitchChange = () => {
if (generalSettings?.isBrandingEnabled && isUserFreePlan) return
onGeneralSettingsChange({
...generalSettings,
isBrandingEnabled: !generalSettings?.isBrandingEnabled,
})
}

const handleNewResultOnRefreshChange = (
isNewResultOnRefreshEnabled: boolean
) =>
onGeneralSettingsChange({
...generalSettings,
isNewResultOnRefreshEnabled,
})

return (
<Stack spacing={6}>
<UpgradeModal isOpen={isOpen} onClose={onClose} />
Expand All @@ -49,6 +59,12 @@ export const GeneralSettingsForm = ({
onChange={handleSwitchChange}
/>
</Flex>
<SwitchWithLabel
id="new-result"
label="Create new session on page refresh"
initialValue={generalSettings.isNewResultOnRefreshEnabled ?? false}
onCheckChange={handleNewResultOnRefreshChange}
/>
</Stack>
)
}
4 changes: 4 additions & 0 deletions apps/builder/playwright/tests/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ test.describe.parallel('Settings page', () => {
await expect(
typebotViewer(page).locator('a:has-text("Made with Typebot")')
).toBeHidden()
await page.click('text=Create new session on page refresh')
await expect(
page.locator('input[type="checkbox"] >> nth=-1')
).toHaveAttribute('checked', '')
})
})

Expand Down
3 changes: 2 additions & 1 deletion apps/viewer/layouts/TypebotPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export const TypebotPage = ({
if (error) setError(error)
if (result) {
setResultId(result.id)
setResultInSession(result.id)
if (typebot.settings.general.isNewResultOnRefreshEnabled !== true)
setResultInSession(result.id)
}
}
}
Expand Down
67 changes: 67 additions & 0 deletions apps/viewer/playwright/tests/settings.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import test, { expect } from '@playwright/test'
import { createTypebots, parseDefaultBlockWithStep } from '../services/database'
import { generate } from 'short-uuid'
import { defaultSettings, defaultTextInputOptions, InputStepType } from 'models'

test('Result should be in storage by default', async ({ page }) => {
const typebotId = generate()
await createTypebots([
{
id: typebotId,
...parseDefaultBlockWithStep({
type: InputStepType.TEXT,
options: defaultTextInputOptions,
}),
},
])
await page.goto(`/${typebotId}-public`)
await page.waitForResponse(
(resp) =>
resp.request().url().includes(`/api/typebots/${typebotId}/results`) &&
resp.status() === 200 &&
resp.request().method() === 'POST'
)
await page.reload()
const resultId = await page.evaluate(() => sessionStorage.getItem('resultId'))
expect(resultId).toBeDefined()
})

test.describe('Create result on page refresh enabled', () => {
test('should work', async ({ page }) => {
const typebotId = generate()
await createTypebots([
{
id: typebotId,
settings: {
...defaultSettings,
general: {
...defaultSettings.general,
isNewResultOnRefreshEnabled: true,
},
},
...parseDefaultBlockWithStep({
type: InputStepType.TEXT,
options: defaultTextInputOptions,
}),
},
])
await page.goto(`/${typebotId}-public`)
await page.waitForResponse(
(resp) =>
resp.request().url().includes(`/api/typebots/${typebotId}/results`) &&
resp.status() === 200 &&
resp.request().method() === 'POST'
)
await page.reload()
await page.waitForResponse(
(resp) =>
resp.request().url().includes(`/api/typebots/${typebotId}/results`) &&
resp.status() === 200 &&
resp.request().method() === 'POST'
)
const resultId = await page.evaluate(() =>
sessionStorage.getItem('resultId')
)
expect(resultId).toBe(null)
})
})
3 changes: 2 additions & 1 deletion packages/models/src/typebot/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type Settings = {

export type GeneralSettings = {
isBrandingEnabled: boolean
isNewResultOnRefreshEnabled?: boolean
}

export type TypingEmulation = {
Expand All @@ -22,7 +23,7 @@ export type Metadata = {
}

export const defaultSettings: Settings = {
general: { isBrandingEnabled: true },
general: { isBrandingEnabled: true, isNewResultOnRefreshEnabled: false },
typingEmulation: { enabled: true, speed: 300, maxDelay: 1.5 },
metadata: {
description:
Expand Down

2 comments on commit 260819f

@vercel
Copy link

@vercel vercel bot commented on 260819f Mar 7, 2022

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

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

@vercel
Copy link

@vercel vercel bot commented on 260819f Mar 7, 2022

Please sign in to comment.