2
0

feat(viewer): 💄 Clear param on page load

This commit is contained in:
Baptiste Arnaud
2022-04-19 14:54:20 -07:00
parent a089a451b6
commit 93811a3c48

View File

@ -1,5 +1,6 @@
import { TypebotViewer } from 'bot-engine'
import { Answer, PublicTypebot, VariableWithValue } from 'models'
import { useRouter } from 'next/router'
import React, { useEffect, useState } from 'react'
import { upsertAnswer } from 'services/answer'
import { SEO } from '../components/Seo'
@ -19,6 +20,7 @@ export const TypebotPage = ({
isIE,
url,
}: TypebotPageProps & { typebot: PublicTypebot }) => {
const { asPath, push } = useRouter()
const [showTypebot, setShowTypebot] = useState(false)
const [predefinedVariables, setPredefinedVariables] = useState<{
[key: string]: string
@ -30,6 +32,7 @@ export const TypebotPage = ({
useEffect(() => {
const urlParams = new URLSearchParams(location.search)
clearQueryParams()
const predefinedVariables: { [key: string]: string } = {}
urlParams.forEach((value, key) => {
predefinedVariables[key] = value
@ -39,6 +42,11 @@ export const TypebotPage = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const clearQueryParams = () => {
const hasQueryParams = asPath.includes('?')
if (hasQueryParams) push(asPath.split('?')[0], undefined, { shallow: true })
}
const initializeResult = async () => {
const resultIdFromSession = getExistingResultFromSession()
if (resultIdFromSession) setResultId(resultIdFromSession)